INSERT INTO library VALUES(1,'Database Systems','Connally T',10); INSERT INTO library VALUES(2,'System Programming','John Donovan',20); INSERT INTO library VALUES(3,'Computer Network & Internet','Douglas E. Comer',18); INSERT INTO library VALUES(4,'Agile Project Management','Ken Schwaber',24); INSERT INTO library VALUES(5,'Python for Data Analysis','Wes McKinney',12);
-- Selecting values from 'library' table
SELECT * FROM library;
-- Sample output for library:
+-----+---------------------------+-------------------+--------------+ | bno | bname | author | allowed_days | +-----+---------------------------+-------------------+--------------+ | 1 | Database Systems | Connally T | 10 | | 2 | System Programming | John Donovan | 20 | | 3 | Computer Network & Internet| Douglas E. Comer | 18 | | 4 | Agile Project Management | Ken Schwaber | 24 | | 5 | Python for Data Analysis | Wes McKinney | 12 | +-----+---------------------------+-------------------+--------------+
-- Creating trigger 'tr1' for auditing before update on 'library'
DELIMITER // CREATE TRIGGER tr1 BEFORE UPDATE ON library FOR EACH ROW BEGIN INSERT INTO library_audit VALUES(NEW.bno, OLD.allowed_days, NEW.allowed_days); END // DELIMITER ;
-- Updating 'library' records and triggering the audit
UPDATE library SET allowed_days=15 WHERE bno=1; UPDATE library SET allowed_days=25 WHERE bno=2; UPDATE library SET allowed_days=13 WHERE bno=3; UPDATE library SET allowed_days=19 WHERE bno=4; UPDATE library SET allowed_days=17 WHERE bno=5;
-- Selecting updated values from 'library' table
SELECT * FROM library;
-- Sample output for updated library:
+-----+---------------------------+-------------------+--------------+ | bno | bname | author | allowed_days | +-----+---------------------------+-------------------+--------------+ | 1 | Database Systems | Connally T | 15 | | 2 | System Programming | John Donovan | 25 | | 3 | Computer Network & Internet| Douglas E. Comer | 13 | | 4 | Agile Project Management | Ken Schwaber | 19 | | 5 | Python for Data Analysis | Wes McKinney | 17 | +-----+---------------------------+-------------------+--------------+
-- Selecting values from 'library_audit' table after updates