Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
592 views

Assignment 07 Answers

Uploaded by

Sam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
592 views

Assignment 07 Answers

Uploaded by

Sam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment:- 07

Triggers
Instructions:
Please share your answers filled in line in the Word document.
Submitcode
separately wherever applicable.

Please ensure you update all the details:


Name: _____________ Batch ID: ___________
Topic: Introduction to Database

1) Write an SQL query to accomplish the following tasks:

a) Create a database named student_db.


b) Create a table named students_details with columns id (integer),
name (varchar), age (integer), and grade (float). id should be set
as the primary key.
c) Insert any four records into students_details.
d) Create a new table named students_details_copy with the same
columns as students_details. id should also be set as the primary
key.
e) Create a trigger named after_insert_details that inserts a new
record into students_details_copy every time a record is inserted
into students_details.
f) Insert a new record into students_details.
g) check whether a record is filling in students_details_copy as you
insert value in students_details.

2) Write an SQL question that accomplishes the following tasks:

a) use student_db ,
b) Create a trigger named update_grade that automatically updates
the grade column every time a record in students_details is
updated based on the following criteria:
c) If the updated record has an age value less than 18, multiply the
grade by 0.9.
d) If the updated record has an age value between 18 and 20
(inclusive), multiply the grade by 1.1.

© 360DigiTMG. All Rights Reserved.


Assignment:- 07
Triggers
e) If the updated record has an age value greater than 20, multiply
the grade by 1.05.
f) Update the age value of one of the records in students_new to
see the trigger in action.

3) Explain the difference between the AFTER and INSTEAD OF trigger


operators in SQL.
4) What is the purpose of the INSTEAD OF DELETE trigger operator in
SQL?

ANSWERS:

### 1) SQL Queries:

a) Create a database named student_db:

```sql
CREATE DATABASE student_db;
```

b) Create a table named students_details with columns id (integer), name


(varchar), age (integer), and grade (float). id should be set as the primary key:

```sql
USE student_db;

CREATE TABLE students_details (


id INT PRIMARY KEY,
name VARCHAR(255),
age INT,
grade FLOAT
);
```

c) Insert any four records into students_details:

© 360DigiTMG. All Rights Reserved.


Assignment:- 07
Triggers
```sql
INSERT INTO students_details (id, name, age, grade)
VALUES
(1, 'John Doe', 20, 85.5),
(2, 'Jane Smith', 18, 92.0),
(3, 'Bob Johnson', 22, 78.5),
(4, 'Alice Brown', 19, 88.0);
```

d) Create a new table named students_details_copy with the same columns


as students_details. id should also be set as the primary key:

```sql
CREATE TABLE students_details_copy (
id INT PRIMARY KEY,
name VARCHAR(255),
age INT,
grade FLOAT
);
```

e) Create a trigger named after_insert_details that inserts a new record into


students_details_copy every time a record is inserted into students_details:

```sql
DELIMITER //

CREATE TRIGGER after_insert_details


AFTER INSERT ON students_details
FOR EACH ROW
BEGIN
INSERT INTO students_details_copy (id, name, age, grade)
VALUES (NEW.id, NEW.name, NEW.age, NEW.grade);
END //

DELIMITER ;
© 360DigiTMG. All Rights Reserved.
Assignment:- 07
Triggers
```

f) Insert a new record into students_details:

```sql
INSERT INTO students_details (id, name, age, grade)
VALUES (5, 'Eva Davis', 21, 94.5);
```

g) Check whether a record is filling in students_details_copy as you insert


value in students_details:

```sql
SELECT * FROM students_details_copy;
```

### 2) SQL Query:

a) Use student_db:

```sql
USE student_db;
```

b) Create a trigger named update_grade that automatically updates the


grade column every time a record in students_details is updated based on
the given criteria:

```sql
DELIMITER //

CREATE TRIGGER update_grade


BEFORE UPDATE ON students_details
FOR EACH ROW
BEGIN
IF NEW.age < 18 THEN
SET NEW.grade = NEW.grade * 0.9;
© 360DigiTMG. All Rights Reserved.
Assignment:- 07
Triggers
ELSEIF NEW.age >= 18 AND NEW.age <= 20 THEN
SET NEW.grade = NEW.grade * 1.1;
ELSE
SET NEW.grade = NEW.grade * 1.05;
END IF;
END //

DELIMITER ;
```

c) Update the age value of one of the records in students_details to see the
trigger in action:

```sql
UPDATE students_details
SET age = 19
WHERE id = 1;
```

### 3) Difference between AFTER and INSTEAD OF trigger operators in SQL:

- **AFTER Trigger:**
- Executes after the triggering event (e.g., AFTER INSERT, AFTER UPDATE).
- Operates on the result set that has already been modified.
- Commonly used for auditing, logging, or maintaining secondary tables.

- **INSTEAD OF Trigger:**
- Executes instead of the triggering event.
- Replaces the default action specified by the triggering event.
- Commonly used for complex scenarios where the default action needs to
be bypassed or replaced.

### 4) Purpose of the INSTEAD OF DELETE trigger operator in SQL:

- **INSTEAD OF DELETE Trigger:**


- Replaces the default action of the DELETE statement.

© 360DigiTMG. All Rights Reserved.


Assignment:- 07
Triggers
- Allows the developer to define custom logic to handle the deletion of
records.
- Useful in scenarios where cascading deletes need to be controlled, or soft
deletes need to be implemented by updating a status column instead of
physically deleting records.

© 360DigiTMG. All Rights Reserved.

You might also like