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

DBMS Practical 7

Uploaded by

SUMEDH PATIL
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)
31 views

DBMS Practical 7

Uploaded by

SUMEDH PATIL
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/ 8

Name : SUMEDH NARAYAN PATIL

Roll No : SI-38
Subject : DBMS

ASSIGNMENT N0 07

Study and Implementation of database MYSQL Triggers

mysql> delimiter //
mysql> show databases //
+ +
| Database |
+ +
| college |
| information_schema |
| mysql |
| performance_schema |
| sys |
+ +
5 rows in set (0.00 sec)
mysql> use college //
Database changed
mysql> create table company(emp_id int,emp_name varchar(10),dept
varchar(10),address varchar(10));//
Query OK, 0 rows affected (0.05 sec)
mysql> create table emp(emp_id int,emp_name varchar(10),dept
varchar(10),address varchar(10));//
Query OK, 0 rows affected (0.03 sec)
mysql> insert into company values(1,'Sanket','it','Anagar');

//Query OK, 1 row affected (0.03 sec)


mysql> insert into company values(1,'Vinit','cs','Pune')
-> ;//
Query OK, 1 row affected (0.01 sec)
mysql> create trigger emp_info
-> after insert on company
-> for each row
-> begin
-> insert into emp values(new.emp_id,new.emp_name,new.dept,new.address);
-> end;//
Query OK, 0 rows affected (0.02 sec)
mysql> select*from emp;//
Empty set (0.01 sec)
mysql> insert into company values(3,'Savim','entc','pune');//
Query OK, 1 row affected (0.01 sec)
mysql> insert into company values(4,'Yash','entc','Mumbai');//
Query OK, 1 row affected (0.01 sec)
mysql> select*from emp;//
+ + + + +
| emp_id | emp_name | dept | address |
+ + + + +
| 3 | Savim | entc | pune |
| 4 | Yash | entc | Mumbai |
+ + + + +
2 rows in set (0.00 sec)
mysql> select*from company;//
+ + + + +
| emp_id | emp_name | dept | address |
+ + + + +
| 1 | Sanket | it | Anagar |
| 1 | Vinit | cs | Pune |
| 3 | Savim | entc | pune |
| 4 | Yash | entc | Mumbai |
+ + + + +
4 rows in set (0.00 sec)
mysql> create table t1(a int,b char(10));//
Query OK, 0 rows affected (0.02 sec)
mysql> create table t2(c char(10),b int);//
Query OK, 0 rows affected (0.04 sec)
insert into t2 values(new.b,new.a)' at line 1
mysql> create trigger trig
-> after insert on t1
-> for each row
-> begin
-> insert into t2 values(new.b,new.a);
-> end;
-> //
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t1 values(69,'Prasad'),
-> (76,'Vignesh');//
Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select*from t2;//
+ + +
|c |b |
+ + +
| Prasad | 69 |
| Vignesh |76 |
+ + +
2 rows in set (0.00 sec)
mysql> drop trigger trig;//
Query OK, 0 rows affected (0.02 sec)
mysql> show triggers;//
+ + + +
+ +
+
+ + +
+ +
| Trigger | Event | Table | Statement
| Timing | Created | sql_mode
| Definer | character_set_client | collation_connection |
Database Collation |
+ + + +
+ + +
+
+ + + +
| emp_info | INSERT | company | begin
insert into emp values(new.emp_id,new.emp_name,new.dept,new.address);
end | AFTER | 2024-04-14 21:23:34.77 |
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DAT
E,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SU
BSTITUTION | root@localhost | cp850 | cp850_general_ci |
utf8mb4_0900_ai_ci |
| trig1 | INSERT | t4 | BEGIN
INSERT INTO t5 SET c = NEW.b,d = NEW.a;
END | AFTER | 2024-04-12 23:03:03.69 |
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DAT
E,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SU
BSTITUTION | root@localhost | cp850 | cp850_general_ci |
utf8mb4_0900_ai_ci |
| trig2 | INSERT | t6 | BEGIN
INSERT INTO t7 SET c = NEW.b,d = NEW.a;
END | AFTER | 2024-04-14 00:22:09.84 |
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DAT
E,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SU
BSTITUTION | root@localhost | cp850 | cp850_general_ci |
utf8mb4_0900_ai_ci |
+ + + +
+ + +
+
+ + + +
3 rows in set (0.03 sec)
mysql> create trigger emp_info2
-> before update on company
-> for each row
-> begin
-> insert into emp values(old.emp_id,old.emp_name,old.dept,old.address);
-> end;//
Query OK, 0 rows affected (0.02 sec)
mysql> truncate table emp;//
Query OK, 0 rows affected (0.07 sec)
mysql> update company set
emp_id=5,emp_name='Prasad',dept='civil',address='kokan' where emp_id=3;//
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select*from emp;//

+ + + + +
| emp_id | emp_name | dept | address |
+ + + + +
| 3 | Prasad | entc | pune |
+ + + + +
1 row in set (0.00 sec)
mysql> select*from company;//
+ + + + +
| emp_id | emp_name | dept | address |
+ + + + +
| 1 | Sanket | it | Anagar |
| 1 | Vinit | cs | Pune |
| 5 | Savim | civil | pune |
| 4 | Yash | entc | Mumbai |
+ + + + +
4 rows in set (0.00 sec)
mysql> create trigger emp_info3
-> after update on company
-> for each row
-> begin
-> insert into emp values(new.emp_id,emp_name,emp_dept,new.address);
-> end;//
Query OK, 0 rows affected (0.01 sec)
mysql> create trigger emp_info4
-> after update on company
-> for each row
-> begin
-> insert into emp values(new.emp_id,emp_name,new.dept,new.address);
-> end;//
Query OK, 0 rows affected (0.02 sec)
mysql> truncate table emp;//
Query OK, 0 rows affected (0.06 sec)
mysql> truncate table emp;//
Query OK, 0 rows affected (0.06 sec)
mysql> update company set
emp_id=3,emp_name='abc',dept='entc',address='mumbai' where emp_id=4;//
ERROR 1054 (42S22): Unknown column 'emp_dept' in 'field list'
mysql> select *from emp;//
Empty set (0.00 sec)
mysql> select*from company;//
+ + + + +
| emp_id | emp_name | dept | address |
+ + + + +
| 1 | Sanket | it | Anagar |
| 1 | Vinit | cs | Pune |
| 5 | Savim | civil | pune |
| 4 | Yash | entc | Mumbai |
+ + + + +
4 rows in set (0.00 sec)

mysql> create trigger emp_info5


-> after delete on company
-> for each row
-> begin
-> insert into emp values(old.emp_id,old.emp_name,old.dept,old.address);
-> end;//
Query OK, 0 rows affected (0.01 sec)
mysql> truncate table emp;//
Query OK, 0 rows affected (0.05 sec)
mysql> delete from company where emp_id=5;//
Query OK, 1 row affected (0.02 sec)
mysql> select*from emp;///
+ + + + +
| emp_id | emp_name | dept | address |
+ + + + +
| 5 | Savim | civil | pune |
+ + + + +
1 row in set (0.00 sec)

mysql> select*from company;//


+ + + + +
| emp_id | emp_name | dept | address |
+ + + + +
| 1 | Sanket | it | Anagr|
| 1 | Vinit | cs | Pune |
| 4 | Yash | entc | Mumbai |
+ + + + +
3 rows in set (0.00 sec)

You might also like