From f854578401e0fd6339b122e3bd6f2449f82ee3f0 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Mon, 28 Oct 2024 13:41:22 +0530 Subject: [PATCH] Moved Queries-A7 to Queries-A8 since A8 is the library/trigger one. Again, thanks to Shivani for pointing it out. --- Practical/Assignment-A8/Queries-A8.md | 93 +++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Practical/Assignment-A8/Queries-A8.md diff --git a/Practical/Assignment-A8/Queries-A8.md b/Practical/Assignment-A8/Queries-A8.md new file mode 100644 index 0000000..157426a --- /dev/null +++ b/Practical/Assignment-A8/Queries-A8.md @@ -0,0 +1,93 @@ +# Database Queries for Assignment-A8 + +## Creating tables + +`Library` table: +```sql +CREATE TABLE Library( + id NUMBER(12), + title VARCHAR(255), + dateofissue DATE, + author VARCHAR(255) +); + +``` + +`Library_Audit` table: +```sql +CREATE TABLE Library_Audit( + id NUMBER(12), + title VARCHAR(255), + dateofaction DATE, + author VARCHAR(255), + status VARCHAR(255) +); + +``` + +## Inserting values + +`Library` table: +```sql +INSERT INTO Library VALUES (1, 'Berserk', TO_DATE('2024-07-28','YYYY-MM-DD'), 'Prashant'); +INSERT INTO Library VALUES (2, 'Dark', TO_DATE('2024-07-15','YYYY-MM-DD'), 'Rajendra'); +INSERT INTO Library VALUES (3, 'Hannibal', TO_DATE('2024-07-20','YYYY-MM-DD'), 'Manoj'); +INSERT INTO Library VALUES (4, 'AOT', TO_DATE('2024-07-30','YYYY-MM-DD'), 'Rajesh'); +INSERT INTO Library VALUES (5, 'GOT', TO_DATE('2024-07-19','YYYY-MM-DD'), 'Anil'); + +``` + +## Trigger + +```sql +CREATE OR REPLACE TRIGGER library_action +AFTER INSERT OR UPDATE OR DELETE ON Library +FOR EACH ROW +BEGIN + IF INSERTING THEN + INSERT INTO Library_Audit (id, title, dateofaction, author, status) VALUES (:NEW.id, :NEW.title, current_timestamp, :NEW.author, 'Insert'); + ELSIF UPDATING THEN + INSERT INTO Library_Audit (id, title, dateofaction, author, status) VALUES (:OLD.id, :OLD.title, current_timestamp, :OLD.author, 'Update'); + ELSIF DELETING THEN + INSERT INTO Library_Audit (id, title, dateofaction, author, status) VALUES (:OLD.id, :OLD.title, current_timestamp, :OLD.author, 'Delete'); + END IF; +END; +/ + +``` + +## Testing trigger + +### Insert operation + +```sql +INSERT INTO Library VALUES (15, 'CREW', TO_DATE('2024-07-22','YYYY-MM-DD'), 'Ramesh'); +INSERT INTO Library VALUES (14, 'Ninteen Eighty Four', TO_DATE('2024-07-01','YYYY-MM-DD'), 'Omkar'); + +SELECT * FROM Library; +SELECT * FROM Library_Audit; + +``` +### Update operation + +```sql +UPDATE Library SET id = 6, title = 'Sherlock', author = 'Deepak' where id = 3; +UPDATE Library SET id = 7, title = 'MR. ROBOT', author = 'Varad' where id = 4; + +SELECT * FROM Library; +SELECT * FROM Library_Audit; + +``` + +### Delete operation + +```sql +DELETE FROM Library WHERE id = 1; +DELETE FROM Library WHERE id = 5; + +SELECT * FROM Library; +SELECT * FROM Library_Audit; + +``` + +---