lab6
lab6
Oracle DBMS
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.
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.
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);
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:
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.