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

Department of Computer Science: // Create Tables To Test The Triggers

Triggers are stored programs that are automatically executed in response to events like data manipulation language (DML) statements or database definition language (DDL) statements. Triggers can be defined on tables, views, schemas, or databases. Examples show triggers that log changes to a table after insert, update, or delete operations, and a trigger that prints salary change details before an update, insert, or delete where the job is not AD_PRES.

Uploaded by

Aftab Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Department of Computer Science: // Create Tables To Test The Triggers

Triggers are stored programs that are automatically executed in response to events like data manipulation language (DML) statements or database definition language (DDL) statements. Triggers can be defined on tables, views, schemas, or databases. Examples show triggers that log changes to a table after insert, update, or delete operations, and a trigger that prints salary change details before an update, insert, or delete where the job is not AD_PRES.

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE

( Rachna College of Engineering and Technology Gujranwala )

DATABASE SYSTEMS

Name: Registration No:

LAB 13

Triggers

Triggers are stored programs, which are automatically executed or fired when some events
occur. Triggers are, in fact, written to be executed in response to any of the following events:

 A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).

 A database definition (DDL) statement (CREATE, ALTER, or DROP).

 A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or


SHUTDOWN).

Triggers could be defined on the table, view, schema, or database with which the event is
associated.

Examples of Triggers:

// Create tables to test the Triggers


CREATE TABLE Test (id integer, name varchar(20));
Create table test_log (id integer, name varchar(20), logdate Date);

// Trigger on Insert, Update or delete


CREATE OR REPLACE TRIGGER test
BEFORE
INSERT OR
UPDATE OR
DELETE
ON test
BEGIN
CASE
WHEN INSERTING THEN
DBMS_OUTPUT.PUT_LINE('Inserting');
WHEN UPDATING('id') THEN
DBMS_OUTPUT.PUT_LINE('Updating ID');
WHEN DELETING THEN
DBMS_OUTPUT.PUT_LINE('Deleting');
END CASE;
END;

// Trigger to be fired after update operation


CREATE OR REPLACE TRIGGER log_test
AFTER UPDATE OF id, Name ON test
FOR EACH ROW
BEGIN
INSERT INTO test_log (id, name, logdate)
VALUES (:NEW.id, :NEW.name, SYSDATE);
END;

// Trigger to be fired after insert


CREATE OR REPLACE TRIGGER log_test
AFTER insert ON test
FOR EACH ROW
BEGIN
INSERT INTO test_log (id, name, logdate)
VALUES (:NEW.id, :NEW.name, SYSDATE);
END;

// Trigger to be fired before update, insert or delete operation


CREATE OR REPLACE TRIGGER print_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON employees
FOR EACH ROW
WHEN (NEW.job_id <> 'AD_PRES')
DECLARE
sal_diff NUMBER;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
DBMS_OUTPUT.PUT(:NEW.last_name || ': ');
DBMS_OUTPUT.PUT('Old salary = ' || :OLD.salary || ', ');
DBMS_OUTPUT.PUT('New salary = ' || :NEW.salary || ', ');
DBMS_OUTPUT.PUT_LINE('Difference: ' || sal_diff);
END;

You might also like