0% found this document useful (0 votes)
4 views5 pages

D 9

The document outlines various SQL commands and operations performed on multiple tables, including creating tables, inserting data, and defining triggers. It demonstrates the use of triggers to automatically insert data into another table upon certain actions, such as insertions and updates. Additionally, it showcases how to manage data integrity by filtering records based on specific conditions.
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)
4 views5 pages

D 9

The document outlines various SQL commands and operations performed on multiple tables, including creating tables, inserting data, and defining triggers. It demonstrates the use of triggers to automatically insert data into another table upon certain actions, such as insertions and updates. Additionally, it showcases how to manage data integrity by filtering records based on specific conditions.
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/ 5

‭ ysql> use A2432;‬

m
‭Reading table information for completion of table and column names‬
‭You can turn off this feature to get a quicker startup with -A‬
‭Database changed‬
‭mysql> desc emp;‬
‭+-------+-------------+------+-----+---------+-------+‬
‭| Field | Type | Null | Key | Default | Extra |‬
‭+-------+-------------+------+-----+---------+-------+‬
‭| Id | int | YES | | NULL | |‬
‭| fname | varchar(20) | YES | | NULL | |‬
‭| lname | varchar(20) | YES | | NULL | |‬
‭| sal | varchar(20) | YES | | NULL | |‬
‭+-------+-------------+------+-----+---------+-------+‬
‭4 rows in set (0.00 sec)‬
‭mysql> select * from emp;‬
‭+------+--------+-------+-------+‬
‭| Id | fname | lname | sal |‬
‭+------+--------+-------+-------+‬
‭| 1 | ramesh | kale | 6000 |‬
‭| 2 | mahesh | bhave | 8000 |‬
‭| 2 | manoj | patil | 9000 |‬
‭| 2 | vijay | patil | 10000 |‬
‭+------+--------+-------+-------+‬
‭4 rows in set (0.00 sec)‬
‭mysql> create table emp_insert(fname varchar(20),sal varchar(20));‬
‭Query OK, 0 rows affected (0.04 sec)‬
‭mysql> select * from emp_insert;‬
‭Empty set (0.00 sec)‬
‭mysql> create trigger insert_attempt after insert on emp for each row insert into emp_insert set‬
‭fname=new.fname,sal=new.sal;‬
‭Query OK, 0 rows affected (0.02 sec)‬
‭mysql> insert into emp values(3,'shivam','gupta','23000');‬
‭Query OK, 1 row affected (0.01 sec)‬
‭mysql> insert into emp values(4,'sumeet','gupta','33000');‬
‭Query OK, 1 row affected (0.01 sec)‬
‭mysql> select * from emp;‬
‭+------+--------+-------+-------+‬
‭| Id | fname | lname | sal |‬
‭+------+--------+-------+-------+‬
‭| 1 | ramesh | kale | 6000 |‬
‭| 2 | mahesh | bhave | 8000 |‬
‭| 2 | manoj | patil | 9000 |‬
‭| 2 | vijay | patil | 10000 |‬
‭| 3 | shivam | gupta | 23000 |‬
‭| 4 | sumeet | gupta | 33000 |‬
‭+------+--------+-------+-------+‬
‭6 rows in set (0.00 sec)‬
‭mysql> select * from emp_insert;‬
‭ --------+-------+‬
+
‭| fname | sal |‬
‭+--------+-------+‬
‭| shivam | 23000 |‬
‭| sumeet | 33000 |‬
‭+--------+-------+‬
‭2 rows in set (0.00 sec)‬
‭mysql>‬
‭mysql> create table student1(Sid int(10),Sname varchar(20),marks int(10));‬
‭Query OK, 0 rows affected, 2 warnings (0.04 sec)‬
‭mysql> create table student2(Sname varchar(20),marks int(10));‬
‭Query OK, 0 rows affected, 1 warning (0.04 sec)‬
‭mysql> insert into student1 values(1,'shivam',55);‬
‭Query OK, 1 row affected (0.01 sec)‬
‭mysql> insert into student2 values('shivam',60);‬
‭Query OK, 1 row affected (0.00 sec)‬
‭mysql> select * from student1;‬
‭+------+--------+-------+‬
‭| Sid | Sname | marks |‬
‭+------+--------+-------+‬
‭| 1 | shivam | 55 |‬
‭+------+--------+-------+‬
‭1 row in set (0.00 sec)‬
‭mysql> select * from student2;‬
‭+--------+-------+‬
‭| Sname | marks |‬
‭+--------+-------+‬
‭| shivam | 60 |‬
‭+--------+-------+‬
‭1 row in set (0.00 sec)‬
‭mysql> delimiter //‬
‭mysql> create trigger stud_insert after insert on student1 for each row begin insert into student2 set‬
‭Sname=new.Sname,marks=new.marks;‬
‭-> delete from student2 where marks <60;‬
‭-> end; //‬
‭Query OK, 0 rows affected (0.01 sec)‬
‭mysql> delimiter ;‬
‭mysql> insert into student1 values(2,'nikhil',45);‬
‭Query OK, 1 row affected (0.01 sec)‬
‭mysql> insert into student1 values(3,'sumeet',85);‬
‭Query OK, 1 row affected (0.01 sec)‬
‭mysql> select * from student1;‬
‭+------+--------+-------+‬
‭| Sid | Sname | marks |‬
‭+------+--------+-------+‬
‭| 1 | shivam | 55 |‬
‭| 2 | nikhil | 45 |‬
‭| 3 | sumeet | 85 |‬
‭ ------+--------+-------+‬
+
‭3 rows in set (0.00 sec)‬
‭mysql> select * from student2;‬
‭+--------+-------+‬
‭| Sname | marks |‬
‭+--------+-------+‬
‭| shivam | 60 |‬
‭| sumeet | 85 |‬
‭+--------+-------+‬
‭2 rows in set (0.00 sec)‬

‭ ysql> create table person(Id int(10),Pname varchar(20),sal int(20));‬


m
‭Query OK, 0 rows affected, 2 warnings (0.04 sec)‬
‭mysql> create table person1(Pname varchar(20),sal int(20));‬
‭Query OK, 0 rows affected, 1 warning (0.04 sec)‬
‭mysql> insert into person values(1,'pqr',200);‬
‭Query OK, 1 row affected (0.01 sec)‬
‭mysql> insert into person values(2,'abc',2000);‬
‭Query OK, 1 row affected (0.01 sec)‬
‭mysql> insert into person1 values('adc',0);‬
‭Query OK, 1 row affected (0.01 sec)‬
‭mysql> select * from person;‬
‭+------+-------+------+‬
‭| Id | Pname | sal |‬
‭+------+-------+------+‬
‭| 1 | pqr | 200 |‬
‭| 2 | abc | 2000 |‬
‭+------+-------+------+‬
‭2 rows in set (0.00 sec)‬
‭mysql> select * from person1;‬
‭+-------+------+‬
‭| Pname | sal |‬
‭+-------+------+‬
‭| adc | 0 |‬
‭+-------+------+‬
‭1 row in set (0.00 sec)‬
‭mysql> delimiter //‬
‭mysql> create trigger t2 after update on person for each row begin insert into person1 set‬
‭Pname=new.Pname,sal=new.sal;‬
‭-> delete from person1 where sal <5000;‬
‭-> end; //‬
‭Query OK, 0 rows affected (0.07 sec)‬
‭mysql> delimiter ;‬
‭mysql> insert into person values(3,'thc',9000);‬
‭Query OK, 1 row affected (0.01 sec)‬
‭mysql> select * from person;‬
‭+------+-------+------+‬
‭| Id | Pname | sal |‬
‭ ------+-------+------+‬
+
‭| 1 | pqr | 200 |‬
‭| 2 | abc | 2000 |‬
‭| 3 | thc | 9000 |‬
‭+------+-------+------+‬
‭3 rows in set (0.01 sec)‬
‭mysql> select * from person1;‬
‭+-------+------+‬
‭| Pname | sal |‬
‭+-------+------+‬
‭| adc | 0 |‬
‭+-------+------+‬
‭1 row in set (0.00 sec)‬
‭mysql> update person set Pname='sumeet' where Pname='thc';‬
‭Query OK, 1 row affected (0.02 sec)‬
‭Rows matched: 1 Changed: 1 Warnings: 0‬
‭mysql> select * from person;‬
‭+------+--------+------+‬
‭| Id | Pname | sal |‬
‭+------+--------+------+‬
‭| 1 | pqr | 200 |‬
‭| 2 | abc | 2000 |‬
‭| 3 | sumeet | 9000 |‬
‭+------+--------+------+‬
‭3 rows in set (0.00 sec)‬
‭mysql> select * from person1;‬
‭+--------+------+‬
‭| Pname | sal |‬
‭+--------+------+‬
‭| sumeet | 9000 |‬
‭+--------+------+‬
‭1 row in set (0.00 sec)‬
‭mysql> create table sample(id int(10),age int(10),adult int(10));‬
‭Query OK, 0 rows affected, 3 warnings (0.04 sec)‬

‭ ysql> delimiter //‬


m
‭mysql> create trigger adult_check before insert on sample for each row begin if new.age>18 then set‬
‭new.adult=1;else set new.adult=0; end if ;‬
‭-> end; //‬
‭Query OK, 0 rows affected (0.02 sec)‬
‭mysql> delimiter ;‬
‭mysql> insert into sample(id,age) values(2,22);‬
‭Query OK, 1 row affected (0.01 sec)‬
‭mysql> insert into sample(id,age) values(1,12);‬
‭Query OK, 1 row affected (0.01 sec)‬
‭mysql> select * from sample;‬
‭ ------+------+-------+‬
+
‭| id | age | adult |‬
‭+------+------+-------+‬
‭| 2 | 22 | 1 |‬
‭| 1 | 12 | 0 |‬
‭+------+------+-------+‬
‭2 rows in set (0.00 sec)‬
‭mysql>‬

You might also like