0% found this document useful (0 votes)
7 views

Practical No 07

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)
7 views

Practical No 07

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/ 4

Practical No.

07
Name : Renuka Jadhav Roll No. : 49
Div : C

Aim : Write a database trigger on Library table. The System should keep track of the records
that are being updated or deleted. The old value of updated or deleted records should be added
in Library_Audit table.

mysql> show databases;


+--------------------+
| Database |

+--------------------+
| college |
| information_schema |
| library |
| mysql |

| performance_schema |
| practicle3 |
| renuka |
| sbibank |
| sys |

| user_auth |
+--------------------+
10 rows in set (0.06 sec)

mysql> create database renuka1;

Query OK, 1 row affected (0.02 sec)


mysql> use renuka1;
Database changed
mysql> create table libdata(id int primary key,book_name varchar(30),doi date,dor date,price
int);
Query OK, 0 rows affected (0.07 sec)

mysql> insert into libdata values(1,"the alchemist","2024-3-5","2024-4-


1",200),(2,"yayati","2024-4-3","2024-5-4",300);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from libdata;


+----+---------------+------------+------------+-------+
| id | book_name | doi | dor | price |
+----+---------------+------------+------------+-------+
| 1 | the alchemist | 2024-03-05 | 2024-04-01 | 200 |

| 2 | yayati | 2024-04-03 | 2024-05-04 | 300 |


+----+---------------+------------+------------+-------+
2 rows in set (0.00 sec)

mysql> create table libaudit(id int primary key,book_name varchar(30),doi date,dor date,price
int);
Query OK, 0 rows affected (0.03 sec)

mysql> select * from libaudit;


Empty set (0.00 sec)

mysql> DELIMITER //
mysql> CREATE TRIGGER demo
-> BEFORE UPDATE
-> ON libdata

-> FOR EACH ROW


-> BEGIN
-> INSERT INTO libaudit(id, book_name, doi, dor, price)
-> VALUES (OLD.id, OLD.book_name, OLD.doi, OLD.dor, OLD.price);
-> END

-> //
Query OK, 0 rows affected (0.02 sec)

mysql> update libdata set price=110 where id=1;


-> //

Query OK, 1 row affected (0.02 sec)


Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from libaudit;


-> //

+----+---------------+------------+------------+-------+
| id | book_name | doi | dor | price |
+----+---------------+------------+------------+-------+
| 1 | the alchemist | 2024-03-05 | 2024-04-01 | 200 |
+----+---------------+------------+------------+-------+

1 row in set (0.00 sec)

mysql> select * from libdata;


-> //
+----+---------------+------------+------------+-------+
| id | book_name | doi | dor | price |
+----+---------------+------------+------------+-------+

| 1 | the alchemist | 2024-03-05 | 2024-04-01 | 110 |


| 2 | yayati | 2024-04-03 | 2024-05-04 | 300 |
+----+---------------+------------+------------+-------+
2 rows in set (0.00 sec)

You might also like