0% found this document useful (0 votes)
5 views2 pages

7349 Assignment7

Uploaded by

subham1495s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

7349 Assignment7

Uploaded by

subham1495s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

-- Creating the 'library' database

CREATE DATABASE lib;


USE lib;

-- Creating 'library' table


CREATE TABLE library(
bno INT(5),
bname VARCHAR(40),
author VARCHAR(20),
allowed_days INT(5)
);

-- Creating 'library_audit' table


CREATE TABLE library_audit(
bno INT(5),
old_all_days INT(5),
new_all_days INT(5)
);

-- Inserting values into 'library' table


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


SELECT * FROM library_audit;

-- Sample output for library_audit:


+-----+--------------+--------------+
| bno | old_all_days | new_all_days |
+-----+--------------+--------------+
| 1 | 10 | 15 |
| 2 | 20 | 25 |
| 3 | 18 | 13 |
| 4 | 24 | 19 |
| 5 | 12 | 17 |
+-----+--------------+--------------+

You might also like