Assignment 07 Answers
Assignment 07 Answers
Triggers
Instructions:
Please share your answers filled in line in the Word document.
Submitcode
separately wherever applicable.
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.
ANSWERS:
```sql
CREATE DATABASE student_db;
```
```sql
USE student_db;
```sql
CREATE TABLE students_details_copy (
id INT PRIMARY KEY,
name VARCHAR(255),
age INT,
grade FLOAT
);
```
```sql
DELIMITER //
DELIMITER ;
© 360DigiTMG. All Rights Reserved.
Assignment:- 07
Triggers
```
```sql
INSERT INTO students_details (id, name, age, grade)
VALUES (5, 'Eva Davis', 21, 94.5);
```
```sql
SELECT * FROM students_details_copy;
```
a) Use student_db:
```sql
USE student_db;
```
```sql
DELIMITER //
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;
```
- **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.