From cd04378e9567c3f5de4be6d2788bcc4889406267 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Mon, 28 Oct 2024 13:40:40 +0530 Subject: [PATCH] Moved old Queries-A6 to Queries-A7 since it's the cursor assignment. --- Practical/Assignment-A7/Queries-A7.md | 133 +++++++++++++++----------- 1 file changed, 76 insertions(+), 57 deletions(-) diff --git a/Practical/Assignment-A7/Queries-A7.md b/Practical/Assignment-A7/Queries-A7.md index d7d8260..e1eae05 100644 --- a/Practical/Assignment-A7/Queries-A7.md +++ b/Practical/Assignment-A7/Queries-A7.md @@ -1,91 +1,110 @@ +# Database Queries for Assignment-A7 + ## Creating tables -`Library` table: +`O_RollCall` table: ```sql -CREATE TABLE Library( - id NUMBER(12), - title VARCHAR(255), - dateofissue DATE, - author VARCHAR(255) +CREATE TABLE O_RollCall( +name VARCHAR(255), +roll NUMBER(14), +class VARCHAR(255) ); ``` -`Library_Audit` table: +`N_RollCall` table: ```sql -CREATE TABLE Library_Audit( - id NUMBER(12), - title VARCHAR(255), - dateofaction DATE, - author VARCHAR(255), - status VARCHAR(255) +CREATE TABLE N_RollCall( +name VARCHAR(255), +roll NUMBER(14), +class VARCHAR(255) ); ``` -## Inserting values +## Inserting data -`Library` table: +`O_RollCall` 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'); +INSERT INTO O_RollCall VALUES ('Stewie', 1, 'Comp 1'); +INSERT INTO O_RollCall VALUES ('Edie', 2, 'Comp 2'); +INSERT INTO O_RollCall VALUES ('Stomp', 3, 'Comp 3'); +INSERT INTO O_RollCall VALUES ('Lara', 4, 'Comp 1'); +INSERT INTO O_RollCall VALUES ('Foxy', 5, 'Comp 2'); ``` -## Trigger - +`N_RollCall` table: ```sql -CREATE OR REPLACE TRIGGER library_action -AFTER INSERT OR UPDATE OR DELETE ON Library -FOR EACH ROW +INSERT INTO N_RollCall VALUES ('Stewie', 1, 'Comp 1'); +INSERT INTO N_RollCall VALUES ('Edie', 2, 'Comp 2'); +INSERT INTO N_RollCall VALUES ('Stomp', 3, 'Comp 3'); +INSERT INTO N_RollCall VALUES ('Lara', 4, 'Comp 1'); +INSERT INTO N_RollCall VALUES ('Foxy', 5, 'Comp 2'); +INSERT INTO N_RollCall VALUES ('Gundeti', 6, 'Comp 3'); +INSERT INTO N_RollCall VALUES ('Kalas', 7, 'Comp 2'); + +``` + +## Procedure + +### Explicit cursor +```sql +DECLARE + p_name VARCHAR(255); + p_rollno NUMBER(15); + p_class VARCHAR(255); + CURSOR cc1 IS SELECT * FROM N_RollCall WHERE roll NOT IN (SELECT roll FROM O_RollCall); 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; + OPEN cc1; + LOOP + FETCH cc1 INTO p_name, p_rollno, p_class; + INSERT INTO O_RollCall VALUES (p_name, p_rollno, p_class); + EXIT WHEN cc1%notfound; + DBMS_OUTPUT.PUT_LINE(p_name || ' ' || p_rollno || ' ' || p_class); + END LOOP; + CLOSE cc1; END; / ``` -## Testing trigger - -### Insert operation - +### Parameterized cursor ```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'); +DECLARE + p_name VARCHAR(255); + p_rollno NUMBER(15); + p_class VARCHAR(255); + CURSOR pp1(roll1 NUMBER) IS SELECT * FROM N_RollCall WHERE roll > roll1; -SELECT * FROM Library; -SELECT * FROM Library_Audit; +BEGIN + OPEN pp1(3); + LOOP + FETCH pp1 INTO p_name, p_rollno, p_class; + EXIT WHEN pp1%notfound; + DBMS_OUTPUT.PUT_LINE(p_name || ' ' || p_rollno || ' ' || p_class); + END LOOP; + CLOSE pp1; -``` -### 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; +END; +/ ``` -### Delete operation - +### Implicit Cursor ```sql -DELETE FROM Library WHERE id = 1; -DELETE FROM Library WHERE id = 5; +DECLARE + total_rows NUMBER(2); -SELECT * FROM Library; -SELECT * FROM Library_Audit; +BEGIN + UPDATE N_RollCall SET roll = roll + 1; + IF sql%notfound THEN + dbms_output.put_line('no roll was updated'); + ELSIF sql%found THEN + total_rows := sql%rowcount; + DBMS_OUTPUT.PUT_LINE( total_rows || ' roll calls were affected '); + END IF; +END; +/ ``` - ----