0% found this document useful (0 votes)
5 views

lab6

lab 6 databases
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

lab6

lab 6 databases
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

2nd Year /Semester3 (2024-2025)

Oracle DBMS

Lab sheet 6: Triggers

OBJECTIVES:

• Implement database triggers to automate actions based on events (INSERT, UPDATE, DELETE) on tables.
• Enforce data integrity by preventing duplicate entries and ensuring correct business logic.
• Update table attributes dynamically using PL/SQL triggers to maintain accurate and consistent data.

A database trigger is a stored PL/SQL program which is automatically executed when some events occur. A trigger can be
executed in response to any of the following events:
1. A database manipulation (DML) statement like DELETE, INSERT or UPDATE.
2. A database definition (DDL) statement like CREATE, ALTER or DROP.
3. A database operation like SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN.

The syntax for creating a trigger is the following:


CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF}
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- Instruction PLSQL
END;

• CREATE [OR REPLACE] TRIGGER trigger_name: to create or overwrite an existing trigger.


• {BEFORE | AFTER | INSTEAD OF}: the trigger execution time, i.e. before or after updating the table. INSTEAD OF is used
to create a trigger on a view.
• {INSERT [OR] | UPDATE [OR] | DELETE}: the operation event that will execute the trigger. It is possible to specify several
events by separating them with OR.
• [OF col_name]: used with the Update operation when we want to execute a trigger only when a specific column is
updated.
• [ON table_name]: the name of the table on which the trigger is defined.
• [REFERENCING OLD AS o NEW AS n]: It is used to reference the old and new values of the data being changed. By
default, it is possible to reference the values as :old.column_name or :new.column_name. The old values cannot be
referenced when inserting a record and new values cannot be referenced when deleting a record because they do not
exist.
• [FOR EACH ROW]: specifies if the trigger is executed for each affected row or only once.
• WHEN (condition): the trigger is executed only when the affected row satisfies the condition.

Note: To generate an exception and prevent the program from proceeding, the user can execute the procedure
raise_application_error (Num_Message, 'Message to be displayed'); Num_Message is between 20000 and 20999.
Example: The following trigger shows the difference in the salary of an employee after executing DELETE, INSERT, or UPDATE
statements.

CREATE OR REPLACE TRIGGER trg_salary_difference


AFTER INSERT OR UPDATE OR DELETE
ON TEACHER
FOR EACH ROW
DECLARE
salary_difference NUMBER;
BEGIN

IF INSERTING THEN
-- On INSERT, the difference is simply the new salary
DBMS_OUTPUT.PUT_LINE ('Operation: INSERT');
DBMS_OUTPUT.PUT_LINE ('New Salary: ' || :NEW.Salary);

ELSIF UPDATING THEN


-- On UPDATE, calculate the difference between old and new salary
salary_difference := :NEW.Salary - :OLD.Salary;
DBMS_OUTPUT.PUT_LINE ('Operation: UPDATE');
DBMS_OUTPUT.PUT_LINE ('Old Salary: ' || :OLD.Salary);
DBMS_OUTPUT.PUT_LINE ('New Salary: ' || :NEW.Salary);
DBMS_OUTPUT.PUT_LINE ('Salary Difference: ' || salary_difference);

ELSIF DELETING THEN


-- On DELETE, show the salary of the deleted record
DBMS_OUTPUT.PUT_LINE ('Operation: DELETE');
DBMS_OUTPUT.PUT_LINE ('Deleted Salary: ' || :OLD.Salary);
END IF;
END;
/

Questions

Let's assume that the tables from the previous labs are created and filled.

1. Create a trigger that displays a message after each operation on the TEACHER table:

• Display 'A new teacher is added' after an INSERT operation.


• Display 'A teacher's record is modified' after an UPDATE operation.
• Display 'A teacher is deleted' after a DELETE operation."

2. Create a trigger that displays the message 'A new course assignment is added to the teacher [Teacher name]' after
each insertion into the COURSE_ASSIGNMENT table. Ensure the teacher's name is retrieved dynamically.

3. Create a trigger that verifies that when a teacher’s salary is modified, the new value can never be lower than the previous
one.

4. Create a trigger that prevents a student from enrolling in the same course multiple times in the ENROLLMENT table. If
a student is already enrolled in the course, raise an error when an insert is attempted.

5. The administrator wants to have the total number of course for each teacher. To do this, the administrator adds an
attribute: TOTAL_COURSES in the teacher table.

a. Add the attribute TOTAL_COURSES in the TEACHER table.


b. Create a TOTAL_COURSES_TRIGGER that updates the TOTAL_COURSES attribute.

You might also like