0% found this document useful (0 votes)
5 views1 page

Experiment 09

Explanation: Before Delete: The trigger executes before a row is deleted. FOR EACH ROW: Ensures the trigger fires for every deleted row. Audit Table: The trigger logs details like emp_id, emp_name, action type, and timestamp into the employee_audit table.

Uploaded by

instafill148
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 views1 page

Experiment 09

Explanation: Before Delete: The trigger executes before a row is deleted. FOR EACH ROW: Ensures the trigger fires for every deleted row. Audit Table: The trigger logs details like emp_id, emp_name, action type, and timestamp into the employee_audit table.

Uploaded by

instafill148
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/ 1

Experiment – 09

Create a PL/SQL trigger before/after delete on employee table for each row/statement.

CREATE TABLE employee (


employee_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR2(100),
salary NUMBER
);

CREATE TABLE employee_log (


log_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
employee_id NUMBER,
old_salary NUMBER,
new_salary NUMBER,
changed_by VARCHAR2(50),
change_date DATE
);

CREATE TABLE audit_log (


log_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
table_name VARCHAR2(50),
operation VARCHAR2(20),
updated_by VARCHAR2(50),
update_date DATE
);

CREATE OR REPLACE TRIGGER trg_employee_delete_row


BEFORE DELETE ON employee
FOR EACH ROW
BEGIN
INSERT INTO employee_log (employee_id, old_salary, new_salary, changed_by, change_date)
VALUES (:OLD.employee_id, :OLD.salary, NULL, USER, SYSDATE);
END;
/

CREATE OR REPLACE TRIGGER trg_employee_delete_stmt


AFTER DELETE ON employee
BEGIN
INSERT INTO audit_log (table_name, operation, updated_by, update_date)
VALUES ('EMPLOYEE', 'DELETE', USER, SYSDATE);
END;
/

INSERT INTO employee (name, salary) VALUES ('Farhan', 5000);


INSERT INTO employee (name, salary) VALUES ('Rancho', 6000);
INSERT INTO employee (name, salary) VALUES ('Raju', 7000);

COMMIT;

DELETE FROM employee WHERE employee_id = 1;

SELECT * FROM employee_log;


SELECT * FROM audit_log;
SELECT * FROM employee;

You might also like