0% found this document useful (0 votes)
52 views131 pages

Dbms Record

Uploaded by

vigneshwar3799
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)
52 views131 pages

Dbms Record

Uploaded by

vigneshwar3799
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/ 131

EX.

NO:
AIRLINES RESERVATION DATABASE MANAGEMENT SYSTEM
DATE:

AIM:

CREATION OF DATABASE:

CREATE DATABASE airlines;


USE airlines;
CREATE TABLE airplane (
ID int (10),
type varchar(10) NOT NULL,
company varchar(20) NOT NULL,
PRIMARY KEY (ID));
mysql> desc airplane;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| ID | int(10) | NO | PRI | NULL | |
| type | varchar(10) | NO | | NULL | |
| company | varchar(20) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
INSERT INTO airplane VALUES
('1001','B738','Emirates'),
('1002','A320','Airbus');
mysql> select * from airplane;
+------+------+----------+
| ID | type | company |
+------+------+----------+
| 1001 | B738 | Emirates |
| 1002 | A320 | Airbus |
+------+------+----------+
CREATE TABLE airport (
code varchar(10),
name varchar(50) NOT NULL,
city varchar(20) NOT NULL,
state varchar(20) NOT NULL,
country varchar(20) NOT NULL,
PRIMARY KEY (`code`));
mysql> desc airport;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| code | varchar(10) | NO | PRI | NULL | |
| name | varchar(50) | NO | | NULL | |
| city | varchar(20) | NO | | NULL | |
| state | varchar(20) | NO | | NULL | |
| country | varchar(20) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
INSERT INTO airport VALUES
('CIA','Chennai International Airport','Chennai','Tamilnadu','india'),
('MIA','Madurai International Airport','Madurai','Tamilnadu','india'),
('SA','Salem Airport','Salem','Tamilnadu','india'),
('TIA','Trichi International Airport','Trichi','Tamilnadu','india');
mysql> select * from airport;
+------+-------------------------------+---------+-----------+---------+
| code | name | city | state | country |
+------+-------------------------------+---------+-----------+---------+
| CIA | Chennai International Airport | Chennai | Tamilnadu | india |
| MIA | Madurai International Airport | Madurai | Tamilnadu | india |
| SA | Salem Airport | Salem | Tamilnadu | india |
| TIA | Trichi International Airport | Trichi | Tamilnadu | india |
+------+-------------------------------+---------+-----------+---------+
CREATE TABLE flight (
f_number varchar(20),
airplane_id int(10) NOT NULL,
departure varchar(10) NOT NULL,
d_time time NOT NULL,
arrival varchar(10) NOT NULL,
a_time time NOT NULL,
PRIMARY KEY (f_number),
FOREIGN KEY (airplane_id) REFERENCES airplane(ID),
FOREIGN KEY (arrival) REFERENCES airport(code),
FOREIGN KEY (departure) REFERENCES airport(code));
mysql> desc flight;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| f_number | varchar(20) | NO | PRI | NULL | |
| airplane_id | int(10) | NO | MUL | NULL | |
| departure | varchar(10) | NO | MUL | NULL | |
| d_time | time | NO | | NULL | |
| arrival | varchar(10) | NO | MUL | NULL | |
| a_time | time | NO | | NULL | |
+-------------+-------------+------+-----+---------+-------+
INSERT INTO flight VALUES
('AA100','1001','CIA','18:35:00','SA','21:00:00'),
('AA110','1001','SA','14:35:00','TIA','17:30:00'),
('AA200','1002','TIA','13:40:00','CIA','19:30:00'),
('AA210','1002','MIA','07:35:00','SA','10:30:00'),
('AA300','1001','TIA','19:30:00','CIA','22:00:00'),
('AA310','1002','CIA','17:00:00','MIA','21:00:00'),
('AA400','1002','SA','20:00:00','TIA','23:00:00'),
('AA410','1001','TIA','11:00:00','CIA','13:00:00');
mysql> select * from flight;
+----------+-------------+-----------+----------+---------+----------+
| f_number | airplane_id | departure | d_time | arrival | a_time |
+----------+-------------+-----------+----------+---------+----------+
| AA100 | 1001 | CIA | 18:35:00 | SA | 21:00:00 |
| AA110 | 1001 | SA | 14:35:00 | TIA | 17:30:00 |
| AA200 | 1002 | TIA | 13:40:00 | CIA | 19:30:00 |
| AA210 | 1002 | MIA | 07:35:00 | SA | 10:30:00 |
| AA300 | 1001 | TIA | 19:30:00 | CIA | 22:00:00 |
| AA310 | 1002 | CIA | 17:00:00 | MIA | 21:00:00 |
| AA400 | 1002 | SA | 20:00:00 | TIA | 23:00:00 |
| AA410 | 1001 | TIA | 11:00:00 | CIA | 13:00:00 |
+----------+-------------+-----------+----------+---------+----------+
CREATE TABLE class (
f_number varchar(10),
name varchar(20),
capacity int(11) NOT NULL,
price float NOT NULL,
PRIMARY KEY (`f_number`,`name`),
FOREIGN KEY (`f_number`) REFERENCES flight(`f_number`));
mysql> desc class;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| f_number | varchar(10) | NO | PRI | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
| capacity | int(11) | NO | | NULL | |
| price | float | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+

INSERT INTO class VALUES


('AA100','Business',5,500),
('AA110','Economy',200,180),
('AA200','Business',15,4000),
('AA210','Economy',100,1000),
('AA300','Business',1,200),
('AA310','Economy',100,100),
('AA400','Business',15,800),
('AA410','Economy',100,240);
mysql> select * from class;
+----------+----------+----------+-------+
| f_number | name | capacity | price |
+----------+----------+----------+-------+
| AA100 | Business | 5 | 500 |
| AA110 | Economy | 200 | 180 |
| AA200 | Business | 15 | 4000 |
| AA210 | Economy | 100 | 1000 |
| AA300 | Business | 1 | 200 |
| AA310 | Economy | 100 | 100 |
| AA400 | Business | 15 | 800 |
| AA410 | Economy | 100 | 240 |
+----------+----------+----------+-------+
CREATE TABLE passanger (
username varchar(30),
email varchar(45) NOT NULL,
phoneno varchar(15) DEFAULT NULL,
gender varchar(10) DEFAULT NULL,
age INT DEFAULT NULL,
password varchar(45) NOT NULL,
PRIMARY KEY (`username`));
mysql> desc passanger;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(30) | NO | PRI | NULL | |
| email | varchar(45) | NO | | NULL | |
| phoneno | varchar(15) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| password | varchar(45) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
mysql>INSERT INTO passanger VALUES
('akash','[email protected]','9845637456','Male','23','abcdef123456'),
('bala','[email protected]','9645837452','Female','43','bala@321'),
('gopal','[email protected]','9347634453','Male','34','gopal$$123'),
('rahul','[email protected]','987654321','Male','26','rahul%$#'),
('mathi','[email protected]','9948937456' ,'Female','21','mathi@mathi');
mysql> select * from passanger;
+----------+-------------------+------------+--------+------+--------------+
| username | email | phoneno | gender | age | password |
+----------+-------------------+------------+--------+------+--------------+
| akash | [email protected] | 9845637456 | Male | 23 | abcdef123456 |
| bala | [email protected] | 9645837452 | Female | 43 | bala@321 |
| gopal | [email protected] | 9347634453 | Male | 34 | gopal$$123 |
| mathi | [email protected] | 9948937456 | Female | 21 | mathi@mathi |
| rahul | [email protected] | 987654321 | Male | 26 | rahul%$# |
+----------+-------------------+------------+--------+------+--------------+
CREATE TABLE book (
ID int(11) NOT NULL AUTO_INCREMENT,
time datetime NOT NULL,
date date NOT NULL,
flightno varchar(10) NOT NULL,
username varchar(45) NOT NULL,
classtype varchar(20) NOT NULL,
paid int(1) DEFAULT '0',
PRIMARY KEY (`ID`,`flightno`),
FOREIGN KEY (`flightno`, `classtype`) REFERENCES class(`f_number`, `name`),
FOREIGN KEY (`username`) REFERENCES passanger(`username`));
mysql> desc book;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| time | datetime | NO | | NULL | |
| date | date | NO | | NULL | |
| flightno | varchar(10) | NO | PRI | NULL | |
| username | varchar(45) | NO | MUL | NULL | |
| classtype | varchar(20) | NO | | NULL | |
| paid | int(1) | YES | | 0 | |
+-----------+-------------+------+-----+---------+----------------+
INSERT INTO book VALUES
(1,'2019-12-01 17:22:00','2019-12-01','AA110','akash','Economy',0),
(2,'2019-12-01 17:23:00','2019-12-01','AA210','gopal','Economy',1),
(3,'2019-12-01 18:24:00','2019-12-02','AA310','rahul','Economy',0),
(4,'2019-12-01 18:25:00','2019-12-01','AA200','bala','Business',1),
(5,'2019-12-01 19:26:00','2019-12-01','AA100','mathi','Business',1);
mysql> select * from book;
+----+---------------------+------------+----------+----------+-----------+------+
| ID | time | date | flightno | username | classtype | paid |
+----+---------------------+------------+----------+----------+-----------+------+
| 1 | 2019-12-01 17:22:00 | 2019-12-01 | AA110 | akash | Economy | 0 |
| 2 | 2019-12-01 17:23:00 | 2019-12-01 | AA210 | gopal | Economy | 1 |
| 3 | 2019-12-01 18:24:00 | 2019-12-02 | AA310 | rahul | Economy | 0 |
| 4 | 2019-12-01 18:25:00 | 2019-12-01 | AA200 | bala | Business | 1 |
| 5 | 2019-12-01 19:26:00 | 2019-12-01 | AA100 | mathi | Business | 1 |
+----+---------------------+------------+----------+----------+-----------+------+
mysql> show tables;
+---------------+
| Tables_in_air |
+---------------+
| airplane |
| airport |
| book |
| class |
| class_flight |
| flight | |
+---------------+
VIEW:
mysql> create view class_flight
as
select class.f_number,arrival,departure,name
from class
left outer join flight on flight.f_number=class.f_number
where class.name='Economy';
mysql> select * from class_flight;
+----------+---------+-----------+---------+
| f_number | arrival | departure | name |
+----------+---------+-----------+---------+
| AA110 | TIA | SA | Economy |
| AA210 | SA | MIA | Economy |
| AA310 | MIA | CIA | Economy |
| AA410 | CIA | TIA | Economy |
+----------+---------+-----------+---------+

INDEX:
mysql> show index from flight;

Non Seg Coloumn Sub Index


TABLE unique Key name in name Collection Cordinality part Packed Null type comment
index
flight 0 PRIMARY 1 f_number A 8 NULL NULL B
flight 1 airplane_id 1 airplane_id A 4 NULL NULL TREE
flight 1 arrival 1 arrivall A 8 NULL NULL B
flight 1 departure 1 departure A 8 NULL NULL TREE
B
TREE
B
TREE

TRIGGERS(AFTER INSERT):
mysql> create table flights(
f_num varchar(20),
action varchar(50),
air_id varchar(20),
arr varchar(10),
dep varchar(10));
mysql> delimiter |
mysql>create trigger flight_tri
->after insert on flight
-> for each row
-> begin
-> insert into flights
-> setaction=’afterinsert',
-> f_num=new.f_number,
-> air_id=new.airplane_id,arr=new.arrival,dep=new.departure;
-> end|
mysql> delimiter ;
mysql> insert into flight(f_number,airplane_id,departure,d_time,arrival,a_time)
values("AA550",'1002',"CIA",'12:45:00',"SA",'14:00:00');
mysql> select *from flights;
+-------+--------------+--------+------+------+
| f_num | action | air_id | arr | dep |
+-------+--------------+--------+------+------+
| AA550 | after insert | 1002 | SA | CIA |
+-------+--------------+--------+------+------+
mysql> select *from flight;
+----------+-------------+-----------+----------+---------+----------+
| f_number | airplane_id | departure | d_time | arrival | a_time |
+----------+-------------+-----------+----------+---------+----------+
| AA100 | 1001 | CIA | 18:35:00 | SA | 21:00:00 |
| AA110 | 1001 | SA | 14:35:00 | TIA | 17:30:00 |
| AA200 | 1002 | TIA | 13:40:00 | CIA | 19:30:00 |
| AA210 | 1002 | MIA | 07:35:00 | SA | 10:30:00 |
| AA300 | 1001 | TIA | 19:30:00 | CIA | 22:00:00 |
| AA310 | 1002 | CIA | 17:00:00 | MIA | 21:00:00 |
| AA400 | 1002 | SA | 20:00:00 | TIA | 23:00:00 |
| AA410 | 1001 | TIA | 11:00:00 | CIA | 13:00:00 |
| AA550 | 1002 | CIA | 12:45:00 | SA | 14:00:00 |
+----------+-------------+-----------+----------+---------+----------+

TRIGGERS(BEFORE INSERT):

mysql> create table flights1(


f_num varchar(200),
action varchar(50),
air_id varchar(200),
arr varchar(100),
dep varchar(100));
mysql> delimiter |
mysql> create trigger flight_tri1
before insert on flight
for each row begin insert into flights1
set action='before insert',
f_num=new.f_number,
air_id=new.airplane_id,arr=new.arrival,dep=new.departure;
end|
mysql> delimiter ;

mysql> insert into flight(f_number,airplane_id,departure,d_time,arrival,a_time)


values("AA250",'1001',"MIA",'22:45:00',"TIA",'02:00:00');

mysql> select *from flights1;


+-------+---------------+--------+------+------+
| f_num | action | air_id | arr | dep |
+-------+---------------+--------+------+------+
| AA250 | before insert | 1001 | TIA | MIA |
+-------+---------------+--------+------+------+

mysql> select *from flight;


+----------+-------------+-----------+----------+---------+----------+
| f_number | airplane_id | departure | d_time | arrival | a_time |
+----------+-------------+-----------+----------+---------+----------+
| AA100 | 1001 | CIA | 18:35:00 | SA | 21:00:00 |
| AA110 | 1001 | SA | 14:35:00 | TIA | 17:30:00 |
| AA200 | 1002 | TIA | 13:40:00 | CIA | 19:30:00 |
| AA210 | 1002 | MIA | 07:35:00 | SA | 10:30:00 |
| AA250 | 1001 | MIA | 22:45:00 | TIA | 02:00:00 |
| AA300 | 1001 | TIA | 19:30:00 | CIA | 22:00:00 |
| AA310 | 1002 | CIA | 17:00:00 | MIA | 21:00:00 |
| AA400 | 1002 | SA | 20:00:00 | TIA | 23:00:00 |
| AA410 | 1001 | TIA | 11:00:00 | CIA | 13:00:00 |
| AA550 | 1002 | CIA | 12:45:00 | SA | 14:00:00 |
+----------+-------------+-----------+----------+---------+----------+
TRIGGERS(AFTER DELETE):
mysql> create table flights2(
-> f_num varchar(20),
-> action varchar(50),
-> air_id int(10),
-> arr varchar(10),
-> dep varchar(10));
mysql> delimiter |
mysql> create trigger flight_tri2
-> after delete on flight
-> for each row begin insert into flights2
-> set action='after delete',
-> f_num=old.f_number,
-> air_id=old.airplane_id,
-> arr=old.arrival,
-> dep=old.departure;
-> end|
mysql> delimiter ;
mysql> delete from flight where f_number='AA550';
mysql> select *from flights2;
+-------+--------------+--------+------+------+
| f_num | action | air_id | arr | dep |
+-------+--------------+--------+------+------+
| AA550 | after delete | 1002 | SA | CIA |
+-------+--------------+--------+------+------+
mysql> select *from flight;
+----------+-------------+-----------+----------+---------+----------+
| f_number | airplane_id | departure | d_time | arrival | a_time |
+----------+-------------+-----------+----------+---------+----------+
| AA100 | 1001 | CIA | 18:35:00 | SA | 21:00:00 |
| AA110 | 1001 | SA | 14:35:00 | TIA | 17:30:00 |
| AA200 | 1002 | TIA | 13:40:00 | CIA | 19:30:00 |
| AA210 | 1002 | MIA | 07:35:00 | SA | 10:30:00 |
| AA250 | 1001 | MIA | 22:45:00 | TIA | 02:00:00 |
| AA300 | 1001 | TIA | 19:30:00 | CIA | 22:00:00 |
| AA310 | 1002 | CIA | 17:00:00 | MIA | 21:00:00 |
| AA400 | 1002 | SA | 20:00:00 | TIA | 23:00:00 |
| AA410 | 1001 | TIA | 11:00:00 | CIA | 13:00:00 |
+----------+-------------+-----------+----------+---------+----------+

TRIGGERS(BEFORE DELETE):
mysql> create table flights3(
-> f_num varchar(20),
-> action varchar(50),
-> air_id int(10),
-> arr varchar(10),
-> dep varchar(10));
mysql> delimiter |
mysql> create trigger flight_tri3
-> before delete on flight
-> for each row
->begin
-> insert into flights3
-> set action='before delete',
-> f_num=old.f_number,
-> air_id=old.airplane_id,
-> arr=old.arrival,
-> dep=old.departure;
-> end|
mysql> delimiter ;
mysql> delete from flight where f_number='AA250';
mysql> select *from flights3;
+-------+---------------+--------+------+------+
| f_num | action | air_id | arr | dep |
+-------+---------------+--------+------+------+
| AA250 | before delete | 1001 | TIA | MIA |
+-------+---------------+--------+------+------+
mysql> select *from flight;
+----------+-------------+-----------+----------+---------+----------+
| f_number | airplane_id | departure | d_time | arrival | a_time |
+----------+-------------+-----------+----------+---------+----------+
| AA100 | 1001 | CIA | 18:35:00 | SA | 21:00:00 |
| AA110 | 1001 | SA | 14:35:00 | TIA | 17:30:00 |
| AA200 | 1002 | TIA | 13:40:00 | CIA | 19:30:00 |
| AA210 | 1002 | MIA | 07:35:00 | SA | 10:30:00 |
| AA300 | 1001 | TIA | 19:30:00 | CIA | 22:00:00 |
| AA310 | 1002 | CIA | 17:00:00 | MIA | 21:00:00 |
| AA400 | 1002 | SA | 20:00:00 | TIA | 23:00:00 |
| AA410 | 1001 | TIA | 11:00:00 | CIA | 13:00:00 |
+----------+-------------+-----------+----------+---------+----------+
TRIGGERS(AFTER UPDATE):
mysql> create table flights4(
f_num varchar(20),
action varchar(50),
air_id int(10),
arr varchar(10),
dep varchar(10));
mysql> delimiter |
mysql> create trigger flight_tri4
after update on flight
for each row
begin
insert into flights4
set action='after update',
f_num=new.f_number,
air_id=new.airplane_id,arr=new.arrival,
dep=new.departure;
end|
mysql> delimiter ;
mysql> select *from flight;
+----------+-------------+-----------+----------+---------+----------+
| f_number | airplane_id | departure | d_time | arrival | a_time |
+----------+-------------+-----------+----------+---------+----------+
| AA100 | 1001 | CIA | 18:35:00 | SA | 21:00:00 |
| AA110 | 1001 | SA | 14:35:00 | TIA | 17:30:00 |
| AA200 | 1002 | TIA | 13:40:00 | CIA | 19:30:00 |
| AA210 | 1002 | MIA | 07:35:00 | SA | 10:30:00 |
| AA300 | 1001 | TIA | 19:30:00 | CIA | 22:00:00 |
| AA310 | 1002 | CIA | 17:00:00 | MIA | 21:00:00 |
| AA400 | 1002 | SA | 20:00:00 | TIA | 23:00:00 |
| AA410 | 1001 | TIA | 11:00:00 | CIA | 13:00:00 |
+----------+-------------+-----------+----------+---------+----------+
mysql> update flight set departure="MIA"where f_number='AA400';
mysql> select *from flights4;
+-------+--------------+--------+------+------+
| f_num | action | air_id | arr | dep |
+-------+--------------+--------+------+------+
| AA400 | after update | 1002 | TIA | MIA |
+-------+--------------+--------+------+------+
TRIGGERS(BEFORE UPDATE):
mysql> create table flights5(
f_num varchar(20),
action varchar(50),
air_id int(20),
arr varchar(10),
dep varchar(10));
mysql> delimiter |
mysql> create trigger flight_tri5
before update on flight
for each row
begin
insert into flights5
set action='before update',
f_num=old.f_number,
air_id=old.airplane_id,
arr=old.arrival,
dep=old.departure;
end|
mysql> delimiter ;
mysql> select *from flight;
+----------+-------------+-----------+----------+---------+----------+
| f_number | airplane_id | departure | d_time | arrival | a_time |
+----------+-------------+-----------+----------+---------+----------+
| AA100 | 1001 | CIA | 18:35:00 | SA | 21:00:00 |
| AA110 | 1001 | SA | 14:35:00 | TIA | 17:30:00 |
| AA200 | 1002 | TIA | 13:40:00 | CIA | 19:30:00 |
| AA210 | 1002 | MIA | 07:35:00 | SA | 10:30:00 |
| AA300 | 1001 | TIA | 19:30:00 | CIA | 22:00:00 |
| AA310 | 1002 | CIA | 17:00:00 | MIA | 21:00:00 |
| AA400 | 1002 | MIA | 20:00:00 | TIA | 23:00:00 |
| AA410 | 1001 | TIA | 11:00:00 | CIA | 13:00:00 |
+----------+-------------+-----------+----------+---------+----------+
mysql> update flight set arrival="SA"where f_number='AA400';
mysql> select *from flights5;
+-------+---------------+--------+------+------+
| f_num | action | air_id | arr | dep |
+-------+---------------+--------+------+------+
| AA400 | before update | 1002 | TIA | MIA |
+-------+---------------+--------+------+------+
mysql> select *from flight;
+----------+-------------+-----------+----------+---------+----------+
| f_number | airplane_id | departure | d_time | arrival | a_time |
+----------+-------------+-----------+----------+---------+----------+
| AA100 | 1001 | CIA | 18:35:00 | SA | 21:00:00 |
| AA110 | 1001 | SA | 14:35:00 | TIA | 17:30:00 |
| AA200 | 1002 | TIA | 13:40:00 | CIA | 19:30:00 |
| AA210 | 1002 | MIA | 07:35:00 | SA | 10:30:00 |
| AA300 | 1001 | TIA | 19:30:00 | CIA | 22:00:00 |
| AA310 | 1002 | CIA | 17:00:00 | MIA | 21:00:00 |
| AA400 | 1002 | MIA | 20:00:00 | SA | 23:00:00 |
| AA410 | 1001 | TIA | 11:00:00 | CIA | 13:00:00 |
+----------+-------------+-----------+----------+---------+----------+
STORED PROCEDURES(WITH OUT PARAMETER):
delimiter |
mysql> create procedure flight_details( )
-> begin
-> select
-> f_number,
-> airplane_id,
-> departure,
-> arrival
-> from
-> flight
-> where
-> airplane_id='1001';
-> end|
mysql> delimiter ;
mysql> call flight_details;
+----------+-------------+-----------+---------+
| f_number | airplane_id | departure | arrival |
+----------+-------------+-----------+---------+
| AA100 | 1001 | CIA | SA |
| AA110 | 1001 | SA | TIA |
| AA300 | 1001 | TIA | CIA |
| AA410 | 1001 | TIA | CIA |
+----------+-------------+-----------+---------+

STORED PROCEDURES(WITH PARAMETER):


mysql>delimiter |
mysql> create procedure flight_details1(IN a INT)
->
-> begin
->
-> select * from class limit a ;
->
-> end|
mysql>delimiter ;
mysql> call flight_details1(3);
+----------+-------------+-----------+----------+---------+----------+
| f_number | airplane_id | departure | d_time | arrival | a_time |
+----------+-------------+-----------+----------+---------+----------+
| AA100 | 1001 | CIA | 18:35:00 | SA | 21:00:00 |
| AA110 | 1001 | SA | 14:35:00 | TIA | 17:30:00 |
| AA200 | 1002 | TIA | 13:40:00 | CIA | 19:30:00 |
+----------+-------------+-----------+----------+---------+----------+

STORED FUNCTIONS:
mysql>delimiter |
mysql> create function class_details(c_price float)
-> returns varchar(20)
-> deterministic
-> begin
-> declare classtype varchar(20);
-> if c_price>500 then
-> set classtype='firstclass';
-> else
-> set classtype='secondclass';
-> end if;
-> return(classtype);
-> end|

mysql>delimiter ;
mysql> select name,capacity,price,class_details(price) from class;
+----------+----------+-------+----------------------+
| name | capacity | price | class_details(price) |
+----------+----------+-------+----------------------+
| Business | 5 | 500 | secondclass |
| Economy | 200 | 180 | secondclass |
| Business | 15 | 4000 | firstclass |
| Economy | 100 | 1000 | firstclass |
| Business | 1 | 200 | secondclass |
| Economy | 100 | 100 | secondclass |
| Business | 15 | 800 | firstclass |
| Economy | 100 | 240 | secondclass |
+----------+----------+-------+----------------------+

CURSOR:
mysql> delimiter |
mysql> create procedure passanger_list(INOUT email_details varchar(30))
-> begin
-> declare finished INTEGER DEFAULT 0;
-> declare p_email varchar(30);
-> declare pass_cur CURSOR
-> FOR SELECT email from passanger;
-> declare continue handler for not found set finished=1;
-> open pass_cur;
-> get_email:
-> LOOP
-> FETCH pass_cur into p_email;
-> IF finished=1 then
-> leave get_email;
-> END IF;
-> set email_details=CONCAT(p_email,",",email_details);
-> END LOOP
-> get_email;
-> CLOSE pass_cur;
-> end|

mysql>delimiter;
mysql> set @email_details="";
mysql> call passanger_list;
mysql> call passanger_list(@email_details);

mysql> select @email_details;


+--------------------------------+
| @email_details |
+--------------------------------+
| [email protected],[email protected], |
+--------------------------------+

RESULT:
EX.NO: BANK DATABASE MANAGEMENT SYSTEM

DATE:

AIM:

CREATION OF DATABASE:
mysql> use bank_management;
mysql>create table t_customer(account_id int,email varchar(25),f_name varchar(20) not null,
l_name varchar(20) not null,username varchar(25) not null,pass varchar(50) not null,
primary key(account_id,username));
Query OK, 0 rows affected (0.59 sec)
mysql> desc t_customer;

+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| account_id | int(11) | NO | PRI | NULL | |
| email | varchar(25) | YES | | NULL | |
| f_name | varchar(20) | NO | | NULL | |
| l_name | varchar(20) | NO | | NULL | |
| username | varchar(25) | NO | PRI | NULL | |
| pass | varchar(50) | NO | | NULL | |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)

mysql> insert into t_customer values


(101,'[email protected]','vignesh','kumar','vignesh kumar','us101'),
(102,'[email protected]','prithivi','raj','prithivi raj','us102'),
(103,'[email protected]','lokeshwaran','m','lokeshwaran m','us103');
Query OK, 1 row affected (0.45 sec)
mysql> select * from t_customer;

+------------+-------------------+-------------+--------+---------------+-------+
| account_id | email | f_name | l_name | username | pass |
+------------+-------------------+-------------+--------+---------------+-------+
| 101 | [email protected] | vignesh | kumar | vignesh kumar | us101 |
| 102 | [email protected] | prithivi | raj | prithivi raj | us102 |
| 103 | [email protected] | lokeshwaran | m | lokeshwaran m | us103 |
+------------+-------------------+-------------+--------+---------------+-------+
3 rows in set (0.00 sec)

mysql> create table t_codes(code_id int,code_key varchar(15) not null,code_type varchar(20) not
null,code_desc varchar(1000) not null,primary key (code_id));
Query OK, 0 rows affected (0.58 sec)
mysql> desc t_codes;

+-----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+-------+
| code_id | int(11) | NO | PRI | NULL | |
| code_key | varchar(15) | NO | | NULL | |
| code_type | varchar(20) | NO | | NULL | |
| code_desc | varchar(1000) | NO | | NULL | |
+-----------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into t_codes values
(1001,'ck1001','swift','transanction'),
(1002,'ck1002','micr','transanction');
(1003,'ck1003','SBI','transanction');
mysql> select * from t_codes;

+---------+----------+-----------+--------------+
| code_id | code_key | code_type | code_desc |
+---------+----------+-----------+--------------+
| 1001 | ck1001 | swift | transanction |
| 1002 | ck1002 | micr | transanction |
| 1003 | ck1003 | SBI | transanction |
+---------+----------+-----------+--------------+
3 rows in set (0.00 sec)

mysql> create table t_account(account_id int,account_type int not null,amount double default 0,
primary key(account_id,account_type),
foreign key(account_id) references t_customer(account_id),
foreign key(account_type) references t_codes(code_id));
Query OK, 0 rows affected (0.32 sec)
mysql> desc t_account;

+--------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| account_id | int(11) | NO | PRI | NULL | |
| account_type | int(11) | NO | PRI | NULL | |
| amount | double | YES | | 0 | |
+--------------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into t_account values


(101,1001,'1000'),
(102,1002,'1000'),
(103,1003,'2000');
mysql> select * from t_account;
+------------+--------------+--------+
| account_id | account_type | amount |
+------------+--------------+--------+
| 101 | 1001 | 1000 |
| 102 | 1002 | 1000 |
| 103 | 1003 | 2000 |
+------------+--------------+--------+
3 rows in set (0.00 sec)
mysql> create table t_transanction_hist(account_id int,transanction_type int not null,
account_type int,amount double,primary key(account_id,transanction_type),
foreign key(transanction_type) references t_codes (code_id),
foreign key(account_type) references t_codes (code_id),
foreign key(account_id) references t_customer (account_id));
Query OK, 0 rows affected (0.64 sec)
mysql> desc t_transanction_hist;
+-------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------+------+-----+---------+-------+
| account_id | int(11) | NO | PRI | NULL | |
| transanction_type | int(11) | NO | PRI | NULL | |
| account_type | int(11) | YES | MUL | NULL | |
| amount | double | YES | | NULL | |
+-------------------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into t_transanction_hist values
(101,1001,1001,1000),
(102,1002,1002,1000),
(103,1003,1003,2000);
mysql> select * from t_transanction_hist;

+------------+-------------------+--------------+--------+
| account_id | transanction_type | account_type | amount |
+------------+-------------------+--------------+--------+
| 101 | 1001 | 1001 | 1000 |
| 102 | 1002 | 1002 | 1000 |
| 103 | 1003 | 1003 | 2000 |
+------------+-------------------+--------------+--------+
3 rows in set (0.00 sec)

mysql> create table t_transanction_hist_archive(account_id int,transanction_type int,


account_type int,amount double,primary key(account_id,transanction_type),
foreign key(transanction_type) references t_codes (code_id),
foreign key(account_type) references t_codes (code_id),
foreign key (account_id) references t_customer (account_id));
Query OK, 0 rows affected (0.81 sec)
mysql> desc t_transanction_hist_archive;

+-------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------+------+-----+---------+-------+
| account_id | int(11) | NO | PRI | NULL | |
| transanction_type | int(11) | NO | PRI | NULL | |
| account_type | int(11) | YES | MUL | NULL | |
| amount | double | YES | | NULL | |
+-------------------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table t_transanction_hist_archive add transanction_date datetime;


Query OK, 0 rows affected (0.72 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc t_transanction_hist_archive;

+-------------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+----------+------+-----+---------+-------+
| account_id | int(11) | NO | PRI | NULL | |
| transanction_type | int(11) | NO | PRI | NULL | |
| account_type | int(11) | YES | MUL | NULL | |
| amount | double | YES | | NULL | |
| transanction_date | datetime | YES | | NULL | |
+-------------------+----------+------+-----+---------+-------+
mysql> insert into t_transanction_hist_archive values
(101,1001,1001,1000,'2001-03-21 10:15:12');
(102,1002,1002,1000,'2003-05-02 12:17:30');
(103,1003,1003,2000,'2004-08-10 22:01:17');
Query OK, 1 row affected (0.52 sec)
mysql> select * from t_transanction_hist_archive;

+------------+-------------------+--------------+--------+---------------------+
| account_id | transanction_type | account_type | amount | transanction_date |
+------------+-------------------+--------------+--------+---------------------+
| 101 | 1001 | 1001 | 1000 | 2001-03-21 10:15:12 |
| 102 | 1002 | 1002 | 1000 | 2003-05-02 12:17:30 |
| 103 | 1003 | 1003 | 2000 | 2004-08-10 22:01:17 |
+------------+-------------------+--------------+--------+---------------------+
3 rows in set (0.00 sec)

mysql> update t_transanction_hist_archive set transanction_date='2002-04-12 11:12:54' where


account_id='101';
mysql> update t_transanction_hist_archive set transanction_date='2004-10-22 13:03:04' where
account_id='102';
Query OK, 1 row affected (0.48 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from t_transanction_hist_archive;

+------------+-------------------+--------------+--------+---------------------+
| account_id | transanction_type | account_type | amount | transanction_date |
+------------+-------------------+--------------+--------+---------------------+
| 101 | 1001 | 1001 | 1000 | 2002-04-12 11:12:54 |
| 102 | 1002 | 1002 | 1000 | 2004-10-22 13:03:04 |
| 103 | 1003 | 1003 | 2000 | 2004-08-10 22:01:17 |
+------------+-------------------+--------------+--------+---------------------+
3 rows in set (0.00 sec)

VIEWS:

mysql> create view v1 as select account_id,amount,transanction_date from t_transanction_hist_archive;


Query OK, 0 rows affected (0.47 sec)

mysql> select * from v1;

+------------+--------+---------------------+
| account_id | amount | transanction_date |
+------------+--------+---------------------+
| 101 | 1000 | 2002-04-12 11:12:54 |
| 102 | 1000 | 2004-10-22 13:03:04 |
| 103 | 2000 | 2004-08-10 22:01:17 |
+------------+--------+---------------------+
3 rows in set (0.00 sec)

mysql> create view v2 as select account_id,username,pass from t_customer;


Query OK, 0 rows affected (0.46 sec)
mysql> select * from v2;
+------------+---------------+-------+
| account_id | username | pass |
+------------+---------------+-------+
| 101 | vignesh kumar | us101 |
| 102 | prithivi raj | us102 |
| 103 | lokeshwaran m | us103 |
+------------+---------------+-------+
TRIGGER:

mysql> create table t1(


-> account_id int AUTO_INCREMENT PRIMARY KEY,
-> f_name VARCHAR(25) NOT NULL,
-> l_name VARCHAR(25) NOT NULL,
-> username VARCHAR(25) NOT NULL,
-> changedate DATETIME DEFAULT NULL,
-> action VARCHAR(25) DEFAULT NULL
-> );
Query OK, 0 rows affected (0.27 sec)

BEFORE UPDATE:

mysql> DELIMITER $$
mysql> CREATE TRIGGER before_bank_management_update
-> BEFORE UPDATE ON t_customer
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO t1
-> SET action='before update',
-> f_name=OLD.f_name,
-> l_name=OLD.l_name,
-> username=OLD.username,
-> changedate=NOW();
-> END $$
Query OK, 0 rows affected (0.06 sec)

mysql> DELIMITER ;

mysql> update t_customer set l_name='N' where account_id='101';


mysql> update t_customer set l_name='P' where account_id='102';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t1;

+------------+----------+--------+---------------+---------------------+--------------+
| account_id | f_name | l_name | username | changedate | action |
+------------+----------+--------+---------------+---------------------+--------------+
| 1 | vignesh | kumar | vignesh kumar | 2019-03-26 15:43:26 beforeupdate |
| 2 | prithivi | raj | prithivi raj | 2019-03-26 15:50:23 | beforeupdate |
+------------+----------+--------+---------------+---------------------+--------------+
2 rows in set (0.00 sec)

AFTER UPDATE:

mysql> DELIMITER $$
mysql> CREATE TRIGGER after_bank_management_update
-> AFTER UPDATE ON t_custome
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO t1
-> SET action='after update',
-> f_name=NEW.f_name,
-> l _ name=NEW.l_name,
-> username=NEW.username,
-> changedate=NOW();
-> END $$
Query OK, 0 rows affected (0.08 sec)
mysql> DELIMITER ;
mysql> update t_customer set l_name='M' where account_id='101';
mysql> update t_customer set l_name='M' where account_id='102';[email protected]
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t1;

+------------+----------+--------+---------------+---------------------+--------------+
| account_id | f_name | l_name | username | changedate | action |
+------------+----------+--------+---------------+---------------------+--------------+
| 1 | vignesh | kumar | vignesh kumar | 2019-03-26 15:43:26|before update |
| 2 | prithivi | raj | prithivi raj | 2019-03-26 15:50:23 | before update|
| 3 | vignesh | N | vignesh kumar | 2019-03-26 16:02:02 | before update|
| 4 | vignesh | M | vignesh kumar | 2019-03-26 16:02:02 | after update |
| 5 | prithivi | P | prithivi raj | 2019-03-26 16:03:26|before update |
| 6 | prithivi | M | prithivi raj | 2019-03-26 16:03:26|afterupdate |
+------------+----------+--------+---------------+---------------------+--------------+
6 rows in set (0.00 sec)

BEFORE INSERT:

mysql> DELIMITER $$
mysql> CREATE TRIGGER before_bank_management_insert
-> BEFORE INSERT ON t_customer
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO t1
-> SET action='before insert',
-> f_name=NEW.f_name,
-> l_name=NEW.l_name,
-> username=NEW.username,
-> changedate=NOW();
-> END $$
Query OK, 0 rows affected (0.09 sec)
mysql> DELIMITER ;
mysql> insert into t_customer values(104,'[email protected]','santhosh','M','santhosh m','us104');
Query OK, 1 row affected (0.04 sec)
mysql> select * from t1;
+------------+----------+--------+---------------+---------------------+--------------+
| account_id | f_name | l_name | username | changedate | action |
+------------+----------+--------+---------------+---------------------+--------------+
| 1 | vignesh | kumar | vignesh kumar | 2019-03-26 15:43:26 | before update|
| 2 | prithivi | raj | prithivi raj | 2019-03-26 15:50:23 | before update|
| 3 | vignesh | N | vignesh kumar | 2019-03-26 16:02:02 | before update|
| 4 | vignesh | M | vignesh kumar | 2019-03-26 16:02:02 | after update |
| 5 | prithivi | P | prithivi raj | 2019-03-26 16:03:26 | before update|
| 6 | prithivi | M | prithivi raj | 2019-03-26 16:03:26 | after update |
| 7 | santhosh | M | santhosh m | 2019-03-26 16:49:34 | before insert|
+------------+----------+--------+---------------+---------------------+--------------+
9 rows in set (0.00 sec)
AFTER INSERT:

mysql> DELIMITER $$
mysql> CREATE TRIGGER after_bank_management_insert
->AFTER INSERT ON t_customer
->FOR EACH ROW
->BEGIN INSERT INTO t1
->SET action = 'after insert',
->f_name = NEW.f_name,
->l_name = NEW.l_name,
->username = NEW.username,
->changedate = NOW();
->END$$
Query OK, 0 rows affected (0.08 sec)
mysql> DELIMITER ;
mysql> select * from t_customer;

+------------+-------------------+-------------+--------+---------------+-------+
| account_id | email | f_name | l_name | username | pass |
+------------+-------------------+-------------+--------+---------------+-------+
| 101 | [email protected] | vignesh | M | vignesh kumar | us101 |
| 102 | [email protected] | prithivi | M | prithivi raj | us102 |
| 103 | [email protected] | lokeshwaran | mr | lokeshwaran m | us103 |
| 104 | [email protected] | santhosh | M | santhosh m | us104 |
+------------+-------------------+-------------+--------+---------------+-------+
4 rows in set (0.00 sec)

mysql> insert into t_customer values(105,'[email protected]','gopi','s','gopi s','us105');


Query OK, 1 row affected (0.06 sec)
mysql> select * from t_customer;

+------------+-------------------+-------------+--------+---------------+-------+
| account_id | email | f_name | l_name | username | pass |
+------------+-------------------+-------------+--------+---------------+-------+
| 101 | [email protected] | vignesh | M | vignesh kumar | us101 |
| 102 | [email protected] | prithivi | M | prithivi raj | us102 |
| 103 | [email protected] | lokeshwaran | mr | lokeshwaran m | us103 |
| 104 | [email protected] | santhosh | M | santhosh m | us104 |
| 105 | [email protected] | gopi | s | gopi s | us105 |
+------------+-------------------+-------------+--------+---------------+-------+

mysql> select * from t1;

+------------+-------------+--------+---------------+--------------------+------------+
| account_id | f_name | l_name | username | changedate | action |
+------------+-------------+--------+---------------+--------------------+------------+
| 1 | vignesh | kumar | vignesh kumar | 2019-03-26 15:43:26|beforeupdate|
| 2 | prithivi | raj | prithivi raj | 2019-03-26 15:50:23|beforeupdate|
| 3 | vignesh | N | vignesh kumar | 2019-03-26 16:02:02|beforeupdate|
| 4 | vignesh | M | vignesh kumar | 2019-03-26 16:02:02|afterupdate |
| 5 | prithivi | P | prithivi raj | 2019-03-26 16:03:26|beforeupdate|
| 6 | prithivi | M | prithivi raj | 2019-03-26 16:03:26|afterupdate |
| 7 | santhosh | M | santhosh m | 2019-03-26 16:49:34|beforeinsert|
| 8 | gopi | s | gopi s | 2019-03-27 11:22:09|beforeinsert|
| 9 | gopi | s | gopi s | 2019-03-27 11:22:09|afterinsert |
+------------+-------------+--------+---------------+---------------------+-----------+
11 rows in set (0.00 sec)

BEFORE DELETE:

mysql> DELIMITER $$
mysql> CREATE TRIGGER before_bank_management_delete
-> BEFORE DELETE ON t_customer
-> FOR EACH ROW
-> BEGIN INSERT INTO t1
-> SET action = 'before delete',
-> f_name = OLD.f_name,
-> l_name = OLD.l_name,
-> username = OLD.username,
-> changedate = NOW();
-> END$$
Query OK, 0 rows affected (0.07 sec)
mysql> DELIMITER ;
mysql> select * from t_customer;

+------------+-------------------+-------------+--------+---------------+-------+
| account_id | email | f_name | l_name | username | pass |
+------------+-------------------+-------------+--------+---------------+-------+
| 101 | [email protected] | vignesh | M | vignesh kumar | us101 |
| 102 | [email protected] | prithivi | M | prithivi raj | us102 |
| 103 | [email protected] | lokeshwaran | mr | lokeshwaran m | us103 |
| 104 | [email protected] | santhosh | M | santhosh m | us104 |
| 105 | [email protected] | gopi | s | gopi s | us105 |
+------------+-------------------+-------------+--------+---------------+-------+
5 rows in set (0.00 sec)

mysql> delete from t_customer where account_id='105';


Query OK, 1 row affected (0.05 sec)
mysql> select * from t_customer;

+------------+-------------------+-------------+--------+---------------+-------+
| account_id | email | f_name | l_name | username | pass |
+------------+-------------------+-------------+--------+---------------+-------+
| 101 | [email protected] | vignesh | M | vignesh kumar | us101 |
| 102 | [email protected] | prithivi | M | prithivi raj | us102 |
| 103 | [email protected] | lokeshwaran | mr | lokeshwaran m | us103 |
| 104 | [email protected] | santhosh | M | santhosh m | us104 |
+------------+-------------------+-------------+--------+---------------+-------+
4 rows in set (0.01 sec)
mysql> select * from t1;

+------------+-------------+--------+---------------+--------------------+------------+
| account_id | f_name | l_name | username | changedate | action |
+------------+-------------+--------+---------------+--------------------+------------+
| 1 | vignesh | kumar | vignesh kumar | 2019-03-26 15:43:26|beforeupdate|
| 2 | prithivi | raj | prithivi raj | 2019-03-26 15:50:23|beforeupdate|
| 3 | vignesh | N | vignesh kumar | 2019-03-26 16:02:02|beforeupdate|
| 4 | vignesh | M | vignesh kumar | 2019-03-26 16:02:02|afterupdate |
| 5 | prithivi | P | prithivi raj | 2019-03-26 16:03:26|beforeupdate|
| 6 | prithivi | M | prithivi raj | 2019-03-26 16:03:26|afterupdate |
| 7 | santhosh | M | santhosh m | 2019-03-26 16:49:34|beforeinsert|
| 8 | gopi | s | gopi s | 2019-03-27 11:22:09|beforeinsert|
| 9 | gopi | s | gopi s | 2019-03-27 11:22:09|afterinsert |
| 10 | gopi | s | gopi s | 2019-03-27 11:26:08|beforedelete|
+------------+-------------+--------+---------------+--------------------+------------+
12 rows in set (0.00 sec)

AFTER DELETE:

mysql> CREATE TRIGGER after_bank_management_delete


-> AFTER DELETE ON t_customer
-> FOR EACH ROW
-> BEGIN INSERT INTO t1
-> SET action = 'after delete',
-> f_name = OLD.f_name,
-> l_name = OLD.l_name,
-> username = OLD.username,
-> changedate = NOW();
-> END$$
Query OK, 0 rows affected (0.07 sec)

mysql> DELIMITER ;
mysql> delete from t_customer where account_id='104';
Query OK, 1 row affected (0.04 sec)
mysql> select * from t_customer;

+------------+-------------------+-------------+--------+---------------+-------+
| account_id | email | f_name | l_name | username | pass |
+------------+-------------------+-------------+--------+---------------+-------+
| 101 | [email protected] | vignesh | M | vignesh kumar | us101 |
| 102 | [email protected] | prithivi | M | prithivi raj | us102 |
| 103 | [email protected] | lokeshwaran | mr | lokeshwaran m | us103 |
+------------+-------------------+-------------+--------+---------------+-------+
3 rows in set (0.00 sec)

mysql> select * from t1;

+------------+-------------+--------+---------------+--------------------+------------+
| account_id | f_name | l_name | username | changedate | action |
+------------+-------------+--------+---------------+--------------------+------------+
| 1 | vignesh | kumar | vignesh kumar | 2019-03-26 15:43:26|beforeupdate|
| 2 | prithivi | raj | prithivi raj | 2019-03-26 15:50:23|beforeupdate|
| 3 | vignesh | N | vignesh kumar | 2019-03-26 16:02:02|beforeupdate|
| 4 | vignesh | M | vignesh kumar | 2019-03-26 16:02:02|afterupdate |
| 5 | prithivi | P | prithivi raj | 2019-03-26 16:03:26|beforeupdate|
| 6 | prithivi | M | prithivi raj | 2019-03-26 16:03:26|afterupdate |
| 7 | santhosh | M | santhosh m | 2019-03-26 16:49:34|beforeinsert|
| 8 | gopi | s | gopi s | 2019-03-27 11:22:09|beforeinsert|
| 9 | gopi | s | gopi s | 2019-03-27 11:22:09|afterinsert |
| 11 | santhosh | M | santhosh m | 2019-03-27 11:28:03|beforedelete|
| 12 | santhosh | M | santhosh m | 2019-03-27 11:28:03|afterdelete |
+------------+-------------+--------+---------------+--------------------+------------+
14 rows in set (0.00 sec)

STORED FUNCTION:

mysql> DELIMITER$$
mysql> create FUNCTION f1(a double)
-> returns varchar(15)
-> DETERMINISTIC
-> BEGIN
-> DECLARE lvl varchar(10);
-> IF a>2000 then
-> set lvl='platinum';
-> ELSEIF (a<=2000 AND a >=1000)then
-> set lvl='gold'; ELSEIF a<1000 then
-> set lvl='silver';
-> END IF;
-> RETURN (lvl);
-> END$$
Query OK, 0 rows affected (0.34 sec)
mysql> DELIMITER ;
mysql> select f1(amount) from t_transanction_hist;
+------------+
| f1(amount) |
+------------+
| gold |
| gold |
| gold |
+------------+
3 rows in set (0.00 sec)
STORED PROCEDURE:

mysql> DELIMITER$$
mysql> create procedure pr_IN( IN a int )
-> select account_id from t_customer limit a;
Query OK, 0 rows affected (0.01 sec)

mysql> call pr_IN(3);

+------------+
| account_id |
+------------+
| 101 |
| 102 |
| 103 |
+------------+
3 rows in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)


mysql> create procedure pr2_OUT(OUT low_pass int) select min(pass) into low_pass from t_customer;
Query OK, 0 rows affected (0.00 sec)
mysql> call pr2_OUT(@PASS);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> SELECT (@PASS);

+---------+
| (@PASS) |
+---------+
| 0 |
+---------+
1 row in set (0.00 sec)

mysql> create procedure getallamount() select amount from t_transanction_hist;


Query OK, 0 rows affected (0.00 sec)

mysql> call getallamount();

+--------+
| amount |
+--------+
| 1000 |
| 1000 |
| 2000 |
+--------+
3 rows in set (0.00 sec)

CURSOR:

mysql> DELIMITER$$;
mysql> create procedure p12(
-> INOUT email_list varchar(30))
-> BEGIN
-> declare v_finished INTEGER DEFAULT 0;
-> declare v_email varchar(100) DEFAULT"";
-> declare email_cursor CURSOR FOR SELECT email FROM t_customer;
-> declare continue handler for not found set v_finished=1;
-> open email_cursor;
-> get_email:LOOP
-> FETCH email_cursor INTO v_email;
-> IF v_finished=1 then
-> leave get_email;
-> END IF;
-> set email_list = CONCAT(v_email,";",email_list);
-> END LOOP get_email;
-> CLOSE email_cursor;
-> end $$
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql> set @email_list="";
Query OK, 0 rows affected (0.00 sec)

mysql> call p12(@email_list);


Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> select @email_list;


+--------------------------------+
| @email_list |
+--------------------------------+
| [email protected];user102@gami |
+--------------------------------+
1 row in set (0.00 sec)

INDEX:

mysql> create index i1 on t_customer(account_id,f_name,l_name,username);


Query OK, 0 rows affected (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 0
Table Non Key name Seq Column name Collation Cardinality Sub Packed Null Index Comment
unique in part type
index

t_customer 0 PRIMARY 1 account_id A 2 NULL NULL BTREE

t_customer 0 PRIMARY 2 username A 2 NULL NULL BTREE

t_customer 1 i1 1 account_id A 3 NULL NULL BTREE

t_customer 1 i1 2 f_name A 3 NULL NULL BTREE

t_customer 1 i1 3 l_name A 3 NULL NULL BTREE

t_customer 1 i1 4 username A 3 NULL NULL BTREE

6 rows in set (0.00 sec)

mysql> quit;

RESULT:
EX.NO: ELECTRICITY BILL DATABASE MANAGEMENT SYSTEM

DATE:

AIM:

CREATION OF DATABASE:

mysql> create database EBBILL;


Query OK, 1 row affected (0.00 sec)
mysql> use EBBILL;
Database changed
mysql> create table user(u_id int ,u_name varchar(30),u_address varchar(50),primary key(u_id));
Query OK, 0 rows affected (0.31 sec)
mysql> desc user;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| u_id | int(11) | NO | PRI | NULL | |
| u_name | varchar(30) | YES | | NULL | |
| u_address | varchar(50) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into user(u_id,u_name,u_address)values(101,'G.Gomathi','Perumal kovil street');
Query OK, 1 row affected (0.04 sec)
mysql> insert into user values(102,'U.Kavya','Nehru street'),(103,'R.Panneer','Bharathi
nagar'),(104,'D.Vijay','Railway peter street'),(105,'N.Selvam','Ganabathi nagar');
Query OK, 1 row affected (0.03 sec)
mysql> select*from user;
+------+-----------+----------------------+
| u_id | u_name | u_address |
+------+-----------+----------------------+
| 101 | G.Gomathi | Perumal kovil street |
| 102 | U.Kavya | Nehru street |
| 103 | R.Panneer | Bharathi nagar |
| 104 | D.Vijay | Railway peter street |
| 105 | N.Selvam | Ganabathi nagar |
+------+-----------+----------------------+
5 rows in set (0.00 sec)
mysql> create table customer(cus_id int(10),cus_name varchar(20),cus_mobile int(11) NOT NULL,u_id int(11)
NOT NULL,primary key(cus_id),foreign key(u_id)references user(u_id));
Query OK, 0 rows affected (0.35 sec)
mysql> desc customer;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| cus_id | int(10) | NO | PRI | NULL | |
| cus_name | varchar(20) | YES | | NULL | |
| cus_mobile | int(11) | NO | | NULL | |
| u_id | int(11) | NO | MUL | NULL | |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into customer(cus_id,cus_name,cus_mobile,u_id)values(50023,'S.Surya',80654,103);
Query OK, 1 row affected (0.03 sec)
mysql> insert into customer values(50024,'A.Roja',67543,102),(50025,'C.Balaji',98765,101),
(50026,'K.Pooja',76584,105),(50027,'N.Savitha',81225,104);
mysql> select*from customer;
+--------+-----------+------------+------+
| cus_id | cus_name | cus_mobile | u_id |
+--------+-----------+------------+------+
| 50023 | S.Surya | 80654 | 103 |
| 50024 | A.Roja | 67543 | 102 |
| 50025 | C.Balaji | 98765 | 101 |
| 50026 | K.Pooja | 76584 | 105 |
| 50027 | N.Savitha | 81225 | 104 |
+--------+-----------+------------+------+
5 rows in set (0.00 sec)
mysql> create table login(log_id int(10),log_name varchar(30),pass_hint varchar(10),cus_id int(11) NOT
NULL,primary key(log_id),foreign key(cus_id)references customer(cus_id));
Query OK, 0 rows affected (0.45 sec)
mysql> desc login;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| log_id | int(10) | NO | PRI | NULL | |
| log_name | varchar(30) | YES | | NULL | |
| pass_hint | varchar(10) | YES | | NULL | |
| cus_id | int(11) | NO | MUL | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> insert into login(log_id,log_name,pass_hint,cus_id)values(1111,'admin',5,50026);


Query OK, 1 row affected (0.04 sec)
mysql> insert into login values(1112,'user01',3,50024),(1113,'batch02',8,50023),(1114,'awd',4,50027),
(1115,'admin19',7,50025);
Query OK, 1 row affected (0.04 sec)
mysql> select*from login;
+--------+----------+-----------+--------+
| log_id | log_name | pass_hint | cus_id |
+--------+----------+-----------+--------+
| 1111 | admin | 5 | 50026 |
| 1112 | user01 | 3 | 50024 |
| 1113 | batch02 | 8 | 50023 |
| 1114 | awd | 4 | 50027 |
| 1115 | admin19 | 7 | 50025 |
+--------+----------+-----------+--------+
5 rows in set (0.00 sec)
mysql> create table units(attender_id int,current_month_reading int,previous_month_reading int,log_id
int,primary key(attender_id),foreign key(log_id)references login(log_id));
Query OK, 0 rows affected (0.33 sec)
mysql> desc units;
+------------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+---------+------+-----+---------+-------+
| attender_id | int(11) | NO | PRI | NULL | |
| current_month_reading | int(11) | YES | | NULL | |
| previous_month_reading | int(11) | YES | | NULL | |
| log_id | int(11) | YES | MUL | NULL | |
+------------------------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into units(attender_id,current_month_reading,previous_month_reading,log_id)
values(321,140,170,1115);
Query OK, 1 row affected (0.04 sec)
mysql> insert into units values(322,120,90,1114),(323,200,290,1113),(324,70,100,1112),(325,220,140,1111);
Query OK, 1 row affected (0.05 sec)
mysql> select*from units;
+-------------+-----------------------+------------------------+--------+
| attender_id | current_month_reading | previous_month_reading | log_id |
+-------------+-----------------------+------------------------+--------+
| 321 | 140 | 170 | 1115 |
| 322 | 120 | 90 | 1114 |
| 323 | 200 | 290 | 1113 |
| 324 | 70 | 100 | 1112 |
| 325 | 220 | 140 | 1111 |
+-------------+-----------------------+------------------------+--------+
5 rows in set (0.00 sec)
mysql> create table bill(bill_id int,bill_num int,bill_cus_id int,attender_id int,primary
key(bill_id),foreign key(attender_id)references units(attender_
Query OK, 0 rows affected (0.34 sec)
mysql> desc bill;
+-------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| bill_id | int(11) | NO | PRI | NULL | |
| bill_num | int(11) | YES | | NULL | |
| bill_cus_id | int(11) | YES | | NULL | |
| attender_id | int(11) | YES | MUL | NULL | |
+-------------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into bill(bill_id,bill_num,bill_cus_id,attender_id)values(400123,34,121,325);
Query OK, 1 row affected (0.04 sec)
mysql> insert into bill values(400124,78,122,324),(400125,23,123,323),(400126,56,124,322),
(400127,58,125,321);
Query OK, 1 row affected (0.04 sec)

mysql> select*from bill;


+---------+----------+-------------+-------------+
| bill_id | bill_num | bill_cus_id | attender_id |
+---------+----------+-------------+-------------+
| 400123 | 34 | 121 | 325 |
| 400124 | 78 | 122 | 324 |
| 400125 | 23 | 123 | 323 |
| 400126 | 56 | 124 | 322 |
| 400127 | 58 | 125 | 321 |
+---------+----------+-------------+-------------+
5 rows in set (0.00 sec)
mysql> update customer set cus_name='K.Gopal' where cus_id=50025;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select*from customer;
+--------+-----------+------------+------+
| cus_id | cus_name | cus_mobile | u_id |
+--------+-----------+------------+------+
| 50023 | S.Surya | 80654 | 103 |
| 50024 | A.Roja | 67543 | 102 |
| 50025 | K.Gopal | 98765 | 101 |
| 50026 | K.Pooja | 76584 | 105 |
| 50027 | N.Savitha | 81225 | 104 |
+--------+-----------+------------+------+
5 rows in set (0.00 sec)
mysql> delete from bill where bill_id=400127;
Query OK, 1 row affected (0.05 sec)
mysql> select*from bill;
+---------+----------+-------------+-------------+
| bill_id | bill_num | bill_cus_id | attender_id |
+---------+----------+-------------+-------------+
| 400123 | 34 | 121 | 325 |
| 400124 | 78 | 122 | 324 |
| 400125 | 23 | 123 | 323 |
| 400126 | 56 | 124 | 322 |
+---------+----------+-------------+-------------+
4 rows in set (0.00 sec)
mysql> alter table customer add column gender varchar(20);
Query OK, 0 rows affected (0.76 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select*from customer;
+--------+-----------+------------+------+--------+
| cus_id | cus_name | cus_mobile | u_id | gender |
+--------+-----------+------------+------+--------+
| 50023 | S.Surya | 80654 | 103 | NULL |
| 50024 | A.Roja | 67543 | 102 | NULL |
| 50025 | K.Gopal | 98765 | 101 | NULL |
| 50026 | K.Pooja | 76584 | 105 | NULL |
| 50027 | N.Savitha | 81225 | 104 | NULL |
+--------+-----------+------------+------+--------+
5 rows in set (0.00 sec)
mysql> update customer set gender='female' where cus_id=50023;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update customer set gender='female' where cus_id=50024;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update customer set gender='male' where cus_id=50025;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update customer set gender='female' where cus_id=50026;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update customer set gender='female' where cus_id=50027;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select*from customer;
+--------+-----------+------------+------+--------+
| cus_id | cus_name | cus_mobile | u_id | gender |
+--------+-----------+------------+------+--------+
| 50023 | S.Surya | 80654 | 103 | female |
| 50024 | A.Roja | 67543 | 102 | female |
| 50025 | K.Gopal | 98765 | 101 | male |
| 50026 | K.Pooja | 76584 | 105 | female |
| 50027 | N.Savitha | 81225 | 104 | female |
+--------+-----------+------------+------+--------+
5 rows in set (0.00 sec)
mysql> alter table customer drop column cus_mobile;
Query OK, 0 rows affected (0.95 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select*from customer;
+--------+-----------+------+--------+
| cus_id | cus_name | u_id | gender |
+--------+-----------+------+--------+
| 50023 | S.Surya | 103 | female |
| 50024 | A.Roja | 102 | male |
| 50025 | K.Gopal | 101 | male |
| 50026 | K.Pooja | 105 | female |
| 50027 | N.Savitha | 104 | female |
+--------+-----------+------+--------+
5 rows in set (0.00 sec)

VIEWS:

mysql> create view user_details as select u_name,u_address from user;


Query OK, 0 rows affected (0.03 sec)
mysql> select*from user_details;
+-----------+----------------------+
| u_name | u_address |
+-----------+----------------------+
| G.Gomathi | Perumal kovil street |
| U.Kavya | Nehru street |
| R.Panneer | Bharathi nagar |
| D.Vijay | Railway peter street |
| N.Selvam | Ganabathi nagar |
+-----------+----------------------+
5 rows in set (0.00 sec)
mysql> create view customer_details as select cus_id,cus_name from customer;
Query OK, 0 rows affected (0.06 sec)
mysql> select*from customer_details;
+--------+-----------+
| cus_id | cus_name |
+--------+-----------+
| 50023 | S.Surya |
| 50024 | A.Roja |
| 50025 | K.Gopal |
| 50026 | K.Pooja |
| 50027 | N.Savitha |
+--------+-----------+
5 rows in set (0.00 sec)

STORED PROCEDURE:

mysql> create procedure getalluser()select u_name from user;


Query OK, 0 rows affected (0.05 sec)
mysql> call getalluser();
+-----------+
| u_name |
+-----------+
| D.Vijay |
| G.Gomathi |
| N.Selvam |
| R.Panneer |
| U.Kavya |
+-----------+
5 rows in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> create procedure user_in(in a int)select u_address from user limit a;
Query OK, 0 rows affected (0.01 sec)
mysql> call user_in(3);
+----------------------+
| u_address |
+----------------------+
| Perumal kovil street |
| Nehru street |
| Bharathi nagar |
+----------------------+
3 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> create procedure user_out(out no_ofuser int)select count(*) into no_ofuser from user;
Query OK, 0 rows affected (0.00 sec)
mysql> call user_out(@K);
Query OK, 1 row affected (0.00 sec)
mysql> select @k;
+------+
| @k |
+------+
| 5 |
+------+
1 row in set (0.00 sec)
mysql> create procedure getallcus()select cus_id from customer;
Query OK, 0 rows affected (0.00 sec)
mysql> call getallcus();
+--------+
| cus_id |
+--------+
| 50025 |
| 50024 |
| 50023 |
| 50027 |
| 50026 |
+--------+
5 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> create procedure cus_in(in b int)select cus_name from customer limit b;
Query OK, 0 rows affected (0.00 sec)
mysql> call cus_in(3);
+----------+
| cus_name |
+----------+
| A.Roja |
| K.Gopal |
| K.Pooja |
+----------+
3 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> create procedure cus_out(out customercount int)select count(*) into customercount from customer;
Query OK, 0 rows affected (0.00 sec)
mysql> call cus_out(@v);
Query OK, 1 row affected (0.00 sec)
mysql> select @v;
+------+
| @v |
+------+
| 5 |
+------+
1 row in set (0.00 sec)
TRIGGERS(AFTER INSERT):

mysql> create table user1(user_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,user_address varchar(64) default NULL);
Query OK, 0 rows affected (0.25 sec)
mysql> delimiter $$
mysql> create trigger eb_tri_aft after insert on user for each row begin insert into user1 set action='after
insert', user_name=NEW.u_name, user_address=NEW.u_address;end$$
Query OK, 0 rows affected (0.06 sec)
mysql> delimiter ;
mysql> insert into user values('106','A.Abishek','villupuram');
Query OK, 1 row affected (0.05 sec)
mysql> select * from user;
+------+-----------+----------------------+
| u_id | u_name | u_address |
+------+-----------+----------------------+
| 101 | G.Gomathi | Perumal kovil street |
| 102 | U.Kavya | Nehru street |
| 103 | R.Panneer | Bharathi nagar |
| 104 | D.Vijay | Railway peter street |
| 105 | N.Selvam | Ganabathi nagar |
| 106 | A.Abishek | villupuram |
+------+-----------+----------------------+
6 rows in set (0.00 sec)
mysql> select * from user1;
+-----------+--------------+----+--------------+
| user_name | action | id | user_address |
+-----------+--------------+----+--------------+
| A.Abishek | after insert | 1 | villupuram |
+-----------+--------------+----+--------------+
1 row in set (0.00 sec)

TRIGGERS(BEFORE INSERT):

mysql> create table user2(user_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,user_address varchar(64) default NULL);
Query OK, 0 rows affected (0.26 sec)
mysql> delimiter $$
mysql> create trigger eb_tri_bft before insert on user for each row begin insert into user2 set action='before
insert', user_name=NEW.u_name, user_address=NEW.u_address; end$$
Query OK, 0 rows affected (0.06 sec)
mysql> delimiter ;
mysql> insert into user values('107','N.Vignesh','chennai');
Query OK, 1 row affected (0.05 sec)
mysql> select * from user;
+------+-----------+----------------------+
| u_id | u_name | u_address |
+------+-----------+----------------------+
| 101 | G.Gomathi | Perumal kovil street |
| 102 | U.Kavya | Nehru street |
| 103 | R.Panneer | Bharathi nagar |
| 104 | D.Vijay | Railway peter street |
| 105 | N.Selvam | Ganabathi nagar |
| 106 | A.Abishek | villupuram |
| 107 | N.Vignesh | chennai |
+------+-----------+----------------------+
7 rows in set (0.00 sec)
mysql> select * from user2;
+-----------+---------------+----+--------------+
| user_name | action | id | user_address |
+-----------+---------------+----+--------------+
| N.Vignesh | before insert | 1 | chennai |
+-----------+---------------+----+--------------+
1 row in set (0.00 sec)

TRIGGERS(AFTER DELETE):

mysql> create table user3(user_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,user_address varchar(64) default NULL);
Query OK, 0 rows affected (0.23 sec)
mysql> delimiter $$
mysql> create trigger eb_tri_ad after delete on user for each row begin insert into user3
set action='after delete', user_name=OLD.u_name, user_address=OLD.u_address; end$$
Query OK, 0 rows affected (0.05 sec)
mysql> delimiter ;
mysql> delete from user where u_id='105';
Query OK, 1 row affected (0.04 sec)
mysql> select * from user;
+------+-----------+----------------------+
| u_id | u_name | u_address |
+------+-----------+----------------------+
| 101 | G.Gomathi | Perumal kovil street |
| 102 | U.Kavya | Nehru street |
| 103 | R.Panneer | Bharathi nagar |
| 104 | D.Vijay | Railway peter street |
| 106 | A.Abishek | villupuram |
| 107 | N.Vignesh | chennai |
+------+-----------+----------------------+
6 rows in set (0.00 sec)
mysql> select * from user3;
+-----------+--------------+----+-----------------+
| user_name | action | id | user_address |
+-----------+--------------+----+-----------------+
| N.Selvam | after delete | 1 | Ganabathi nagar |
+-----------+--------------+----+-----------------+
1 row in set (0.00 sec)

TRIGGERS(BEFORE DELETE):

mysql> create table user4(user_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,user_address varchar(64) default NULL);
Query OK, 0 rows affected (0.43 sec)
mysql> delimiter $$
mysql> create trigger eb_tri_bd before delete on user for each row begin insert into user4
set action='before delete', user_name=OLD.u_name, user_address=OLD.u_address; end$$
Query OK, 0 rows affected (0.06 sec)
mysql> delimiter ;
mysql> delete from user where u_id='104';
Query OK, 1 row affected (0.04 sec)
mysql> select * from user;
+------+-----------+----------------------+
| u_id | u_name | u_address |
+------+-----------+----------------------+
| 101 | G.Gomathi | Perumal kovil street |
| 102 | U.Kavya | Nehru street |
| 103 | R.Panneer | Bharathi nagar |
| 106 | A.Abishek | villupuram |
| 107 | N.Vignesh | chennai |
+------+-----------+----------------------+
5 rows in set (0.00 sec)
mysql> select * from user4;
+-----------+---------------+----+----------------------+
| user_name | action | id | user_address |
+-----------+---------------+----+----------------------+
| D.Vijay | before delete | 1 | Railway peter street |
+-----------+---------------+----+----------------------+
1 row in set (0.00 sec)

TRIGGERS(AFTER UPDATE):

mysql> create table user5(user_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,user_address varchar(64) default NULL,change_date DATETIME default NULL);
Query OK, 0 rows affected (0.37 sec)
mysql> delimiter $$
mysql> create trigger eb_tri_au after update on user for each row begin insert into user5
set action='after update', user_name=NEW.u_name, user_address=NEW.u_address,change_date=NOW();
-> end$$
Query OK, 0 rows affected (0.05 sec)
mysql> delimiter ;
mysql> update user set u_address='madurai' where u_id='103';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from user;
+------+-----------+----------------------+
| u_id | u_name | u_address |
+------+-----------+----------------------+
| 101 | G.Gomathi | Perumal kovil street |
| 102 | U.Kavya | Nehru street |
| 103 | R.Panneer | madurai |
| 106 | A.Abishek | villupuram |
| 107 | N.Vignesh | chennai |
+------+-----------+----------------------+
5 rows in set (0.00 sec)
mysql> select * from user5;
+-----------+--------------+----+--------------+---------------------+
| user_name | action | id | user_address | change_date |
+-----------+--------------+----+--------------+---------------------+
| R.Panneer | after update | 1 | madurai | 2019-03-27 15:25:54 |
+-----------+--------------+----+--------------+---------------------+
1 row in set (0.01 sec)

TRIGGERS(BEFORE UPDATE):
mysql> create table user6(user_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,user_address varchar(64) default NULL,change_date DATETIME default NULL);
Query OK, 0 rows affected (0.25 sec)
mysql> delimiter $$
mysql> create trigger eb_tri_bu before update on user for each row begin insert into user6 set
action='before update', user_name=OLD.u_name, user_address=OLD.u_address,change_date=NOW();end$$
Query OK, 0 rows affected (0.06 sec)
mysql> delimiter ;
mysql> update user set u_address='thiruvannamalai' where u_id='102';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from user;
+------+-----------+----------------------+
| u_id | u_name | u_address |
+------+-----------+----------------------+
| 101 | G.Gomathi | Perumal kovil street |
| 102 | U.Kavya | thiruvannamalai |
| 103 | R.Panneer | madurai |
| 106 | A.Abishek | villupuram |
| 107 | N.Vignesh | chennai |
+------+-----------+----------------------+
5 rows in set (0.00 sec)
mysql> select * from user6;
+-----------+---------------+----+--------------+---------------------+
| user_name | action | id | user_address | change_date |
+-----------+---------------+----+--------------+---------------------+
| U.Kavya | before update | 1 | Nehru street | 2019-03-27 15:30:58 |
+-----------+---------------+----+--------------+---------------------+
1 row in set (0.00 sec)

FUNCTION:

mysql> DELIMITER $$
mysql> CREATE FUNCTION f1(cus_mobile int) RETURNS VARCHAR(20) BEGIN
-> DECLARE lvl varchar(19);
-> IF cus_mobile>80000 THEN
-> SET lvl='PLATINUM';
-> ELSEIF(cus_mobile<=80000 AND cus_mobile>=70000) THEN
-> SET lvl='GOLD';
-> ELSEIF cus_mobile<70000 THEN SET lvl='SILVER';
-> END IF;
-> RETURN (lvl);
-> END $$
Query OK, 0 rows affected (0.05 sec)
mysql> DELIMITER ;
mysql> select f1(cus_mobile) from customer;
+----------------+
| f1(cus_mobile) |
+----------------+
| PLATINUM |
| SILVER |
| PLATINUM |
| GOLD |
| PLATINUM |
+----------------+
5 rows in set (0.00 sec)

CURSOR:

mysql> DELIMITER $$
mysql> CREATE PROCEDURE p1(INOUT name_list varchar(1000))
-> BEGIN
-> DECLARE v_finished INTEGER DEFAULT 0;
-> DECLARE v_name varchar(100) DEFAULT "";
-> DECLARE name_cursor cursor for SELECT cus_name FROM customer;
-> DECLARE continue handler for not found SET v_finished =1;
-> OPEN name_cursor;
-> get_name: LOOP
-> FETCH name_cursor INTO v_name;
-> IF v_finished =1 THEN LEAVE get_name;
-> END IF;
-> SET name_list=CONCAT(v_name,";",name_list);
-> END LOOP get_name;
-> CLOSE name_cursor;
-> END $$
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> SET @name_list="";
Query OK, 0 rows affected (0.00 sec)
mysql> CALL p1(@name_list);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @name_list;
+--------------------------------------------+
| @name_list |
+--------------------------------------------+
| N.Savitha;K.Pooja;C.Balaji;A.Roja;S.Surya; |
+--------------------------------------------+
1 row in set (0.00 sec)

INDEX:

mysql> create unique index user_index on user(u_name);


Query OK, 0 rows affected (0.38 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from user;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
| user | 0 | PRIMARY | 1 | u_id | A | 4 | NULL | NULL | | BTREE |
| user | 0 | user_index | 1 | u_name | A | 4 | NULL | NULL | YES | BTREE |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
2 rows in set (0.00 sec)

RESULT:
EX.NO: EMPLOYEE DATABASE MANAGEMENT SYSTEM

DATE:

AIM:

CREATION OF DATABASE:

mysql> Create datbase employee;


Query ok,1 row affected(0.00 sec)
mysql> use employee;
Database changed
mysql> create table employees(emp_no int(10)not null,birth_date DATE,name varchar(14),gender
enum('M','F'),email varchar(20),primary key(emp_no) );
Query OK, 0 rows affected (0.10 sec)
mysql> desc employees;
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| emp_no | int(10) | NO | PRI | NULL | |
| birth_date | date | YES | | NULL | |
| name | varchar(14) | YES | | NULL | |
| gender | enum('M','F') | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
+------------+---------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> insert into employees
values(01,'1999-08-22','Priya','F',’[email protected]’),(02,'1998-07-27','Kamal','M',’[email protected]’),
(03,'1988-11-12','Kamal','M',’[email protected]’),(04,'1989-10-11','Uma','F',’[email protected]’),
(05,'1992-09-08','Shakthi','F',’[email protected]’);
Query OK, 3 row affected (0.44 sec)
mysql> select*from employees;
+--------+------------+---------+--------+----------------+
| emp_no | birth_date | name | gender | email |
+--------+------------+---------+--------+----------------+
| 1 | 1999-08-22 | Priya | F | [email protected] |
| 2 | 1998-07-27 | Kamal | M | [email protected] |
| 3 | 1988-11-12 | Kamal | M | [email protected] |
| 4 | 1989-10-11 | Uma | F | [email protected] |
| 5 | 1992-09-08 | Shakthi | F | [email protected] |
+--------+------------+---------+--------+----------------+
5 rows in set (0.00 sec)
mysql> create table departments(dept_no int(10) not null,dept_name varchar(20) not null,dept_sec
varchar(10),primary key(dept_name),foreign key(dept_no) references dept_emp(dept_no));
Query OK, 0 rows affected (0.09 sec)
mysql> desc departments;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| dept_no | int(10) | NO | MUL | NULL | |
| dept_name | varchar(20) | NO | PRI | NULL | |
| dept_sec | varchar(10) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into departments
values(12,'IT','A1'),(13,'ECE','A2'),(14,'CSE','A3'),(15,'EEE','A4'),(16,'MECH','A5');
Query OK, 1 row affected (0.04 sec)
mysql> select * from departments;
+---------+-----------+----------+
| dept_no | dept_name | dept_sec |
+---------+-----------+----------+
| 14 | CSE | A3 |
| 13 | ECE | A2 |
| 15 | EEE | A4 |
| 12 | IT | A1 |
| 16 | MECH | A5 |
+---------+-----------+----------+
5 rows in set (0.01 sec)
mysql> create table titles(emp_no int(10) not null,name varchar(50),from_dt DATE,to_dt DATE,title
varchar(50),primary key(title),foreign key(emp_no) references employees(emp_no));
Query OK, 0 rows affected (0.09 sec)
mysql> desc titles;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| emp_no | int(10) | NO | MUL | NULL | |
| name | varchar(50) | YES | | NULL | |
| from_dt | date | YES | | NULL | |
| to_dt | date | YES | | NULL | |
| title | varchar(50) | NO | PRI | | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into titles values(01,'priya','2018-12-01','2018-12-10','DBMS'),
(02,'kamal','2018-12-11','2018-12-20','CT'),(03,'Ram','2018-12-21','2018-12-31','OS'),
(04,'uma','2019-01-01','2019-01-10','CA'),(05,'shakthi','2019-01-11','2019-01-20','Fibreoptics');
Query OK, 1 row affected (0.04 sec)
mysql> select*from titles;
+--------+---------+------------+------------+-------------+
| emp_no | name | from_dt | to_dt | title |
+--------+---------+------------+------------+-------------+
| 4 | uma | 2019-01-01 | 2019-01-10 | CA |
| 2 | kamal | 2018-12-11 | 2018-12-20 | CT |
| 1 | priya | 2018-12-01 | 2018-12-10 | DBMS |
| 5 | shakthi | 2019-01-11 | 2019-01-20 | Fibreoptics |
| 3 | Ram | 2018-12-21 | 2018-12-31 | OS |
+--------+---------+------------+------------+-------------+
5 rows in set (0.00 sec)
mysql> create table workers(emp_no int, pro_name varchar(10),wrkng_hrs varchar(10) not null,primary
key(wrkng_hrs),foreign key(emp_no) references employees(emp_no));
Query OK, 0 rows affected (0.33 sec)
mysql> desc workers;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| emp_no | int(11) | YES | MUL | NULL | |
| pro_name | varchar(10) | YES | | NULL | |
| wrkng_hrs | varchar(10) | NO | PRI | NULL | |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into workers values('1','cd_tkrs','5hrs'),('2','evk_pwr','6hrs'),('3','kodiak','7hrs');
Query OK, 3 row affected (0.03 sec)
mysql> select * from workers;
+--------+----------+-----------+
| emp_no | pro_name | wrkng_hrs |
+--------+----------+-----------+
| 1 | cd_tkrs | 5hrs |
| 2 | evk_pwr | 6hrs |
| 3 | kodiak | 7hrs |
+--------+----------+-----------+
3 rows in set (0.00 sec)
mysql> alter table employees drop column email;
Query OK, 0 rows affected (0.72 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table employees drop column gender;
Query OK, 0 rows affected (0.98 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table employees add column dept_no int(10);
Query OK, 0 rows affected (0.65 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table employees add column salary int(20);
Query OK, 0 rows affected (0.69 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from employees;
+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salary |
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | NULL | NULL |
| 2 | 1998-07-27 | Kamal | NULL | NULL |
| 3 | 1988-11-12 | Ram | NULL | NULL |
| 4 | 1989-10-11 | Uma | NULL | NULL |
| 5 | 1992-09-08 | Shakthi | NULL | NULL |
+--------+------------+---------+---------+--------+
5 rows in set (0.00 sec)
mysql> update employees set dept_no='14',salary='95000' where emp_no='1';
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update employees set dept_no='12',salary='85000' where emp_no='2';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update employees set dept_no='13',salary='79000' where emp_no='3';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update employees set dept_no='14',salary='95000' where emp_no='1';
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update employees set dept_no='12',salary='85000' where emp_no='2';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update employees set dept_no='13',salary='79000' where emp_no='3';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from employees;
+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salary |
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 2 | 1998-07-27 | Kamal | 12 | 85000 |
| 3 | 1988-11-12 | Ram | 13 | 79000 |
| 4 | 1989-10-11 | Uma | NULL | NULL |
| 5 | 1992-09-08 | Shakthi | NULL | NULL |
+--------+------------+---------+---------+--------+
mysql> update employees set dept_no='15',salary='71000' where emp_no='4';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update employees set dept_no='16',salary='68000' where emp_no='5';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from employees;

+--------+---------+------------+---------+--------+
| emp_no | birth_date | name | dept_no | salary |
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 2 | 1998-07-27 | Kamal | 12 | 85000 |
| 3 | 1988-11-12 | Ram | 13 | 79000 |
| 4 | 1989-10-11 | Uma | 15 | 71000 |
| 5 | 1992-09-08 | Shakthi | 16 | 68000 |
+--------+------------+---------+---------+--------+
5 rows in set (0.01 sec)
mysql> select * from departments;
+---------+-----------+----------+
| dept_no | dept_name | dept_sec |
+---------+-----------+----------+
| 13 | ECE | A2 |
| 15 | EEE | A3 |
| 12 | IT | A1 |
+---------+-----------+----------+
3 rows in set (0.00 sec)
mysql> alter table employees add column location varchar(10);
Query OK, 0 rows affected (0.60 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table departments add column location varchar(10);
Query OK, 0 rows affected (0.83 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from departments;
+---------+-----------+----------+----------+
| dept_no | dept_name | dept_sec | location |
+---------+-----------+----------+----------+
| 13 | ECE | A2 | NULL |
| 15 | EEE | A3 | NULL |
| 12 | IT | A1 | NULL |
+---------+-----------+----------+----------+
3 rows in set (0.00 sec)
mysql> update departments set location='block_a' where dept_no='13';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update departments set location='block_b' where dept_no='12';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update departments set location='block_c' where dept_no='15';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from departments;
+---------+-----------+----------+----------+
| dept_no | dept_name | dept_sec | location |
+---------+-----------+----------+----------+
| 13 | ECE | A2 | block_a |
| 15 | EEE | A3 | block_c |
| 12 | IT | A1 | block_b |
+---------+-----------+----------+----------+
3 rows in set (0.00 sec)
mysql> select * from titles;
+--------+-------+------------+------------+-------+
| emp_no | name | from_dt | to_dt | title |
+--------+-------+------------+------------+-------+
| 2 | kamal | 2018-12-11 | 2018-12-20 | CT |
| 1 | priya | 2018-12-01 | 2018-12-10 | DBMS |
| 3 | Ram | 2018-12-21 | 2018-12-31 | OS |
+--------+-------+------------+------------+-------+
3 rows in set (0.00 sec)
mysql> rename table titles to project;
Query OK, 0 rows affected (0.39 sec)
mysql> select * from project;
+--------+-------+------------+------------+-------+
| emp_no | name | from_dt | to_dt | title |
+--------+-------+------------+------------+-------+
| 2 | kamal | 2018-12-11 | 2018-12-20 | CT |
| 1 | priya | 2018-12-01 | 2018-12-10 | DBMS |
| 3 | Ram | 2018-12-21 | 2018-12-31 | OS |
+--------+-------+------------+------------+-------+
3 rows in set (0.01 sec)
mysql> alter table project drop column name;
Query OK, 0 rows affected (0.94 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table project add column pro_name varchar(30);
Query OK, 0 rows affected (0.69 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update project set pro_name='code talkers' where title='CT';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update project set pro_name='evoke power' where title='DBMS';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update project set pro_name='kodiak' where title='OS';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from project;
+--------+------------+------------+-------+--------------+
| emp_no | from_dt | to_dt | title | pro_name |
+--------+------------+------------+-------+--------------+
| 2 | 2018-12-11 | 2018-12-20 | CT | code talkers |
| 1 | 2018-12-01 | 2018-12-10 | DBMS | evoke power |
| 3 | 2018-12-21 | 2018-12-31 | OS | kodiak |
+--------+------------+------------+-------+--------------+
3 rows in set (0.00 sec)

View:

mysql> create view wrks as select emp_no,wrkng_hrs from workers where pro_name='cd_tkrs';
Query OK, 0 rows affected (0.03 sec)
mysql> select * from wrks;
+--------+-----------+
| emp_no | wrkng_hrs |
+--------+-----------+
| 1 | 5hrs |
+--------+-----------+
1 row in set (0.00 sec)
mysql> create view emp_details as select name,salary,birth_date from employees;
Query OK, 0 rows affected (0.04 sec)
mysql> select * from emp_details;
+---------+--------+------------+
| name | salary | birth_date |
+---------+--------+------------+
| Priya | 95000 | 1999-08-22 |
| Kamal | 85000 | 1998-07-27 |
| Ram | 79000 | 1988-11-12 |
| Uma | 71000 | 1989-10-11 |
| Shakthi | 68000 | 1992-09-08 |
+---------+--------+------------+
5 rows in set (0.00 sec)

TRIGGER(BEFORE INSERT):

mysql> use employee;


Database changed
mysql> select*from employees;
+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salary |
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 2 | 1998-07-27 | Kamal | 12 | 85000 |
| 3 | 1988-11-12 | Ram | 13 | 79000 |
| 4 | 1989-10-11 | Uma | 15 | 71000 |
+--------+------------+---------+---------+--------+
6 rows in set (0.00 sec) t
Mysql> create table employees_auditemp_no int(11) not null auto increment primary key,name
varchar(20),changedat datetime default null );
Query OK, 0 rows affected(0.00 sec)
mysql> desc employees_audit;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| emp_no | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| changedat | datetime | YES | | NULL | |
| action | varchar(50) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> delimiter $$
mysql> create trigger tri_sal_insert before insert on employees for each row begin insert into employees_audit
set action ='insert',emp_no=new.emp_no,name= new.name,changedat=NOW();
-> end$$
Query OK, 0 rows affected (0.17 sec)
mysql> delimiter ;
mysql> insert into salaries values('5','shakthi',75000,'2019-01-11','2019-01-20');
Query OK, 1 row affected (0.11 sec)
mysql> select*from employees;
+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salaryy|
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 2 | 1998-07-27 | Kamal | 12 | 85000 |
| 3 | 1988-11-12 | Ram | 13 | 79000 |
| 4 | 1989-10-11 | Uma | 15 | 71000 |
| 5 | 1988-07-09 | shakthi | 16 | 83738 |
+--------+------------+---------+---------+--------+
5 rows in set (0.00 sec)
mysql> select*from employees_audit;
+--------+--------+---------------------+--------+
| emp_no | name | changedat | action |
+--------+--------+---------------------+--------+
| 5 | shathi | 2019-03-27 11:44:08 | insert |
+--------+--------+---------------------+--------+
1 row in set (0.00 sec)

BEFORE DELETE:

mysql> DELIMITER ;
mysql> DELIMITER $$
mysql> create trigger before_salary_delete before delete on employees for each row begin insert into
employees_audit set action ='before delete',emp_no=old.emp_no,name= old.name,changedat=NOW(); end$$
Query OK, 0 rows affected (0.39 sec)
mysql> DELIMITER ;
mysql> delete from employees where emp_no='2';
Query OK, 1 row affected (0.36 sec)
mysql> select*from employees;
+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salaryy|
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 3 | 1988-11-12 | Ram | 13 | 79000 |
| 4 | 1989-10-11 | Uma | 15 | 71000 |
| 5 | 1988-07-09 | shakthi | 16 | 83738 |
+--------+------------+---------+---------+--------+
4 rows in set (0.00 sec)
mysql> select*from employees_audit;
+--------+---------+---------------------+---------------+
| emp_no | name | changedat | action |
+--------+---------+---------------------+---------------+
| 5 | shakthi | 2019-03-29 22:44:08 | before insert |
| 2 | raj | 2019-03-29 22:53:46 | before delete |
+--------+---------+---------------------+---------------+
2 rows in set (0.01 sec)

AFTER INSERT:

mysql> create trigger after_insert after insert on employees for each row begin insert into employees_audit
set action='after insert',emp_no=new.emp_no,name=new.name,changedat=NOW();
-> end##
Query OK, 0 rows affected (0.21 sec)
mysql> delimiter ;
mysql> insert into employees values(6,'1979-01-21',vamsi,'17',78000);
Query OK, 1 row affected (0.35 sec)
mysql> select *from employees_audit;
+--------+---------+---------------------+---------------+
| emp_no | name | changedat | action |
+--------+---------+---------------------+---------------+
| 2 | Raj | 2019-03-27 22:44:08 | insert |
| 5 | shakthi | 2019-03-27 22:53:46 | before delete |
| 6 | vamsi | 2019-03-27 23:42:44 | after insert |
+--------+---------+---------------------+---------------+
3 rows in set (0.00 sec)
mysql> select*from employees;
+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salaryy|
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 3 | 1988-11-12 | Ram | 13 | 79000 |
| 4 | 1989-10-11 | Uma | 15 | 71000 |
| 5 | 1988-07-09 | shakthi | 16 | 83738 |
| 6 | 1979-01-21 | vamsi | 17 | 78000 |
+--------+------------+---------+---------+--------+
5 rows in set (0.00 sec)

AFTER DELETE:

mysql> create trigger after_delete after delete on employees for each row begin insert into employees_audit
set action='after insert',emp_no=new.emp_no,name=new.name,changedat=NOW();
-> end##
Query OK, 0 rows affected (0.21 sec)
mysql> delimiter ;
mysql> delete from employees where emp_no=’4’;
Query OK, 1 row affected (0.35 sec)
mysql> select *from employees_audit;
+--------+---------+---------------------+---------------+
| emp_no | name | changedat | action |
+--------+---------+---------------------+---------------+
| 2 | Raj | 2019-03-27 22:44:08 | insert |
| 5 | shakthi | 2019-03-27 22:53:46 | before delete |
| 6 | vamsi | 2019-03-27 23:42:44 | after insert |
| 4 | uma | 2019-03-27 22:57:43 | after delete |
+--------+---------+---------------------+---------------+
3 rows in set (0.00 sec)

BEFORE UPDATE:

mysql> select*from employees;


+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salaryy|
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 3 | 1988-11-12 | Ram | 13 | 79000 |
| 5 | 1988-07-09 | shakthi | 16 | 83738 |
| 6 | 1979-01-21 | vamsi | 17 | 78000 |
+--------+------------+---------+---------+--------+
4 rows in set (0.00 sec)
mysql> desc employees_audit;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| emp_no | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| changedat | datetime | YES | | NULL | |
| action | varchar(50) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> delimiter |
mysql> create trigger before_update before update on employees for each row begin insert into employees_audit
set action='after delete',emp_no=old.emp_no,name=old.name,changedat=NOW(); end|
Query OK, 0 rows affected (0.15 sec)
mysql> delimiter ;
mysql> update employees set salary='87000' where emp_no=3;
Query OK, 1 row affected (0.38 sec)
mysql> select *from employees_audit;
+--------+---------+---------------------+---------------+
| emp_no | name | changedat | action |
+--------+---------+---------------------+---------------+
| 2 | Raj | 2019-03-29 22:44:08 | insert |
| 4 | uma | 2019-03-29 23:45:47 | after delete |
| 5 | shakthi | 2019-03-29 22:53:46 | before delete |
| 6 | vamsi | 2019-03-29 23:42:44 | after insert |
| 3 | ram | 2019-03-29 22:55:44 | before update |
+--------+---------+---------------------+---------------+
5 rows in set (0.01 sec)
mysql> select*from employees;
+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salaryy|
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 3 | 1988-11-12 | Ram | 13 | 87000 |
| 5 | 1988-07-09 | shakthi | 16 | 83738 |
| 6 | 1979-01-21 | vamsi | 17 | 78000 |
+--------+------------+---------+---------+--------+
4 rows in set (0.01 sec)

AFTER UPDATE:

mysql> delimiter |
mysql> create trigger after_update after update on employees for each row begin insert into employees_audit
set action='after delete',emp_no=old.emp_no,name=old.name,changedat=NOW(); end|
Query OK, 0 rows affected (0.15 sec)
mysql> delimiter ;
mysql> update employees set depy_no='15' where emp_no=6;
Query OK, 1 row affected (0.38 sec)
mysql> select *from employees_audit;
+--------+---------+---------------------+---------------+
| emp_no | name | changedat | action |
+--------+---------+---------------------+---------------+
| 2 | Raj | 2019-03-29 22:44:08 | insert |
| 4 | uma | 2019-03-29 23:45:47 | after delete |
| 5 | shakthi | 2019-03-29 22:53:46 | before delete |
| 6 | vamsi | 2019-03-29 23:42:44 | after insert |
| 3 | ram | 2019-03-29 22:55:44 | before update |
| 6 | vamsi | 2019-03-29 23:45:43 | after update |
+--------+---------+---------------------+---------------+
5 rows in set (0.01 sec)
mysql> select*from employees;
+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salaryy|
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 3 | 1988-11-12 | Ram | 13 | 87000 |
| 5 | 1988-07-09 | shakthi | 16 | 83738 |
| 6 | 1979-01-21 | vamsi | 15 | 78000 |
+--------+------------+---------+---------+--------+
4 rows in set (0.01 sec)
FUNCTION:

mysql> select * from employees;


+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salary |
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 2 | 1998-07-27 | Kamal | 12 | 85000 |
| 3 | 1988-11-12 | Ram | 13 | 79000 |
| 4 | 1989-10-11 | Uma | 15 | 71000 |
+--------+------------+---------+---------+--------+
mysql> alter table employees add PF int;
Query OK, 4 rows affected (0.64 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> update employees set PF='20000'where emp_no='1';
Query OK, 1 row affected (0.37 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update employees set PF='25000'where emp_no='2';
Query OK, 1 row affected (0.13 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update employees set PF='15000'where emp_no='3';
Query OK, 1 row affected (0.14 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update employees set PF='10000'where emp_no='4';
Query OK, 1 row affected (0.13 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from employees;
+--------+------------+---------+---------+--------+---------+
| emp_no | birth_date | name | dept_no | salary | PF |
+--------+------------+---------+---------+--------+---------|
| 1 | 1999-08-22 | Priya | 14 | 90000 | 20000 |
| 2 | 1998-07-27 | Kamal | 12 | 98900 | 25000 |
| 3 | 1988-11-12 | Ram | 13 | 85000 | 15000 |
| 4 | 1989-10-11 | Uma | 15 | 78000 | 10000 |
+--------+------------+---------+---------+--------+---------+
4 rows in set (0.00 sec)
mysql> delimiter $$
mysql> create function slry(salary int,PF int)returns int begin declare total_salary int; set
total_salary=salary-PF; return total_salary; end$$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> select *, slry(salary,PF)as total_salary from employees;
+--------+-------+--------+------------+------------+-------+--------------+
| emp_no | name | salary | from_dt | to_dt | PF | total_salary |
+--------+-------+--------+------------+------------+-------+--------------+
| 1 | priya | 90000 | 2018-12-01 | 2018-12-10 | 20000 | 70000 |
| 2 | Raj | 98900 | 2018-12-11 | 2018-12-20 | 25000 | 73900 |
| 3 | Ram | 85000 | 2018-12-21 | 2018-12-31 | 15000 | 70000 |
| 4 | uma | 78000 | 2019-01-01 | 2019-01-10 | 10000 | 68000 |
+--------+-------+--------+------------+------------+-------+--------------+
4 rows in set (0.00 sec)
PROCEDURE:

mysql> select*from employees;


+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salary |
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 2 | 1998-07-27 | Kamal | 12 | 85000 |
| 3 | 1988-11-12 | Ram | 13 | 79000 |
| 4 | 1989-10-11 | Uma | 15 | 71000 |
| 5 | 1992-09-08 | Shakthi | 16 | 68000 |
| 6 | 1992-09-17 | vamsi | 17 | 88000 |
+--------+------------+---------+---------+--------+
6 rows in set (0.00 sec)

mysql> create procedure getallemp()


-> select*from employees;
Query OK, 0 rows affected (0.00 sec)
mysql> call getallemp();
+--------+------------+---------+---------+--------+
| emp_no | birth_date | name | dept_no | salary |
+--------+------------+---------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 2 | 1998-07-27 | Kamal | 12 | 85000 |
| 3 | 1988-11-12 | Ram | 13 | 79000 |
| 4 | 1989-10-11 | Uma | 15 | 71000 |
| 5 | 1992-09-08 | Shakthi | 16 | 68000 |
| 6 | 1992-09-17 | vamsi | 17 | 88000 |
+--------+------------+---------+---------+--------+
6 rows in set (0.00 sec)

mysql> create procedure empl_in(IN a int) select*from employees limit a;


Query OK, 0 rows affected (0.00 sec)
mysql> call empl_in(3);
+--------+------------+-------+---------+--------+
| emp_no | birth_date | name | dept_no | salary |
+--------+------------+-------+---------+--------+
| 1 | 1999-08-22 | Priya | 14 | 95000 |
| 2 | 1998-07-27 | Kamal | 12 | 85000 |
| 3 | 1988-11-12 | Ram | 13 | 79000 |
+--------+------------+-------+---------+--------+
3 rows in set (0.00 sec)

mysql> create procedure employ_out(OUT highsalary int) select max(salary) into highsalary from employees;
Query OK, 0 rows affected (0.00 sec)
mysql> call employ_out(@M);
Query OK, 1 row affected (0.00 sec)
mysql> select @M;
+-------+
| @M |
+-------+
| 95000 |
+-------+
1 row in set (0.00 sec)

mysql> create procedure employee_out(OUT lowsalary int) select min(salary) into lowsalary from employees;
Query OK, 0 rows affected (0.00 sec)

mysql> call employee_out(@M);


Query OK, 1 row affected (0.00 sec)
mysql> select @M;
+-------+
| @M |
+-------+
| 68000 |
+-------+
1 row in set (0.00 sec)

INDICES:

mysql> create index ind on departments(dept_no,dept_sec);


Query OK, 0 rows affected (0.45 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show indexes from departments;
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
| departments | 0 | PRIMARY | 1 | dept_name | A | 3 | NULL | NULL | | BTREE |
| departments | 0 | emp_indx | 1 | dept_name | A | 3 | NULL | NULL | | BTREE |
| departments | 0 | emp_indx | 2 | dept_sec | A | 3 | NULL | NULL | YES | BTREE |
| departments | 1 | ind | 1 | dept_no | A | 3 | NULL | NULL | | BTREE |
| departments | 1 | ind | 2 | dept_sec | A | 3 | NULL | NULL | YES | BTREE |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
5 rows in set (0.00 sec)

CURSOR:

mysql> select * from employees;


+--------+------------+---------+--------+-----------------+
| emp_no | birth_date | name | gender | email |
+--------+------------+---------+--------+-----------------+
| 1 | 1999-08-22 | Priya | F | [email protected] |
| 2 | 1998-07-27 | Kamal | M | [email protected] |
| 3 | 1988-11-12 | Ram | M | [email protected] |
| 4 | 1989-10-11 | Uma | F | [email protected] |
| 5 | 1992-09-08 | Shakthi | F | [email protected] |
+--------+------------+---------+--------+-----------------+
5 rows in set (0.00 sec)
mysql> delimiter $$
mysql> create procedure build_email_list(INOUT email_list varchar(400))
-> begin
-> declare v_finished integer default 0;
-> declare v_email varchar(200) default "";
-> declare email_cursor cursor for
-> select email from employees;
-> declare continue handler
-> for not found set v_finished=1;
-> open email_cursor;
-> get_email:loop
-> fetch email_cursor into v_email;
-> if v_finished=1 then
-> leave get_email;
-> end if;
-> set email_list=concat(v_email,";",email_list);
-> end loop get_email;
-> close email_cursor;
-> end $$
Query OK, 0 rows affected (0.07 sec)

mysql> delimiter ;
mysql> set @email_list="";
Query OK, 0 rows affected (0.00 sec)

mysql> call build_email_list(@email_list);


Query OK, 0 rows affected (0.03 sec)

mysql> select @email_list;


+----------------------------------------------------------------------------------+
| @email_list |
+----------------------------------------------------------------------------------+
| [email protected];[email protected];[email protected];[email protected];[email protected]; |
+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)

RESULT:
EX.NO: HOSPITAL DATABASE MANAGEMENT SYSTEM

DATE:

AIM:

CREATION OF DATABASE:

mysql> create database hospital;


Query OK, 1 row affected (0.00 sec)
mysql> use hospital;
Database changed
mysql> create table patient(p_id int,name varchar(20),age int,gender enum('M','F'),disease
varchar(15),doc_id int,PRIMARY KEY(p_id));
Query OK, 0 rows affected (0.24 sec)
mysql> desc patient;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| p_id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| gender | enum('M','F') | YES | | NULL | |
| disease | varchar(15) | YES | | NULL | |
| doc_id | int(11) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
mysql> insert into patient values('01','mano',19,'F','typhoid','4225'),
('02','gokul',20,'M','appendix','4226'),('03','gayathri',18,'F','jaundice','4227');
Query OK, 1 row affected (0.36 sec)
mysql> select * from patient;
+------+----------+------+--------+----------+--------+
| p_id | name | age | gender | disease | doc_id |
+------+----------+------+--------+----------+--------+
| 01 | mano | 19 | F | typhoid | 4225 |
| 02 | gokul | 20 | M | appendix | 4226 |
| 03 | gayathri | 18 | F | jaundice | 4227 |
+------+----------+------+--------+----------+--------+
3 rows in set (0.00 sec)
mysql> create table doctor(doc_id int,doc_name varchar(15),p_id int,dept varchar(20),PRIMARY
KEY(doc_id),FOREIGN KEY(p_id)references patient(p_id));
Query OK, 0 rows affected (0.77 sec)
mysql> desc doctor;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| doc_id | int(11) | NO | PRI | NULL | |
| doc_name | varchar(15) | YES | | NULL | |
| p_id | int(11) | YES | MUL | NULL | |
| dept | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> insert into doctor values ('4225','hari','01','general'),('4226','bhalaji','02','general'),
('4227','lakshmanan','03','general');
Query OK, 1 row affected (0.19 sec)
mysql> select * from doctor;
+--------+------------+------+---------+
| doc_id | doc_name | p_id | dept |
+--------+------------+------+---------+
| 4225 | hari | 01 | general |
| 4226 | bhalaji | 02 | general |
| 4227 | lakshmanan | 03 | general |
+--------+------------+------+---------+
3 rows in set (0.00 sec)
mysql> create table out_pat(p_id int,date DATE,doc_id int,PRIMARY KEY(p_id),FOREIGN KEY(doc_id)references
doctor(doc_id));
Query OK, 0 rows affected (0.50 sec)
mysql> desc out_pat;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| p_id | int(11) | NO | PRI | NULL | |
| date | date | YES | | NULL | |
| doc_id | int(11) | YES | MUL | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> insert into out_pat values ('01','2019-08-18','4225'),('02','2019-09-18','4226'),
('03','2019-10-18','4227');
Query OK, 1 row affected (0.55 sec)
mysql> select * from out_pat;
+------+------------+--------+
| p_id | date | doc_id |
+------+------------+--------+
| 01 | 2019-08-18 | 4225 |
| 02 | 2019-09-18 | 4226 |
| 03 | 2019-10-18 | 4227 |
+------+------------+--------+
3 rows in set (0.0sec)
mysql> create table in_pat(p_id int,room_no int,date_of_adm DATE,date_of_dis DATE,doc_id int,PRIMARY
KEY(p_id),FOREIGN KEY(doc_id)references doctor(doc_id));
Query OK, 0 rows affected (0.78 sec)
mysql> desc in_pat;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| p_id | int(11) | NO | PRI | NULL | |
| room_no | int(11) | YES | | NULL | |
| date_of_adm | date | YES | | NULL | |
| date_of_dis | date | YES | | NULL | |
| doc_id | int(11) | YES | MUL | NULL | |
+-------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into in_pat values('01',101,'2018-01-02','2018-01-04','4225'),
('02',102,'2018-02-02','2018-02-04','4226'),('03',103,'2018-03-02','2018-03-04','4227');
Query OK, 1 row affected (0.54 sec)
mysql> select * from in_pat;
+------+---------+-------------+-------------+--------+
| p_id | room_no | date_of_adm | date_of_dis | doc_id |
+------+---------+-------------+-------------+--------+
| 01 | 101 | 2018-01-02 | 2018-01-04 | 4225 |
| 02 | 102 | 2018-02-02 | 2018-02-04 | 4226 |
| 03 | 103 | 2018-03-02 | 2018-03-04 | 4227 |
+------+---------+-------------+-------------+--------+
3 rows in set (0.00 sec)
mysql> create table bill(bill_no int,p_id int,doc_charge int,medicine_charge int,no_of_days int,
nursing_charge int,PRIMARY KEY(bill_no),FOREIGN KEY(p_id)references in_pat(p_id));
Query OK, 0 rows affected (0.34 sec)
mysql> desc bill;
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| bill_no | int(11) | NO | PRI | NULL | |
| p_id | int(11) | YES | MUL | NULL | |
| doc_charge | int(11) | YES | | NULL | |
| medicine_charge | int(11) | YES | | NULL | |
| no_of_days | int(11) | YES | | NULL | |
| nursing_charge | int(11) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> insert into bill values ('1001','01','1000','300','5','500'),
('1002','02','2000','200','4','400'),('1003','03','3000','500','3','600');
Query OK, 1 row affected (0.54 sec)
mysql> select * from bill;
+---------+------+------------+-----------------+------------+----------------+
| bill_no | p_id | doc_charge | medicine_charge | no_of_days | nursing_charge |
+---------+------+------------+-----------------+------------+----------------+
| 1001 | 01 | 1000 | 300 | 5 | 500 |
| 1002 | 02 | 2000 | 200 | 4 | 400 |
| 1003 | 03 | 3000 | 500 | 3 | 600 |
+---------+------+------------+-----------------+------------+----------------+
3 rows in set (0.00 sec)
mysql> alter table bill add advance int;
Query OK, 0 rows affected (1.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update bill set advance='800';
Query OK, 3 rows affected (0.37 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> update bill set advance='1000' where bill_no='1002';
Query OK, 1 row affected (0.45 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update bill set advance='1500' where bill_no='1003';
Query OK, 1 row affected (0.47 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from bill;
+---------+------+------------+-----------------+------------+----------------+---------+---------+
| bill_no | p_id | doc_charge | medicine_charge | no_of_days | nursing_charge | advance |billamt |
+---------+------+------------+-----------------+------------+----------------+---------+---------+
| 1001 | 01 | 1000 | 300 | 5 | 500 | 800 |1700 |
| 1002 | 02 | 2000 | 200 | 4 | 400 | 1000 |2700 |
| 1003 | 03 | 3000 | 500 | 3 | 600 | 1500 |3700 |
+---------+------+------------+-----------------+------------+----------------+---------+---------+
3 rows in set (0.00 sec)
mysql> rename table patient to patient_details;
Query OK, 0 rows affected (0.56 sec)
mysql> show tables;
+--------------------+
| Tables_in_hospital |
+--------------------+
| bill |
| doctor |
| in_pat |
| out_pat |
| patient_details |
+--------------------+
mysql> alter table bill drop column advance;
Query OK, 0 rows affected (1.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from bill;
+---------+------+------------+-----------------+------------+----------------+
| bill_no | p_id | doc_charge | medicine_charge | no_of_days | nursing_charge |
+---------+------+------------+-----------------+------------+----------------+
| 1001 | 01 | 1000 | 300 | 5 | 500 |
| 1002 | 02 | 2000 | 200 | 4 | 400 |
| 1003 | 03 | 3000 | 500 | 3 | 600 |
+---------+------+------------+-----------------+------------+----------------+
3 rows in set (0.00 sec)
mysql> create table lab(lab_no varchar(5),weight int,category varchar(10),amount int);
Query OK, 0 rows affected (0.77 sec)
mysql> insert into lab values('lh-01','45','bloodtest','100');
Query OK, 1 row affected (0.47 sec)
mysql> insert into lab values('lh-02','65','blood','200');
Query OK, 1 row affected (0.34 sec)
mysql> select * from lab;
+--------+--------+-----------+--------+
| lab_no | weight | category | amount |
+--------+--------+-----------+--------+
| lh-01 | 45 | bloodtest | 100 |
| lh-02 | 65 | blood | 200 |
+--------+--------+-----------+--------+
2 rows in set (0.00 sec)
mysql> delete from lab where lab_no='lh-01';
Query OK, 1 row affected (0.46 sec)
mysql> select * from lab;
+--------+--------+----------+--------+
| lab_no | weight | category | amount |
+--------+--------+----------+--------+
| lh-02 | 65 | blood | 200 |
+--------+--------+----------+--------+
1 row in set (0.00 sec)
mysql> truncate table lab;
Query OK, 0 rows affected (0.63 sec)

mysql> show tables;


+--------------------+
| Tables_in_hospital |
+--------------------+
| bill |
| doctor |
| in_pat |
| lab |
| out_pat |
| patient_details |
+--------------------+
6 rows in set (0.00 sec)

VIEW :

mysql> create view patient_details1 as select p_id,name,disease from patient_details;


Query OK, 0 rows affected (0.52 sec)
mysql> select * from patient_details1;
+------+----------+----------+
| p_id | name | disease |
+------+----------+----------+
| 01 | mano | typhoid |
| 02 | gokul | appendix |
| 03 | gayathri | jaundice |
+------+----------+----------+
3 rows in set (0.00 sec)
mysql> create view doctor1 as select doc_id,doc_name,dept from doctor;
Query OK, 0 rows affected (0.48 sec)
mysql> select * from doctor1;
+--------+------------+---------+
| doc_id | doc_name | dept |
+--------+------------+---------+
| 4225 | hari | general |
| 4226 | bhalaji | general |
| 4227 | lakshmanan | general |
+--------+------------+---------+
3 rows in set (0.00 sec)

TRIGGER(BEFORE INSERT) :

mysql> create table t1(


-> id INT AUTO_INCREMENT PRIMARY KEY,
-> p_id INT NOT NULL,
-> name VARCHAR(50) NOT NULL,
-> age INT NOT NULL,
-> gender varchar(10) NOT NULL,
-> disease varchar(15) NOT NULL,
-> doc_id int NOT NULL,
-> changedate DATETIME DEFAULT NULL,
-> action VARCHAR(50) DEFAULT NULL
-> );
Query OK, 0 rows affected (1.17 sec)
mysql> DELIMITER $$
mysql> CREATE TRIGGER before_hospital_insert
-> BEFORE INSERT ON patient_details
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO t1
-> SET action ='before insert',
-> p_id=NEW.p_id,
-> name=NEW.name,
-> age=NEW.age,
-> gender=NEW.gender,
-> disease=NEW.disease,
-> doc_id=NEW.doc_id,
-> changedate=NOW();
-> END $$
Query OK, 0 rows affected (0.06 sec)
mysql> DELIMITER ;
mysql> insert into patient_details values(04,'gayu','10','F','fever','4228');
Query OK, 1 row affected (0.06 sec)
mysql> select * from t1;
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| id | p_id | name | age | gender | disease | doc_id | changedate | action |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| 1 | 3 | gayathri | 18 | F | jaundice | 4227 | 2019-03-27 12:38:17 | before update |
| 2 | 2 | gokul | 20 | M | appendix | 4226 | 2019-03-27 12:44:11 | before update |
| 3 | 2 | mani | 20 | M | appendix | 4226 | 2019-03-27 12:44:11 | after update |
| 4 | 4 | gayu | 10 | F | fever | 4228 | 2019-03-27 12:50:17 | before insert |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
4 rows in set (0.00 sec)

TRIGGER(BEFORE UPDATE) :
mysql> DELIMITER $$
mysql> CREATE TRIGGER before_hospital_update
-> BEFORE UPDATE ON patient_details
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO t1
-> SET action ='before update',
-> p_id=OLD.p_id,
-> name=OLD.name,
-> age=OLD.age,
-> gender=OLD.gender,
-> disease=OLD.disease,
-> doc_id=OLD.doc_id,
-> changedate=NOW();
-> END $$
Query OK, 0 rows affected (0.06 sec)
mysql> DELIMITER ;
mysql> select * from patient_details;
+------+----------+------+--------+----------+--------+
| p_id | name | age | gender | disease | doc_id |
+------+----------+------+--------+----------+--------+
| 1 | mano | 19 | F | typhoid | 4225 |
| 2 | gokul | 20 | M | appendix | 4226 |
| 3 | gayathri | 18 | F | jaundice | 4227 |
+------+----------+------+--------+----------+--------+
3 rows in set (0.00 sec)
mysql> update patient_details set name='deepak' where doc_id='4227';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from patient_details;
+------+--------+------+--------+----------+--------+
| p_id | name | age | gender | disease | doc_id |
+------+--------+------+--------+----------+--------+
| 1 | mano | 19 | F | typhoid | 4225 |
| 2 | gokul | 20 | M | appendix | 4226 |
| 3 | deepak | 18 | F | jaundice | 4227 |
+------+--------+------+--------+----------+--------+
mysql> select*from t1;
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| id | p_id | name | age | gender | disease | doc_id | changedate | action |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| 1 | 3 | gayathri | 18 | F | jaundice | 4227 | 2019-03-27 12:38:17 | before update |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
1 row in set (0.00 sec)
TRIGGER(BEFORE DELETE) :

mysql> DELIMITER $$
mysql> CREATE TRIGGER befor_hospital_delete BEFORE DELETE ON patient_details FOR EACH ROW BEGIN INSERT INTO
t1 SET action ='before delete',p_id=OLD.p_id,name=OLD.name,age=OLD.age,gender=OLD.gender,
disease=OLD.disease,doc_id=OLD.doc_id,changedate=NOW();END $$
Query OK, 0 rows affected (0.06 sec)
mysql> DELIMITER ;
mysql> select * from patient_details;
+------+--------+------+--------+----------+--------+
| p_id | name | age | gender | disease | doc_id |
+------+--------+------+--------+----------+--------+
| 1 | mano | 19 | F | typhoid | 4225 |
| 2 | mani | 20 | M | appendix | 4226 |
| 3 | deepak | 18 | F | jaundice | 4227 |
| 4 | gayu | 10 | F | fever | 4228 |
| 5 | lokesh | 19 | M | fever | 4229 |
+------+--------+------+--------+----------+--------+
5 rows in set (0.00 sec)
mysql> delete from patient_details where p_id='05';
Query OK, 1 row affected (0.05 sec)
mysql> select * from t1;
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| id | p_id | name | age | gender | disease | doc_id | changedate | action |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| 1 | 3 | gayathri | 18 | F | jaundice | 4227 | 2019-03-27 12:38:17 | before update |
| 2 | 2 | gokul | 20 | M | appendix | 4226 | 2019-03-27 12:44:11 | before update |
| 3 | 2 | mani | 20 | M | appendix | 4226 | 2019-03-27 12:44:11 | after update |
| 4 | 4 | gayu | 10 | F | fever | 4228 | 2019-03-27 12:50:17 | before insert |
| 5 | 5 | lokesh | 19 | M | fever | 4229 | 2019-03-27 14:08:52 | before insert |
| 6 | 5 | lokesh | 19 | M | fever | 4229 | 2019-03-27 14:08:52 | after insert |
| 7 | 5 | lokesh | 19 | M | fever | 4229 | 2019-03-27 14:14:03 | before delete |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
7 rows in set (0.00 sec)

mysql> select * from patient_details;


+------+--------+------+--------+----------+--------+
| p_id | name | age | gender | disease | doc_id |
+------+--------+------+--------+----------+--------+
| 1 | mano | 19 | F | typhoid | 4225 |
| 2 | mani | 20 | M | appendix | 4226 |
| 3 | deepak | 18 | F | jaundice | 4227 |
| 4 | gayu | 10 | F | fever | 4228 |
+------+--------+------+--------+----------+--------+
4 rows in set (0.00 sec)

TRIGGER(AFTER INSERT) :

mysql>DELIMITER $$
mysql> CREATE TRIGGER after_hospital_insert AFTER INSERT ON patient_details FOR EACH ROW BEGIN INSERT INTO
t1 SET action ='afterinsert',p_id=NEW.p_id,name=NEW.name,age=NEW.age,gender=NEW.gender,
disease=NEW.disease,doc_id=NEW.doc_id,changedate=NOW();END $$
mysql> DELIMITER ;
mysql> select * from patient_details;
+------+--------+------+--------+----------+--------+
| p_id | name | age | gender | disease | doc_id |
+------+--------+------+--------+----------+--------+
| 1 | mano | 19 | F | typhoid | 4225 |
| 2 | mani | 20 | M | appendix | 4226 |
| 3 | deepak | 18 | F | jaundice | 4227 |
| 4 | gayu | 10 | F | fever | 4228 |
+------+--------+------+--------+----------+--------+
4 rows in set (0.00 sec)
mysql> insert into patient_details values(05,'lokesh','19','M','fever','4229');
Query OK, 1 row affected (0.04 sec)
mysql> select * from t1;
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| id | p_id | name | age | gender | disease | doc_id | changedate | action |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| 1 | 3 | gayathri | 18 | F | jaundice | 4227 | 2019-03-27 12:38:17 | before update |
| 2 | 2 | gokul | 20 | M | appendix | 4226 | 2019-03-27 12:44:11 | before update |
| 3 | 2 | mani | 20 | M | appendix | 4226 | 2019-03-27 12:44:11 | after update |
| 4 | 4 | gayu | 10 | F | fever | 4228 | 2019-03-27 12:50:17 | before insert |
| 5 | 5 | lokesh | 19 | M | fever | 4229 | 2019-03-27 14:08:52 | before insert |
| 6 | 5 | lokesh | 19 | M | fever | 4229 | 2019-03-27 14:08:52 | after insert |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
6 rows in set (0.00 sec)

TRIGGER(AFTER UPDATE) :

mysql>DELIMITER //
mysql> CREATE TRIGGER after_hospital_update
-> AFTER UPDATE ON patient_details
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO t1
-> SET action ='after update',
-> p_id=NEW.p_id,
-> name=NEW.name,
-> age=NEW.age,
-> gender=NEW.gender,
-> disease=NEW.disease,
-> doc_id=NEW.doc_id,
-> changedate=NOW();
-> END //
Query OK, 0 rows affected (0.08 sec)
mysql> DELIMITER ;
mysql> update patient_details set name='mani' where doc_id='4226';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from patient_details;
+------+--------+------+--------+----------+--------+
| p_id | name | age | gender | disease | doc_id |
+------+--------+------+--------+----------+--------+
| 1 | mano | 19 | F | typhoid | 4225 |
| 2 | mani | 20 | M | appendix | 4226 |
| 3 | deepak | 18 | F | jaundice | 4227 |
+------+--------+------+--------+----------+--------+
3 rows in set (0.00 sec)
mysql> select * from t1;
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| id | p_id | name | age | gender | disease | doc_id | changedate | action |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| 1 | 3 | gayathri | 18 | F | jaundice | 4227 | 2019-03-27 12:38:17 | before update |
| 2 | 2 | gokul | 20 | M | appendix | 4226 | 2019-03-27 12:44:11 | before update |
| 3 | 2 | mani | 20 | M | appendix | 4226 | 2019-03-27 12:44:11 | after update |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
3 rows in set (0.00 sec)

TRIGGER(AFTER DELETE) :

mysql> DELIMITER $$
mysql> CREATE TRIGGER after_hospital_delete AFTER DELETE ON patient_details FOR EACH ROW BEGIN INSERT INTO
t1 SET action ='after delete',p_id=OLD.p_id,name=OLD.name,age=OLD.age,gender=OLD.gender,
disease=OLD.disease,doc_id=OLD.doc_id,changedate=NOW();END $$
Query OK, 0 rows affected (0.06 sec)
mysql> DELIMITER ;
mysql> delete from patient_details where p_id='03';
Query OK, 1 row affected (0.05 sec)
mysql> select * from patient_details;
+------+------+------+--------+----------+--------+
| p_id | name | age | gender | disease | doc_id |
+------+------+------+--------+----------+--------+
| 1 | mano | 19 | F | typhoid | 4225 |
| 2 | mani | 20 | M | appendix | 4226 |
| 4 | gayu | 10 | F | fever | 4228 |
+------+------+------+--------+----------+--------+
3 rows in set (0.00 sec)
mysql> select * from t1;
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| id | p_id | name | age | gender | disease | doc_id | changedate | action |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
| 1 | 3 | gayathri | 18 | F | jaundice | 4227 | 2019-03-27 12:38:17 | before update |
| 2 | 2 | gokul | 20 | M | appendix | 4226 | 2019-03-27 12:44:11 | before update |
| 3 | 2 | mani | 20 | M | appendix | 4226 | 2019-03-27 12:44:11 | after update |
| 4 | 4 | gayu | 10 | F | fever | 4228 | 2019-03-27 12:50:17 | before insert |
| 5 | 5 | lokesh | 19 | M | fever | 4229 | 2019-03-27 14:08:52 | before insert |
| 6 | 5 | lokesh | 19 | M | fever | 4229 | 2019-03-27 14:08:52 | after insert |
| 7 | 5 | lokesh | 19 | M | fever | 4229 | 2019-03-27 14:14:03 | before delete |
| 8 | 3 | deepak | 18 | F | jaundice | 4227 | 2019-03-27 14:15:57 | before delete |
| 9 | 3 | deepak | 18 | F | jaundice | 4227 | 2019-03-27 14:15:57 | after delete |
+----+------+----------+-----+--------+----------+--------+---------------------+---------------+
9 rows in set (0.01 sec)

STORED FUNCTION :

mysql> desc bill;


+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| bill_no | int(11) | NO | PRI | NULL | |
| p_id | int(11) | YES | MUL | NULL | |
| doc_charge | int(11) | YES | | NULL | |
| medicine_charge | int(11) | YES | | NULL | |
| no_of_days | int(11) | YES | | NULL | |
| nursing_charge | int(11) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> delimiter $$
mysql> create function billamt(nursing_charge int,medicine_charge int,doc_charge int)
-> returns int
-> begin
-> declare billamt int;
-> set billamt=nursing_charge+medicine_charge+doc_charge;
-> return billamt;
-> end$$
Query OK, 0 rows affected (0.06 sec)
mysql>delimiter ;
mysql> select billamt(nursing_charge,doc_charge,medicine_charge) as billamt from bill;
+---------+
| billamt |
+---------+
| 1700 |
| 2700 |
| 3700 |
+---------+
3 rows in set (0.04 sec)

STORED PROCEDURE :

mysql> create table patient_details(p_id int,name varchar(20),age int,gender enum('M','F'),


disease varchar(15),doc_id int,PRIMARY KEY(p_id));
Query OK, 0 rows affected (0.24 sec)
mysql> select * from patient_details;
+------+----------+------+--------+----------+--------+
| p_id | name | age | gender | disease | doc_id |
+------+----------+------+--------+----------+--------+
| 01 | mano | 19 | F | typhoid | 4225 |
| 02 | gokul | 20 | M | appendix | 4226 |
| 03 | gayathri | 18 | F | jaundice | 4227 |
+------+----------+------+--------+----------+--------+
3 rows in set (0.00 sec)

mysql> create procedure getallpatient()


-> select * from patient_details;
Query OK, 0 rows affected (0.39 sec)
mysql> call getallpatient();
+------+----------+------+--------+----------+--------+
| p_id | name | age | gender | disease | doc_id |
+------+----------+------+--------+----------+--------+
| 01 | mano | 19 | F | typhoid | 4225 |
| 02 | gokul | 20 | M | appendix | 4226 |
| 03 | gayathri | 18 | F | jaundice | 4227 |
+------+----------+------+--------+----------+--------+
3 rows in set (0.08 sec)
Query OK, 0 rows affected (0.08 sec)
mysql> create procedure doctor(IN var1 int)
-> select * from doctor limit var1;
Query OK, 0 rows affected (0.00 sec)
mysql> call doctor(3);
+--------+------------+------+---------+
| doc_id | doc_name | p_id | dept |
+--------+------------+------+---------+
| 4225 | hari | 01 | general |
| 4226 | bhalaji | 02 | general |
| 4227 | lakshmanan | 03 | general |
+--------+------------+------+---------+
3 rows in set (0.00 sec)
mysql> create procedure bill(OUT high_amt int)
-> select max(nursing_charge)into high_amt from bill;
Query OK, 0 rows affected (0.00 sec)
mysql> call bill(@M);
Query OK, 1 row affected (0.00 sec)

INDEX :

mysql> select * from patient;


+------+----------+------+--------+----------+--------+
| p_id | name | age | gender | disease | doc_id |
+------+----------+------+--------+----------+--------+
| 01 | mano | 19 | F | typhoid | 4225 |
| 02 | gokul | 20 | M | appendix | 4226 |
| 03 | gayathri | 18 | F | jaundice | 4227 |
+------+----------+------+--------+----------+--------+
3 rows in set (0.00 sec)

mysql> create unique index patient on patient_details(p_id,name);


Query OK, 0 rows affected (0.41 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from patient_details;
+-----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type |
+-----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------|
| patient_details | 0 | PRIMARY | 1 | p_id | A | 2 | NULL | NULL | | BTREE |
| patient_details | 0 | patient | 1 | p_id | A | 2 | NULL | NULL | | BTREE |
| patient_details | 0 | patient | 2 | name | A | 2 | NULL | NULL | YES | BTREE |
+-----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
3 rows in set (0.01 sec)

CURSOR :

mysql> delimiter ##
mysql> create procedure build_disease_list(INOUT dis_list varchar(50))
-> begin
-> declare v_finished integer default 0;
-> declare v_dis varchar(100) default "";
-> declare dis_cursor cursor for
-> select disease from patient;
-> declare continue handler
-> for not found set v_finished=1;
-> open dis_cursor;
-> get_dis:loop
-> fetch dis_cursor into v_dis;
-> if v_finished=1 then
-> leave get_dis;
-> end if;
-> set dis_list=concat(v_dis,";",dis_list);
-> end loop get_dis;
-> close dis_cursor;
-> end##
Query OK, 0 rows affected (0.03 sec)
mysql> delimiter ;
mysql> set @dis_list="";
Query OK, 0 rows affected (0.00 sec)
mysql> call build_disease_list(@dis_list);
Query OK, 0 rows affected (0.00 sec)
mysql> select @dis_list;
+----------------------------+
| @dis_list |
+----------------------------+
| jaundice;appendix;typhoid; |
+----------------------------+
1 row in set (0.00 sec)

RESULT:
EX.NO: HOSTEL DATABASE MANAGEMENT SYSTEM

DATE:

AIM:

CREATION OF DATABASE:

mysql> create database hostel;


Query OK, 1 row affected (0.58 sec)
mysql> use hostel;
Database changed
mysql> create table hstl(building_num int(20),no_of_rooms int,no_of_student varchar(50),annual_expenses
int,primary key(building_num));
Query OK, 0 rows affected (1.16 sec)
mysql> desc hstl;
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| building_num | int(20) | NO | PRI | NULL | |
| no_of_rooms | int(11) | YES | | NULL | |
| no_of_student | varchar(50) | YES | | NULL | |
| annual_expenses | int(11) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set (0.07 sec)
mysql> insert into hstl values
('01','50','100','45000'),('02','75','150','55000'),('03','85','250','65000');
Query OK, 1 row affected (0.38 sec)
mysql> select * from hstl;
+--------------+-------------+---------------+-----------------+
| building_num | no_of_rooms | no_of_student | annual_expenses |
+--------------+-------------+---------------+-----------------+
| 01 | 50 | 100 | 45000 |
| 02 | 75 | 150 | 55000 |
| 03 | 85 | 250 | 65000 |
+--------------+-------------+---------------+-----------------+
3 rows in set (0.01 sec)
mysql> create table room_status(stu_id int,room_no int,bef_vaccate_capacity int,after_vaccate_capacity
int,vaccate_date date,primary key(stu_id),foreign key(stu_id)references student(stu_id));
Query OK, 0 rows affected (0.66 sec)
mysql> desc room_status;
+------------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+---------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| room_no | int(11) | YES | | NULL | |
| bef_vaccate_capacity | int(11) | YES | | NULL | |
| after_vaccate_capacity | int(11) | YES | | NULL | |
| vaccate_date | date | YES | | NULL | |
+------------------------+---------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into room_status values ('422501','20','04','03','2018-10-09'),
('422502','21','05','04','2018-10-04'),('422503','22','03','02','2018-10-27');
Query OK, 1 row affected (0.06 sec)
mysql> select * from room_status;
+--------+---------+----------------------+------------------------+--------------+
| stu_id | room_no | bef_vaccate_capacity | after_vaccate_capacity | vaccate_date |
+--------+---------+----------------------+------------------------+--------------+
| 422501 | 20 | 4 | 3 | 2018-10-09 |
| 422502 | 21 | 5 | 4 | 2018-10-04 |
| 422503 | 22 | 3 | 2 | 2018-10-27 |
+--------+---------+----------------------+------------------------+--------------+
3 rows in set (0.00 sec)
mysql> create table student(stu_id int,room_no int,stu_name varchar(20),father_name varchar(20),dept
varchar(20),primary key(stu_id));
Query OK, 0 rows affected (0.40 sec)
mysql> desc student;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| room_no | int(11) | YES | | NULL | |
| stu_name | varchar(20) | YES | | NULL | |
| father_name | varchar(20) | YES | | NULL | |
| dept | varchar(20) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into student values ('422501','20','gayathri','lakshmanan','IT'),
('422502','21','mohana','siva','CSE'),('422503','22','mano','gokul','ECE');
Query OK, 1 row affected (0.06 sec)
mysql> select * from student;
+--------+---------+----------+-------------+------+
| stu_id | room_no | stu_name | father_name | dept |
+--------+---------+----------+-------------+------+
| 422501 | 20 | gayathri | lakshmanan | IT |
| 422502 | 21 | mohana | siva | CSE |
| 422503 | 22 | mano | gokul | ECE |
+--------+---------+----------+-------------+------+
3 rows in set (0.00 sec)
mysql> create table room(build_no int,stu_id int,capacity int,room_no int);
Query OK, 0 rows affected (0.70 sec)
mysql> desc room;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| build_no | int(11) | YES | | NULL | |
| stu_id | int(11) | YES | | NULL | |
| capacity | int(11) | YES | | NULL | |
| room_no | int(11) | NO | PRI | NULL | |
+----------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table room add primary key(room_no);
Query OK, 0 rows affected (1.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into room values ('01','422501','2','20'),('02','422502','2','21'),('03','422503','2','22');
Query OK, 1 row affected (0.39 sec)
mysql> select * from room;
+----------+--------+----------+---------+
| build_no | stu_id | capacity | room_no |
+----------+--------+----------+---------+
| 1 | 422501 | 2 | 20 |
| 2 | 422502 | 2 | 21 |
| 3 | 422503 | 2 | 22 |
+----------+--------+----------+---------+
3 rows in set (0.00 sec)

mysql> create table fees(feeslip_id int,stu_id int,fees_status varchar(20),dept varchar(25),primary


key(feeslip_id,stu_id));
Query OK, 0 rows affected (0.61 sec)
mysql> desc fees;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| feeslip_id | int(11) | NO | PRI | NULL | |
| stu_id | int(11) | NO | PRI | NULL | |
| fees_status | varchar(20) | YES | | NULL | |
| dept | varchar(25) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into fees values ('111','422501','paid','IT'),
('112','422502','notPaid','CSE'),('113','422503','paid','ECE');
Query OK, 1 row affected (0.38 sec)
mysql> alter table hstl add location varchar(40);
Query OK, 0 rows affected (0.97 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update hstl set location='east' where building_num='01';
Query OK, 1 row affected (0.41 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update hstl set location='west' where building_num='02';
Query OK, 1 row affected (0.39 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update hstl set location='north' where building_num='03';
Query OK, 1 row affected (0.16 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from hstl;
+--------------+-------------+---------------+-----------------+----------+
| building_num | no_of_rooms | no_of_student | annual_expenses | location |
+--------------+-------------+---------------+-----------------+----------+
| 01 | 50 | 100 | 45000 | east |
| 02 | 75 | 150 | 55000 | west |
| 03 | 85 | 250 | 65000 | north |
+--------------+-------------+---------------+-----------------+----------+
3 rows in set (0.00 sec)
mysql> rename table student to student_details;
Query OK, 0 rows affected (0.55 sec)
mysql> select * from student_details;
+--------+---------+----------+-------------+------+
| stu_id | room_no | stu_name | father_name | dept |
+--------+---------+----------+-------------+------+
| 422501 | 20 | gayathri | lakshmanan | IT |
| 422502 | 21 | mohana | siva | CSE |
| 422503 | 22 | mano | gokul | ECE |
+--------+---------+----------+-------------+------+
3 rows in set (0.30 sec)
mysql> update student_details set stu_name='gayathriLaxmanan' where stu_id='422501';
Query OK, 1 row affected (0.42 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student_details set stu_name='mohanasiva' where stu_id='422502';
Query OK, 1 row affected (0.39 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student_details set stu_name='manoGokul' where stu_id='422503';
Query OK, 1 row affected (0.38 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student_details;
+--------+---------+------------------+-------------+------+
| stu_id | room_no | stu_name | father_name | dept |
+--------+---------+------------------+-------------+------+
| 422501 | 20 | gayathriLaxmanan | lakshmanan | IT |
| 422502 | 21 | mohanasiva | siva | CSE |
| 422503 | 22 | manoGokul | gokul | ECE |
+--------+---------+------------------+-------------+------+
3 rows in set (0.00 sec)
mysql> delete from room_status where bef_vaccate_capacity='3';
Query OK, 1 row affected (0.35 sec)
mysql> select * from room_status;
+--------+---------+----------------------+------------------------+--------------+
| stu_id | room_no | bef_vaccate_capacity | after_vaccate_capacity | vaccate_date |
+--------+---------+----------------------+------------------------+--------------+
| 422501 | 20 | 4 | 3 | 2018-10-09 |
| 422502 | 21 | 5 | 4 | 2018-10-04 |
+--------+---------+----------------------+------------------------+--------------+
2 rows in set (0.00 sec)
mysql> alter table fees drop column dept;
Query OK, 0 rows affected (1.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from fees;
+------------+--------+-------------+
| feeslip_id | stu_id | fees_status |
+------------+--------+-------------+
| 111 | 422501 | paid |
| 112 | 422502 | notPaid |
| 113 | 422503 | paid |
+------------+--------+-------------+
3 rows in set (0.00 sec)
mysql> alter table student_details add address varchar(25);
Query OK, 0 rows affected (0.90 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update student_details set address='villupuram' where stu_id='422501';
Query OK, 1 row affected (0.47 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student_details set address='Thiruvanamalai' where stu_id='422502';
Query OK, 1 row affected (0.39 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student_details set address='Ulundurpet' where stu_id='422503';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student_details;
+--------+---------+------------------+-------------+------+----------------+
| stu_id | room_no | stu_name | father_name | dept | address |
+--------+---------+------------------+-------------+------+----------------+
| 422501 | 20 | gayathriLaxmanan | lakshmanan | IT | villupuram |
| 422502 | 21 | mohanasiva | siva | CSE | Thiruvanamalai |
| 422503 | 22 | manoGokul | gokul | ECE | Ulundurpet |
+--------+---------+------------------+-------------+------+----------------+
3 rows in set (0.00 sec)
mysql> drop table room_status;
Query OK, 0 rows affected (0.23 sec)

VIEW:

mysql> create view stu_det as select room_no,dept,address from student_details where stu_id='422501';
Query OK, 0 rows affected (0.39 sec)
mysql> select * from stu_det;
+---------+------+------------+
| room_no | dept | address |
+---------+------+------------+
| 20 | IT | villupuram |
+---------+------+------------+
1 row in set (0.00 sec)

mysql> create view fees_detail as select fees_status from fees where feeslip_id='111';
Query OK, 0 rows affected (0.08 sec)

mysql> select * from fees_detail;


+-------------+
| fees_status |
+-------------+
| paid |
+-------------+
1 row in set (0.04 sec)

TRIGGER(BEFORE INSERT):

mysql> create table hstl(building_num int(20),no_of_rooms int,no_of_student int(50),annual_expenses


int(10));
Query OK, 0 rows affected (0.97 sec)
mysql> create trigger annual_total before insert on hstl for each row set @total=@total+new.annual_expenses;
Query OK, 0 rows affected (0.11 sec)
mysql> set @total=0;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into hstl values('01','50','100','45000'),
('02','75','150','55000'),('03','85','250','65000');
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select @total as 'total annual_expenses';
+-----------------------+
| total annual_expenses |
+-----------------------+
| 165000 |
+-----------------------+
1 row in set (0.00 sec)
mysql> delimiter $$
mysql> create trigger annual_expenses_check before insert on hstl for each row if new.annual_expenses<55000
then set new.annual_expenses=55000;
-> end if;
-> end$$
Query OK, 0 rows affected (0.07 sec)
mysql>delimiter ;
mysql> insert into hstl values('04','55','85','60000');
Query OK, 1 row affected (0.05 sec)
mysql> select * from hstl;
+--------------+-------------+---------------+-----------------+
| building_num | no_of_rooms | no_of_student | annual_expenses |
+--------------+-------------+---------------+-----------------+
| 1 | 50 | 100 | 45000 |
| 2 | 75 | 150 | 55000 |
| 3 | 85 | 250 | 65000 |
| 4 | 55 | 85 | 60000 |
+--------------+-------------+---------------+-----------------+
4 rows in set (0.00 sec)
TRIGGER(BEFORE UPDATE):

mysql> create table hstl(building_num int(20),no_of_rooms int,no_of_student int(50),annual_expenses int);


Query OK, 0 rows affected (0.97 sec)
mysql> delimiter $$
mysql> create trigger upd_check before update on hstl for each row begin if new.annual_expenses<45000 then
set new.annual_expenses=75000;
-> elseif new.annual_expenses>45000 then set new.annual_expenses=33000;
-> end if;
-> end$$
Query OK, 0 rows affected (0.09 sec)
mysql> delimiter ;
mysql> update hstl set annual_expenses=53000 where building_num='1';
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * FROM hstl;
+--------------+-------------+---------------+-----------------+
| building_num | no_of_rooms | no_of_student | annual_expenses |
+--------------+-------------+---------------+-----------------+
| 1 | 50 | 100 | 33000 |
| 2 | 75 | 150 | 55000 |
| 3 | 85 | 250 | 65000 |
| 4 | 55 | 85 | 60000 |
+--------------+-------------+---------------+-----------------+
4 rows in set (0.00 sec)

TRIGGER(BEFORE DELETE):

mysql> create table removable_annual(building_num int(10),no_of_rooms int(10),no_of_student


int(10),annual_expenses int(10));
Query OK, 0 rows affected (0.29 sec)
mysql> delimiter $$
mysql> create trigger delete_annual before delete on hstl for each row begin insert into
removable_annual(building_num,no_of_rooms,no_of_student,annual_expenses)values
(old.building_num,old.no_of_rooms,old.no_of_student,old.annual_expenses); end$$
Query OK, 0 rows affected (0.09 sec)
mysql> delimiter ;
mysql> delete from hstl where building_num='1';
Query OK, 1 row affected (0.06 sec)
mysql> select * from delete_annual;
+--------------+-------------+---------------+-----------------+
| building_num | no_of_rooms | no_of_student | annual_expenses |
+--------------+-------------+---------------+-----------------+
| 1 | 50 | 100 | 33000 |
+--------------+-------------+---------------+-----------------+
1 row in set (0.00 sec)

TRIGGER(AFTER INSERT) :

mysql> create table hstl_af(building_num int(10),no_of_rooms int(10),no_of_students


int(10),annual_expenses int(10),action varchar(50));
Query OK, 0 rows affected (0.30 sec)

mysql>delimiter //
mysql> create trigger after_hstl after insert on hstl for each row begin insert into
hstl_af(building_num,no_of_rooms,no_of_student,annual_expenses,action)values
(NEW.building_num,NEW.no_of_rooms,NEW.no_of_student,NEW.annual_expenses,'AFTER INSERT'); END//
Query OK, 0 rows affected (0.10 sec)
mysql> delimiter ;
mysql> insert into hstl values('1','50','150','35000');
Query OK, 1 row affected (0.06 sec)
mysql> insert into hstl values('2','55','170','43000');
Query OK, 1 row affected (0.06 sec)
mysql> select *from hstl_af;
+--------------+-------------+---------------+-----------------+--------------+
| building_num | no_of_rooms | no_of_student | annual_expenses | action |
+--------------+-------------+---------------+-----------------+--------------+
| 1 | 50 | 150 | 35000 | AFTER INSERT |
| 2 | 55 | 170 | 43000 | AFTER INSERT |
+--------------+-------------+---------------+-----------------+--------------+
2 rows in set (0.00 sec)

TRIGGER(AFTER UPDATE):

mysql> create table hstl_af_up(building_num int(10),no_of_rooms int(10),no_of_student


int(10),annual_expenses int(10),action varchar(50));
Query OK, 0 rows affected (0.30 sec)
mysql> delimiter $$
mysql> create trigger after_upt after update on hstl for each row begin insert into
hstl_af_up(building_num,no_of_rooms,no_of_student,annual_expenses,action)values
(NEW.building_num,NEW.no_of_rooms,NEW.no_of_student,NEW.annual_expenses,'AFTER UPDATE'); END$$
Query OK, 0 rows affected (0.09 sec)
mysql> delimiter ;
mysql> update hstl set no_of_rooms='90' where building_num='1';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update hstl set no_of_rooms='100' where building_num='2';
Query OK, 2 rows affected (0.15 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> update hstl set no_of_rooms='120' where building_num='3';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from hstl_af_up;
+--------------+-------------+---------------+-----------------+--------------+
| building_num | no_of_rooms | no_of_student | annual_expenses | action |
+--------------+-------------+---------------+-----------------+--------------+
| 1 | 90 | 150 | 33000 | AFTER UPDATE |
| 2 | 100 | 150 | 33000 | AFTER UPDATE |
| 2 | 100 | 170 | 33000 | AFTER UPDATE |
| 3 | 120 | 250 | 33000 | AFTER UPDATE |
+--------------+-------------+---------------+-----------------+--------------+
4 rows in set (0.00 sec)

TRIGGER(AFTER DELETE):

mysql> create table hstl_af_de(building_num int(10),no_of_rooms int(10),no_of_student


int(10),annual_expenses int(10));
Query OK, 0 rows affected (0.30 sec)
mysql> create table hstl_af_de(building_num int(10),n10),no_of_student int(10),annual_expenses
int(10),action varchar(50),date date,time time);
Query OK, 0 rows affected (0.30 sec)
mysql> delimiter $$
mysql> create trigger af_delete_hstl after delete on hstl for each row begin insert into hstl_af_de
(building_num,no_of_rooms,no_of_student,annual_expenses,date,time)values
(OLD.building_num,OLD.no_of_rooms,OLD.no_of_student,OLD.annual_expenses,NOW(),SYSDATE());
-> END $$
Query OK, 0 rows affected (0.11 sec)
mysql> delimiter ;
mysql> delete from hstl where building_num='1';
Query OK, 1 row affected (0.13 sec)
mysql> delete from hstl where building_num='2';
Query OK, 2 rows affected (0.11 sec)
mysql> select * from hstl_af_de;
+--------------+-------------+---------------+-----------------+--------+------------+----------+
| building_num | no_of_rooms | no_of_student | annual_expenses | action | date | time |
+--------------+-------------+---------------+-----------------+--------+------------+----------+
| 1 | 90 | 150 | 33000 | NULL | 2019-03-27 | 14:24:57 |
| 2 | 100 | 150 | 33000 | NULL | 2019-03-27 | 14:25:06 |
| 2 | 100 | 170 | 33000 | NULL | 2019-03-27 | 14:25:06 |
+--------------+-------------+---------------+-----------------+--------+------------+----------+
3 rows in set (0.00 sec)

FUNCTION:

mysql> select*from fees;


+------------+--------+-------------+------+-----------+-------------+------------+
| feeslip_id | stu_id | fees_status | dept | mess_fees | hostel_fees | advance |
+------------+--------+-------------+------+-----------+-------------+------------+
| 111 | 422501 | paid | IT | 15000 | 10000 | 5000 |
| 112 | 422502 | notPaid | CSE | 20000 | 30000 | 5000 |
| 113 | 422503 | paid | ECE | 25000 | 20000 | 5000 |
+------------+--------+-------------+------+-----------+-------------+------------+
3 rows in set (0.00 sec)
mysql> delimiter $$
->create function fs(mess_fees int,hostel_fees int,advance int)returns int
->begin
->declare grant_fees int;
->set grant_fees=mess_fees+hostel_fees-advance;
->return grant_fees;
->end$$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> select *, fs(mess_fees,hostel_fees)as grant_fees from fees;
+------------+--------+-------------+------+-----------+-------------+----------+------------+
| feeslip_id | stu_id | fees_status | dept | mess_fees | hostel_fees | advance | grant_fees |
+------------+--------+-------------+------+-----------+-------------+----------+------------+
| 111 | 422501 | paid | IT | 15000 | 10000 | 5000 | 20000 |
| 112 | 422502 | notPaid | CSE | 20000 | 30000 | 5000 | 45000 |
| 113 | 422503 | paid | ECE | 25000 | 20000 | 5000 | 40000 |
+------------+--------+-------------+------+-----------+-------------+----------+------------+
3 rows in set (0.00 sec)

STORED_PROCEDURE(WITHOUT PARAMETER):

mysql> create procedure stu()


-> select * from student_details;
Query OK, 0 rows affected (0.36 sec)
mysql> call stu();
+--------+---------+------------------+-------------+------+----------------+
| stu_id | room_no | stu_name | father_name | dept | address |
+--------+---------+-----------------+-------------+------+----------------+
| 422501 | 20 | gayathriLaxmanan | lakshmanan | IT | villupuram |
| 422502 | 21 | mohanasiva | siva | CSE | Thiruvanamalai |
| 422503 | 22 | manoGokul | gokul | ECE | Ulundurpet |
+--------+---------+------------------+-------------+------+----------------+
3 rows in set (0.06 sec)

Query OK, 0 rows affected (0.06 sec)


STORED_PROCEDURE(WITH PARAMETER):

mysql> create procedure hstl1(IN annual_expenses int) select * from hstl limit annual_expenses;
Query OK, 0 rows affected (0.00 sec)
mysql> call hstl1(2);
+--------------+-------------+---------------+-----------------+----------+
| building_num | no_of_rooms | no_of_student | annual_expenses | location |
+--------------+-------------+---------------+-----------------+----------+
| 01 | 50 | 100 | 45000 | east |
| 02 | 75 | 150 | 55000 | west |
+--------------+-------------+---------------+-----------------+----------+
2 rows in set (0.31 sec)
Query OK, 0 rows affected (0.31 sec)
mysql> create procedure hstl2(OUT high_annual_expenses int) select max(annual_expenses) into
high_annual_expenses from hstl;
Query OK, 0 rows affected (0.29 sec)
mysql> call hstl2(@m);
Query OK, 1 row affected (0.09 sec)
mysql> select @m;
+-------+
| @m |
+-------+
| 65000 |
+-------+
1 row in set (0.00 sec)
mysql> create procedure hstl3(OUT low_annual_expenses int) select min(annual_expenses) into
low_annual_expenses from hstl;
Query OK, 0 rows affected (0.00 sec)
mysql> call hstl3(@k);
Query OK, 1 row affected (0.28 sec)
mysql> select @k;
+-------+
| @k |
+-------+
| 45000 |
+-------+
1 row in set (0.00 sec)

INDEX :

mysql> select * from room;


+----------+--------+----------+---------+
| build_no | stu_id | capacity | room_no |
+----------+--------+----------+---------+
| 1 | 422501 | 5 | 20 |
| 2 | 422502 | 5 | 21 |
+----------+--------+----------+---------+
2 rows in set (0.00 sec)

mysql> create index rms on room(stu_id);


Query OK, 0 rows affected (0.27 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show indexes from room where key_name='rms';
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
| room | 1 | rms | 1 | stu_id | A | 3 | NULL | NULL | YES | BTREE |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
1 row in set (0.00 sec)

CURSOR :

mysql> delimiter $$
mysql> create procedure myhstl()
-> reads sql data
-> begin
-> declare 1_last_row int default 0;
-> declare 1_hstl_stu_id int;
-> declare c_hstl cursor for
-> select stu_id from student_details;
-> declare continue handler for not found set 1_last_row=1;
-> open c_hstl;
-> hstl_cursor:loop
-> fetch c_hstl into 1_hstl_stu_id;
-> if(1_last_row=1) then
-> leave hstl_cursor;
-> end if;
-> select 1_hstl_stu_id;
-> end loop hstl_cursor;
-> close c_hstl;
-> end$$
Query OK, 0 rows affected (0.05 sec)
mysql> delimiter ;
mysql> call myhstl();
+---------------+-------------+
| 1_hstl_stu_id |
+-----------------------------+
| 422501;422502;422503 |
+-----------------------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)

RESULT:
EX:NO:
HOTEL DATABASE MANAGEMENT SYSTEM

DATE:

AIM

CREATION OF DATABASE:

mysql>create database hotel_management;


mysql>use hotel_management;
mysql> CREATE TABLE hotel( hotelID INT PRIMARY KEY, address VARCHAR(64) NOT NULL, name VARCHAR(32) NOT NULL,
phoneNumber INT NOT NULL );
mysql> desc hotel;

+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| hotelID | int(11) | NO | PRI | NULL | |
| address | varchar(64) | NO | | NULL | |
| name | varchar(32) | NO | | NULL | |
| phoneNumber | int(11) | NO | | NULL | |
+-------------+-------------+------+-----+---------+-------+

mysql> INSERT INTO hotel VALUES


(100, '27 Timber Dr, Garner, NC 27529', 'WolfVilla',6342587544),
(101, 'chennai','sky_residency',9365874596),
(102, 'mumbai','gopi_residency',8825902425);
mysql> select * from hotel;

+---------+--------------------------------+----------------+-------------+
| hotelID | address | name | phoneNumber |
+---------+--------------------------------+----------------+-------------+
| 100 | 27 Timber Dr, Garner, NC 27529 | WolfVilla | 6342587544 |
| 101 | chennai | sky_residency | 9365874596 |
| 102 | mumbai | gopi_residency | 8825902425 |
+---------+--------------------------------+----------------+-------------+

mysql> CREATE TABLE roomType (r_id INT PRIMARY KEY,roomType VARCHAR(32) NOT NULL,floor varchar(10));
mysql> desc roomType;

+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| r_id | int(11) | NO | PRI | NULL | |
| roomType | varchar(32) | NO | | NULL | |
| floor | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+

mysql> INSERT INTO roomType VALUES


(201,'ac','first'),
(211,'non_ac','ground'),
(202,'ac','first');
mysql> select * from roomType;

+-----+----------+--------+
| r_id| roomType | floor |
+-----+----------+--------+
| 201 | ac | first |
| 202 | ac | first |
| 211 | non_ac | ground |
+-----+----------+--------+

mysql> CREATE TABLE customer ( customerID INT PRIMARY KEY, name VARCHAR(32),email VARCHAR(32), address
VARCHAR(64), phoneNumber INT,r_id int not null,foreign key(r_id) references roomType(r_id));
mysql> desc customer;

+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| customerID | int(11) | NO | PRI | NULL | |
| name | varchar(32) | YES | | NULL | |
| email | varchar(32) | YES | | NULL | |
| address | varchar(64) | YES | | NULL | |
| phoneNumber | int(11) | YES | | NULL | |
| r_id | int(11) | NO | MUL | NULL | |
+-------------+-------------+------+-----+---------+-------+

mysql> insert into customer values


(1,'santhosh','[email protected]','chennai','2147483647',201),
(2,'ajaykumar','[email protected]','villupuram','2147483647',202),
(3,'gopikrishnan','[email protected]','mumbai','2147483647',211);

mysql> select * from customer;

+------------+--------------+---------------+------------+-------------+-----+
| customerID | name | email | address | phoneNumber | id |
+------------+--------------+---------------+------------+-------------+-----+
| 1 | santhosh | [email protected] | chennai | 2147483647 | 201 |
| 2 | ajaykumar | [email protected] | villupuram | 2147483647 | 202 |
| 3 | gopikrishnan | [email protected] | mumbai | 2147483647 | 211 |
+------------+--------------+---------------+------------+-------------+-----+

mysql> update customer set phoneNumber='8825902425' where customerID='1';


mysql> update customer set phoneNumber='9597708371' where customerID='2';

mysql> select * from customer;

+------------+--------------+---------------+------------+-------------+-----+
| customerID | name | email | address | phoneNumber | id |
+------------+--------------+---------------+------------+-------------+-----+
| 1 | santhosh | [email protected] | chennai | 8825902425 | 201 |
| 2 | ajaykumar | [email protected] | villupuram | 9597708371 | 202 |
| 3 | gopikrishnan | [email protected] | mumbai | 2147483647 | 211 |
+------------+--------------+---------------+------------+-------------+-----+

mysql> create table service(service_id int primary key,server_name varchar(20),service_type varchar(20));


mysql> desc service;

+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| service_id | int(11) | NO | PRI | NULL | |
| server_name | varchar(20) | YES | | NULL | |
| service_type | varchar(20) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
mysql> alter table service add customerID int(10);
mysql> alter table service add foreign key(customerID) references customer(customerID);
mysql> desc service;

+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| service_id | int(11) | NO | PRI | NULL | |
| server_name | varchar(20) | YES | | NULL | |
| service_type | varchar(20) | YES | | NULL | |
| customerID | int(10) | YES | MUL | NULL | |
+--------------+-------------+------+-----+---------+-------+

mysql> insert into service values


('10','vignesh','serving_food','1'),
('11','maniraj','went to shop','3'),
('12','kavi','driving','2');

mysql> select * from service;

+------------+-------------+--------------+------------+
| service_id | server_name | service_type | customerID |
+------------+-------------+--------------+------------+
| 10 | vignesh | serving_food | 1 |
| 11 | maniraj | went to shop | 3 |
| 12 | kavi | driving | 2 |
+------------+-------------+--------------+------------+

nmysql> create view contact_details as select name,address,phoneNumber from customer;


Query OK, 0 rows affected (0.36 sec)
mysql> select * from contact_details;

+--------------+------------+-------------+
| name | address | phoneNumber |
+--------------+------------+-------------+
| santhosh | chennai | 8825902425 |
| ajaykumar | villupuram | 9597708371 |
| gopikrishnan | mumbai | 2147483647 |
+--------------+------------+-------------+
mysql> create table charges(no int not null primary key,staying_days int,charges int,id int,foreign key(id)
references roomType (id));
mysql> desc charges;
+--------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| no | int(11) | NO | PRI | NULL | |
| staying_days | int(11) | YES | | NULL | |
| charges | int(11) | YES | | NULL | |
| id | int(11) | YES | MUL | NULL | |
+--------------+---------+------+-----+---------+-------+
mysql> insert into charges values
('501',10,15000,201),
('502',15,20000,202),
('503',5,6000,211);
mysql> select * from charges;

+-----+--------------+---------+------+
| no | staying_days | charges | id |
+-----+--------------+---------+------+
| 501 | 10 | 15000 | 201 |
| 502 | 15 | 21000 | 202 |
| 503 | 5 | 4500 | 211 |
+-----+--------------+---------+------+

VIEWS

mysql> create view r_charges as select staying_days,charges from charges;


mysql> select * from r_charges;

+--------------+---------+
| staying_days | charges |
+--------------+---------+
| 10 | 15000 |
| 15 | 21000 |
| 5 | 4500 |
+--------------+---------+

mysql> create view contact_details as select name,address,phoneNumber from customer;


mysql> select * from contact_details;

+--------------+------------+-------------+
| name | address | phoneNumber |
+--------------+------------+-------------+
| santhosh | chennai | 8825902425 |
| ajaykumar | villupuram | 9597708371 |
| gopikrishnan | mumbai | 2147483647 |
+--------------+------------+-------------+

TRIGGERS(AFTER INSERT):

mysql> create table hotels(hot_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,address varchar(64) default NULL);

mysql> delimiter $$
mysql> create trigger hot_tri_aft
->after insert on hotel
->for each row
->begin
->insert into hotels
->set action='after insert', hot_name=NEW.name, address=NEW.address;
->end$$
mysql> delimiter ;

mysql> select * from hotels;


Empty set (0.00 sec)

mysql> insert into hotel values('103','villupuram','GM_residency','86081875');


mysql> select * from hotels;

+--------------+--------------+----+------------+
| hot_name | action | id | address |
+--------------+--------------+----+------------+
| GM_residency | after insert | 1 | villupuram |
+--------------+--------------+----+------------+
1 row in set (0.00 sec)

mysql> select * from hotel;

+---------+--------------------------------+----------------+-------------+
| hotelID | address | name | phoneNumber |
+---------+--------------------------------+----------------+-------------+
| 100 | 27 Timber Dr, Garner, NC 27529 | WolfVilla | 63425875 |
| 101 | chennai | sky_residency | 93658748 |
| 102 | mumbai | gopi_residency | 88259024 |
| 103 | villupuram | GM_residency | 86081875 |
+---------+--------------------------------+----------------+-------------+
4 rows in set (0.00 sec)

TRIGGERS(BEFORE INSERT):

mysql> create table hotel1(hot_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,address varchar(64) default NULL);

mysql> delimiter $$
mysql> create trigger hot_tri_bft
->before insert on hotel
->for each row
->begin
->insert into hotel
->set action='before insert', hot_name=NEW.name, address=NEW.address;
->end$$
mysql> delimiter ;

mysql> select * from hotel1;


Empty set (0.00 sec)
mysql> insert into hotel(hotelID,address,name,phoneNumber)
values('104','pondy','Gold_residency','79042556');
mysql> select * from hotel1;

+----------------+---------------+----+---------+
| hot_name | action | id | address |
+----------------+---------------+----+---------+
| Gold_residency | before insert | 1 | pondy |
+----------------+---------------+----+---------+
1 row in set (0.00 sec)
mysql> select * from hotel;

+---------+--------------------------------+----------------+-------------+
| hotelID | address | name | phoneNumber |
+---------+--------------------------------+----------------+-------------+
| 100 | 27 Timber Dr, Garner, NC 27529 | WolfVilla | 63425875 |
| 101 | chennai | sky_residency | 93658748 |
| 102 | mumbai | gopi_residency | 88259024 |
| 103 | villupuram | GM_residency | 86081875 |
| 104 | pondy | Gold_residency | 79042556 |
+---------+--------------------------------+----------------+-------------+
5 rows in set (0.00 sec)
TRIGGERS(AFTER DELETE):

mysql> create table hotel_daf(hot_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,address varchar(64) default NULL);

mysql> delimiter $$
mysql> create trigger hot_tri_daf
->after delete on hotel
->for each row
->begin
->insert into hotel_daf
->set action='after delete', hot_name=OLD.name, address=NEW.address;
->end$$
mysql> delimiter ;
mysql> select * from hotel_daf;
Empty set (0.00 sec)

mysql> delete from hotel where hotelID='104';


mysql> select * from hotel_df;

+----------------+--------------+----+---------+
| hot_name | action | id | address |
+----------------+--------------+----+---------+
| Gold_residency | after delete | 1 | pondy |
+----------------+--------------+----+---------+
1 row in set (0.00 sec)

mysql> select * from hotel;

+---------+--------------------------------+----------------+-------------+
| hotelID | address | name | phoneNumber |
+---------+--------------------------------+----------------+-------------+
| 100 | 27 Timber Dr, Garner, NC 27529 | WolfVilla | 63425875 |
| 101 | chennai | sky_residency | 93658748 |
| 102 | mumbai | gopi_residency | 88259024 |
| 103 | villupuram | GM_residency | 86081875 |
+---------+--------------------------------+----------------+-------------+
4 rows in set (0.00 sec)

TRIGGERS(BEFORE DELETE):

mysql> create table hotel_dbf(hot_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,address varchar(64) default NULL);
Query OK, 0 rows affected (0.24 sec)

mysql> delimiter $$
mysql> create trigger hot_tri_dbf
->before delete on hotel
->for each row
->begin
->insert into hotel_dbf
->set action='before delete', hot_name=OLD.name, address=OLD.address;
->end$$
Query OK, 0 rows affected (0.06 sec)
mysql> delimiter ;
mysql> select * from hotel;

+---------+--------------------------------+----------------+-------------+
| hotelID | address | name | phoneNumber |
+---------+--------------------------------+----------------+-------------+
| 100 | 27 Timber Dr, Garner, NC 27529 | WolfVilla | 63425875 |
| 101 | chennai | sky_residency | 93658748 |
| 102 | mumbai | gopi_residency | 88259024 |
| 103 | villupuram | GM_residency | 86081875 |
+---------+--------------------------------+----------------+-------------+
4 rows in set (0.00 sec)

mysql> select * from hotel_dbf;


Empty set (0.00 sec)

mysql> delete from hotel where hotelID='103';


Query OK, 1 row affected (0.05 sec)

mysql> select * from hotel_dbf;

+--------------+---------------+----+------------+
| hot_name | action | id | address |
+--------------+---------------+----+------------+
| GM_residency | before delete | 1 | villupuram |
+--------------+---------------+----+------------+
1 row in set (0.00 sec)

TRIGGERS(AFTER UPDATE):

mysql> create table hotel_au(hot_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,address varchar(64) default NULL);

mysql> delimiter $$
mysql> create trigger hot_tri_au
->after update on hotel
->for each row
->begin
->insert into hotel_au
->set action='after update', hot_name=NEW.name, address=NEW.address;
->end$$
mysql> delimiter ;

mysql> select * from hotel_au;


Empty set (0.00 sec)

mysql> select * from hotel;

+---------+--------------------------------+----------------+-------------+
| hotelID | address | name | phoneNumber |
+---------+--------------------------------+----------------+-------------+
| 100 | 27 Timber Dr, Garner, NC 27529 | WolfVilla | 63425875 |
| 101 | chennai | sky_residency | 93658748 |
| 102 | mumbai | gopi_residency | 88259024 |
+---------+--------------------------------+----------------+-------------+

mysql> update hotel set name='Chennai_residency' where hotelID='100';


Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from hotel_au;

+-------------------+--------------+----+--------------------------------+
| hot_name | action | id | address |
+-------------------+--------------+----+--------------------------------+
| Chennai_residency | after update | 1 | 27 Timber Dr, Garner, NC 27529 |
+-------------------+--------------+----+--------------------------------+
1 row in set (0.00 sec)

mysql> select * from hotel;

+---------+--------------------------------+-------------------+-------------+
| hotelID | address | name | phoneNumber |
+---------+--------------------------------+-------------------+-------------+
| 100 | 27 Timber Dr, Garner, NC 27529 | Chennai_residency | 63425875 |
| 101 | chennai | sky_residency | 93658748 |
| 102 | mumbai | gopi_residency | 88259024 |
+---------+--------------------------------+-------------------+-------------+
3 rows in set (0.00 sec)

TRIGGERS(BEFORE UPDATE):

mysql> create table hotel_bu(hot_name varchar(64) default NULL,action varchar(40) default NULL,id int
auto_increment primary key,address varchar(64) default NULL,change_date DATETIME default NULL);
Query OK, 0 rows affected (0.28 sec)

mysql> delimiter $$
mysql> create trigger hot_tri_bu
->before update on hotel
->for each row
->begin insert into hotel_bu
->set action='before update', hot_name=OLD.name, address=OLD.address,change_date=NOW();
-> end$$
Query OK, 0 rows affected (0.05 sec)
mysql> delimiter ;
mysql> select * from hotel_bu;
Empty set (0.00 sec)
mysql> select * from hotel;

+---------+--------------------------------+-------------------+-------------+
| hotelID | address | name | phoneNumber |
+---------+--------------------------------+-------------------+-------------+
| 100 | 27 Timber Dr, Garner, NC 27529 | Chennai_residency | 63425875 |
| 101 | chennai | sky_residency | 93658748 |
| 102 | mumbai | gopi_residency | 88259024 |
+---------+--------------------------------+-------------------+-------------+

mysql> update hotel set address='madurai' where hotelID='100';


Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from hotel_bu;

+-------------------+---------------+----+--------------------------------+---------------------+
| hot_name | action | id | address | change_date |
+-------------------+---------------+----+--------------------------------+---------------------+
| Chennai_residency | before update | 1 | 27 Timber Dr, Garner, NC 27529 | 2019-03-26 16:24:47 |
+-------------------+---------------+----+--------------------------------+---------------------+
1 row in set (0.00 sec)
mysql> select * from hotel;

+---------+---------+-------------------+-------------+
| hotelID | address | name | phoneNumber |
+---------+---------+-------------------+-------------+
| 100 | madurai | Chennai_residency | 63425875 |
| 101 | chennai | sky_residency | 93658748 |
| 102 | mumbai | gopi_residency | 88259024 |
+---------+---------+-------------------+-------------+
3 rows in set (0.00 sec)

STORED FUNCTIONS:

mysql> delimiter $$
mysql> create function charges_amt(amount double) returns varchar(20)
-> DETERMINISTIC
-> BEGIN
-> DECLARE charge varchar(20);
-> IF amount>20000 THEN
-> SET charge='MAX Amount';
-> ELSEIF(amount<=20000 AND amount>=5000) THEN
-> SET charge='AVG Amount';
-> ELSEIF(amount<5000) THEN
-> SET charge='MIN Amount';
-> END IF;
-> return(charge);
-> END$$
Query OK, 0 rows affected (0.35 sec)

mysql> delimiter ;

mysql> select staying_days,charges_amt(charges) from charges;

+--------------+----------------------+
| staying_days | charges_amt(charges) |
+--------------+----------------------+
| 10 | AVG Amount |
| 15 | MAX Amount |
| 5 | MIN Amount |
+--------------+----------------------+
3 rows in set (0.00 sec)

STORED PROCEDURE WITHOUT PARAMETER:

mysql> delimiter $$
mysql> create procedure getalladdress()
-> BEGIN
-> select address from contact_details;
-> END$$
mysql> delimiter ;
mysql> call getalladdress;

+------------+
| address |
+------------+
| chennai |
| villupuram |
| mumbai |
+------------+
3 rows in set (0.00 sec)
STORED PROCEDURE WITH PARAMETER:

mysql> delimiter $$
mysql> create procedure conta_in(IN a int)
-> BEGIN
-> select * from contact_details limit a;
-> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call conta_in(2);

+-----------+------------+-------------+
| name | address | phoneNumber |
+-----------+------------+-------------+
| santhosh | chennai | 8825902425 |
| ajaykumar | villupuram | 2147483647 |
+-----------+------------+-------------+
2 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

CURSOR:

mysql> delimiter $$
mysql> create procedure name_list(INOUT n_list varchar(4000))
-> begin
-> declare done integer default 0;
-> declare h_name varchar(100) default "";
-> declare cur2 cursor for
-> select name from hotel;
-> declare continue handler
-> for not found set done=1;
-> open cur2;
-> get_name: loop
-> fetch cur2 into h_name;
-> if done=1 then
-> leave get_name;
-> end if;
-> set n_list=concat(h_name,";",n_list);
-> end loop get_name;
-> close cur2;
-> end$$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> set @n_list="";
Query OK, 0 rows affected (0.00 sec)

mysql> call name_list(@n_list);


Query OK, 0 rows affected (0.00 sec)

mysql> select @n_list;

+-------------------------------------------------+
| @n_list |
+-------------------------------------------------+
| gopi_residency;sky_residency;Chennai_residency; |
+-------------------------------------------------+
1 row in set (0.00 sec)
INDEX:

mysql>create index ind on customer (name,phoneNubmer);


Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0s

mysql>show index from customer;

Table Non_unique Key_name Seq_i Column_na Collation Cardinali Sub_part Packed Null Index_typ
n_ind me ty e
ex

Customer 0 PRIMARY 1 A A NULL NULL NULL BTREE

Customer 1 Id 1 A 3 NULL NULL NULL BTREE

Customer 1 Ind 1 A 3 NULL NULL YES BTREE

Customer 1 Ind 2 A 3 NULL NULL YES BTREE

RESULT
EX,NO: LIBRARY MANAGEMENT SYSTEM

DATE:

AIM:

CREATION OF DATABASE:

mysql> create database library;


Query OK, 1 row affected (0.00 sec)
mysql> use library;
Database changed
mysql> create table book(acc_no int(30),ISBN int(20),title varchar(50),type varchar(20),author
varchar(50),quantity int(11),price int(11),primary key(ISBN));
Query OK, 0 rows affected (0.26 sec)
mysql> desc book;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| acc_no | int(30) | YES | | NULL | |
| ISBN | int(20) | NO | PRI | NULL | |
| title | varchar(50) | YES | | NULL | |
| type | varchar(20) | YES | | NULL | |
| author | varchar(50) | YES | | NULL | |
| quantity | int(11) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
7 rows in set (0.01 sec)

mysql> insert into book values


('1','100001','DATABASE SYSTEM CONCEPTS','TEXT BOOK','SILBERSCHATZ','10','480'),
('2','105671','THE COMPLETE REFERENCE JAVA','TEXT BOOK','HERBERT SCHILDT','8','560'),
('3','108965','INTELLIGENT COMPLEX ADAPTIVE SYSTEMS','TEXT BOOK','ANG YANG','5','320');
Query OK, 3 row affected (0.44 sec)
mysql> SELECT * FROM book;
+--------+--------+--------------------------------------+-----------+-----------------+----------+-------+
| acc_no | ISBN | title | type | author | quantity | price |
+--------+--------+--------------------------------------+-----------+-----------------+----------+-------|
| 1 | 100001 | DATABASE SYSTEM CONCEPTS | TEXT BOOK | SILBERSCHATZ | 10 | 480 |
| 2 | 105671 | THE COMPLETE REFERENCE JAVA | TEXT BOOK | HERBERT SCHILDT | 8 | 560 |
| 3 | 108965 | INTELLIGENT COMPLEX ADAPTIVE SYSTEMS | TEXT BOOK | ANG YANG | 5 | 320 |
+--------+--------+--------------------------------------+-----------+-----------------+----------+-------+
3 rows in set (0.00 sec)
mysql> create table staff (staff_id int(35),name varchar(50),designation varchar(35),primary key(staff_id));
Query OK, 0 rows affected (0.60 sec)
mysql> desc staff;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| staff_id | int(35) | NO | PRI | NULL | |
| name | varchar(50) | YES | | NULL | |
| designation | varchar(35) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into staff values('101','KUMAR','MANAGER'),
('102','SANKAR','ASSISSTANT MANAGER'),
('103','SARAVANAN','WORKER');
Query OK, 3 row affected (0.42 sec)
mysql> SELECT * from staff;
+----------+-----------+--------------------+
| staff_id | name | designation |
+----------+-----------+--------------------+
| 101 | KUMAR | MANAGER |
| 102 | SANKAR | ASSISSTANT MANAGER |
| 103 | SARAVANAN | WORKER |
+----------+-----------+--------------------+
3 rows in set (0.00 sec)

mysql> create table student(stu_id int(35),name varchar(50),dept varchar(25),roll_no int(25),adress


varchar(56),primary key(stu_id,roll_no));
Query OK, 0 rows affected (0.71 sec)
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stu_id | int(35) | NO | PRI | NULL | |
| name | varchar(50) | YES | | NULL | |
| dept | varchar(25) | YES | | NULL | |
| roll_no | int(25) | NO | PRI | NULL | |
| adress | varchar(56) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into student values('001','ABI','IT','422517205001','THIRUVANNAMALAI'),
('005','ARUN','CSE','422517204005','CHENNAI'),
('001','ARAVINTH','CSE','422517204001','VILLUPURAM'),
('004','Akash','MECH','422517203004','VILLUPURAM');
Query OK, 4 row affected (0.44 sec)
mysql> SELECT * from student;
+--------+----------+------+--------------+-----------------+
| stu_id | name | dept | roll_no | adress |
+--------+----------+------+--------------+-----------------+
| 001 | ARAVINTH | CSE | 422517204001 | VILLUPURAM |
| 001 | ABI | IT | 422517205001 | THIRUVANNAMALAI |
| 004 | Akash | MECH | 422517203004 | VILLUPURAM |
| 005 | ARUN | CSE | 422517204005 | CHENNAI |
+--------+----------+------+--------------+-----------------+
4 rows in set (0.00 sec)
mysql> create table issue_status(is_id int(11),stu_id int(35),stu_name varchar(50),stu_dept
varchar(25),ISBN int(20),book_title varchar(50),is_date date,due_date date,primary
key(is_id),foreign key(stu_id) references student(stu_id),foreign key(ISBN) references book(ISBN));
Query OK, 0 rows affected (0.70 sec)
mysql> desc issue_status;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| is_id | int(11) | NO | PRI | NULL | |
| stu_id | int(35) | YES | MUL | NULL | |
| stu_name | varchar(50) | YES | | NULL | |
| stu_dept | varchar(25) | YES | | NULL | |
| ISBN | int(20) | YES | MUL | NULL | |
| book_title | varchar(50) | YES | | NULL | |
| is_date | date | YES | | NULL | |
| due_date | date | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
mysql> insert into issue_status values
('1001','001','ABI','IT','105671','THE COMPLETE REFERENCE JAVA','2019-03-12','2019-03-27'),
('1002','005','ARUN','CSE','108965','INTELLIGENT COMPLEX ADAPTIVE','2019-03-15','2019-03-30'),
('1003','001','ARAVINTH','CSE','100001','DATABASE SYSTEM CONCEPTS','2019-03-16','2019-03-31');
Query OK, 3 row affected (0.45 sec)
mysql> SELECT * from issue_status;
+-------+--------+----------+----------+--------+--------------------------------------+------------+------------+
| is_id | stu_id | stu_name | stu_dept | ISBN | book_title | is_date | due_date |
+-------+--------+----------+----------+--------+--------------------------------------+------------+------------+
| 1001 | 001 | ABI | IT | 105671 | THE COMPLETE REFERENCE JAVA | 2019-03-12 | 2019-03-27 |
| 1002 | 005 | ARUN | CSE | 108965 | INTELLIGENT COMPLEX ADAPTIVE SYSTEMS | 2019-03-15 | 2019-03-30 |
| 1003 | 001 | ARAVINTH | CSE | 100001 | DATABASE SYSTEM CONCEPTS | 2019-03-16 | 2019-03-31 |
+-------+--------+----------+----------+--------+--------------------------------------+------------+------------+
3 rows in set (0.00 sec)
mysql> alter table issue_status add rool_no int(25);
Query OK, 0 rows affected (1.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table issue_status drop column rool_no;
Query OK, 0 rows affected (1.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table issue_status add roll_no int(25);
Query OK, 0 rows affected (1.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update issue_status set roll_no='422517204001' where is_id='1003';
Query OK, 1 row affected (0.44 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update issue_status set roll_no='422517205001' where is_id='1001';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update issue_status set roll_no='422517204005' where is_id='1002';
Query OK, 1 row affected (0.42 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from issue_status;
+-------+--------+----------+----------+--------+--------------------------------------+------------+------------+--------------+
| is_id | stu_id | stu_name | stu_dept | ISBN | book_title | is_date | due_date | roll_no |
+-------+--------+----------+----------+--------+--------------------------------------+------------+------------+--------------+
| 1001 | 001 | ABI | IT | 105671 | THE COMPLETE REFERENCE JAVA | 2019-03-12 | 2019-03-27 | 422517205001 |
| 1002 | 005 | ARUN | CSE | 108965 | INTELLIGENT COMPLEX ADAPTIVE SYSTEMS | 2019-03-15 | 2019-03-30 | 422517204005 |
| 1003 | 001 | ARAVINTH | CSE | 100001 | DATABASE SYSTEM CONCEPTS | 2019-03-16 | 2019-03-31 | 422517204001 |
+-------+--------+----------+----------+--------+--------------------------------------+------------+------------+--------------+
3 rows in set (0.00 sec)
mysql> create table return_status(re_id int(11),is_stu_id int(35),ISBN int(20),is_book_title
varchar(50),is_date date,re_date date,fine numeric(8,2),primary key(re_id),foreign key(is_stu_id)
references issue_status(stu_id),foreign key(ISBN) references issue_status(ISBN));
Query OK, 0 rows affected (0.71 sec)
mysql> desc return_status;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| re_id | int(11) | NO | PRI | NULL | |
| is_stu_id | int(35) | YES | MUL | NULL | |
| ISBN | int(20) | YES | MUL | NULL | |
| is_book_title | varchar(50) | YES | | NULL | |
| is_date | date | YES | | NULL | |
| re_date | date | YES | | NULL | |
| fine | numeric(8,2)| YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> insert into return_status values
('1001','001','100001','DATABASE SYSTEM CONCEPTS','2019-03-16','2019-03-21','0.00'),
('1002','005','108965','INTELLIGENT COMPLEX ADAPTIVE SYSTEMS ','2019-03-15','2019-03-22','0.00'),
('1003','001','105671','THE COMPLETE REFERENCE JAVA','2019-03-12','2019-03-24','0.00');
mysql> select * from return_status;
+-------+-----------+--------+---------------------------------------+------------+------------+------+
| re_id | is_stu_id | ISBN | is_book_title | is_date | re_date | fine |
+-------+-----------+--------+---------------------------------------+------------+------------+------+
| 1001 | 001 | 100001 | DATABASE SYSTEM CONCEPTS | 2019-03-16 | 2019-03-21 | 0.00 |
| 1002 | 005 | 108965 | INTELLIGENT COMPLEX ADAPTIVE SYSTEMS | 2019-03-15 | 2019-03-22 | 0.00 |
| 1003 | 001 | 105671 | THE COMPLETE REFERENCE JAVA | 2019-03-12 | 2019-03-24 | 0.00 |
+-------+-----------+--------+---------------------------------------+------------+------------+------+
3 rows in set (0.00 sec)
mysql> alter table return_status add roll_no int(25);
Query OK, 0 rows affected (0.69 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update return_status set roll_no='422517204005' where re_id='1002';
Query OK, 1 row affected (0.43 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update return_status set roll_no='422517204001' where re_id='1001';
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update return_status set roll_no='422517205001' where re_id='1003';
Query OK, 1 row affected (0.44 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from return_status;
+-------+-----------+--------+---------------------------------------+------------+------------+------+-------------+
| re_id | is_stu_id | ISBN | is_book_title | is_date | re_date | fine |roll_no |
+-------+-----------+--------+---------------------------------------+------------+------------+------+-------------+
| 1001 | 001 | 100001 | DATABASE SYSTEM CONCEPTS | 2019-03-16 | 2019-03-21 | 0.00 |422517204001 |
| 1002 | 005 | 108965 | INTELLIGENT COMPLEX ADAPTIVE SYSTEMS | 2019-03-15 | 2019-03-22 | 0.00 |422517204005 |
| 1003 | 001 | 105671 | THE COMPLETE REFERENCE JAVA | 2019-03-12 | 2019-03-24 | 0.00 |422517205001 |
+-------+-----------+--------+---------------------------------------+------------+------------+------+-------------+
3 rows in set (0.00 sec)

VIEWS:

mysql> create view book_price as select title as BOOK_NAME,price as PRICE from book;
Query OK, 0 rows affected (0.42 sec)
mysql> select * from book_price;
+--------------------------------------+-------+
| BOOK_NAME | PRICE |
+--------------------------------------+-------+
| DATABASE SYSTEM CONCEPTS | 480 |
| THE COMPLETE REFERENCE JAVA | 560 |
| INTELLIGENT COMPLEX ADAPTIVE SYSTEMS | 320 |
+--------------------------------------+-------+
3 rows in set (0.00 sec)
mysql> create view retuned_books as select ISBN,is_book_title as BOOK_NAME from return_status;
Query OK, 0 rows affected (0.05 sec)
mysql> select * from retuned_books;
+--------+---------------------------------------+
| ISBN | BOOK_NAME |
+--------+---------------------------------------+
| 100001 | DATABASE SYSTEM CONCEPTS |
| 108965 | INTELLIGENT COMPLEX ADAPTIVE SYSTEMS |
| 105671 | THE COMPLETE REFERENCE JAVA |
+--------+---------------------------------------+
3 rows in set (0.00 sec)

TRIGGERS(BEFORE INSERT):

mysql> create table book(acc_no varchar(30),ISBN varchar(20),title varchar(50),type varchar(20),author


varchar(50),quantity int(11),price int(11),primary key(ISBN));
Query OK, 0 rows affected (0.27 sec)
mysql> delimiter $$
mysql> CREATE TRIGGER quantitycheck BEFORE INSERT ON book FOR EACH ROW IF NEW.quantity < 0 THEN
SET NEW.quantity = 1; END IF;$$
Query OK, 0 rows affected (0.10 sec)
mysql>delimiter ;
mysql> insert into book values
('1','100001','DATABASE SYSTEM CONCEPTS','TEXT BOOK','SILBERSCHATZ','0','480');
Query OK, 1 row affected (0.07 sec)
mysql> insert into book values
('2','105671','THE COMPLETE REFERENCE JAVA','TEXT BOOK','HERBERT SCHILDT','0','560');
Query OK, 1 row affected (0.44 sec)
mysql> select * from book;
+--------+--------+-----------------------------+-----------+-----------------+----------+-------+
| acc_no | ISBN | title | type | author | quantity | price |
+--------+--------+-----------------------------+-----------+-----------------+----------+-------+
| 1 | 100001 | DATABASE SYSTEM CONCEPTS | TEXT BOOK | SILBERSCHATZ | 1 | 480 |
| 2 | 105671 | THE COMPLETE REFERENCE JAVA | TEXT BOOK | HERBERT SCHILDT | 1 | 560 |
+--------+--------+-----------------------------+-----------+-----------------+----------+-------+
2 rows in set (0.00 sec)

TRIGGERS(BEFORE UPDATE):
mysql> delimiter $$
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON book
-> FOR EACH ROW
-> begin
-> IF NEW.quantity <= 0 THEN
-> SET NEW.quantity=1;
-> ELSEIF NEW.quantity > 50 THEN
-> SET NEW.quantity = 50;
-> end if;
-> end $$
Query OK, 0 rows affected (0.10 sec)
mysql>delimiter ;
mysql> update book set quantity='0' where ISBN='100001';
Query OK, 0 rows affected (0.01 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> select * from book;
+--------+--------+-----------------------------+-----------+-----------------+----------+-------+
| acc_no | ISBN | title | type | author | quantity | price |
+--------+--------+-----------------------------+-----------+-----------------+----------+-------+
| 1 | 100001 | DATABASE SYSTEM CONCEPTS | TEXT BOOK | SILBERSCHATZ | 1 | 480 |
| 2 | 105671 | THE COMPLETE REFERENCE JAVA | TEXT BOOK | HERBERT SCHILDT | 1 | 560 |
+--------+--------+-----------------------------+-----------+-----------------+----------+-------+
2 rows in set (0.00 sec)
mysql> update book set quantity='100' where ISBN='105671';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from book;
+--------+--------+-----------------------------+-----------+-----------------+----------+-------+
| acc_no | ISBN | title | type | author | quantity | price |
+--------+--------+-----------------------------+-----------+-----------------+----------+-------+
| 1 | 100001 | DATABASE SYSTEM CONCEPTS | TEXT BOOK | SILBERSCHATZ | 1 | 480 |
| 2 | 105671 | THE COMPLETE REFERENCE JAVA | TEXT BOOK | HERBERT SCHILDT | 50 | 560 |
+--------+--------+-----------------------------+-----------+-----------------+----------+-------+
2 rows in set (0.00 sec)
TRIGGERS(BEFORE DELETE):

mysql> create table delete_book(acc_no varchar(30),ISBN varchar(20),title varchar(50),author


varchar(50),time time,date date);
Query OK, 0 rows affected (0.28 sec)
mysql> delimiter $$
mysql> create trigger delete_book before delete on book for each row begin insert into
delete_book(acc_no,ISBN,title,author,time,date)
values(OLD.acc_no,OLD.ISBN,OLD.title,OLD.author,NOW(),SYSDATE()); end$$
Query OK, 0 rows affected (0.10 sec)
mysql> delimiter ;
mysql> delete from book where acc_no='2';
Query OK, 1 row affected (0.06 sec)
mysql> select * from delete_book;
+--------+--------+-----------------------------+-----------------+----------+------------+
| acc_no | ISBN | title | author | time | date |
+--------+--------+-----------------------------+-----------------+----------+------------+
| 2 | 105671 | THE COMPLETE REFERENCE JAVA | HERBERT SCHILDT | 21:23:48 | 2019-03-26 |
+--------+--------+-----------------------------+-----------------+----------+------------+
2 rows in set (0.00 sec)

TRIGGERS(AFTER INSERT):
mysql> create table book_af(acc_no varchar(30),ISBN varchar(20),title varchar(50),author varchar(50),ACTION
varchar(50));
Query OK, 0 rows affected (0.30 sec)
mysql> delimiter $$
mysql> create trigger after_ins after insert on book for each row begin insert into
book_af(acc_no,ISBN,title,author,ACTION)
values(NEW.acc_no,NEW.ISBN,NEW.title,NEW.author,'AFTER INSERT');
-> end $$
Query OK, 0 rows affected (0.39 sec)
mysql> delimiter ;
mysql> insert into book values
('1','100001','DATABASE SYSTEM CONCEPTS','TEXT BOOK','SILBERSCHATZ','0','480');
Query OK, 1 row affected (0.13 sec)
mysql> insert into book values
('2','105671','THE COMPLETE REFERENCE JAVA','TEXT BOOK','HERBERT SCHILDT','5','560');
Query OK, 1 row affected (0.05 sec)
mysql> select * from book_af;
+--------+--------+-----------------------------+-----------------+--------------+
| acc_no | ISBN | title | author | ACTION |
+--------+--------+-----------------------------+-----------------+--------------+
| 1 | 100001 | DATABASE SYSTEM CONCEPTS | SILBERSCHATZ | AFTER INSERT |
| 2 | 105671 | THE COMPLETE REFERENCE JAVA | HERBERT SCHILDT | AFTER INSERT |
+--------+--------+-----------------------------+-----------------+--------------+
2 rows in set (0.00 sec)

TRIGGERS(AFTER UPDATE):

mysql> create table book_af_up(acc_no varchar(30),ISBN varchar(20),title varchar(50),author


varchar(50),ACTION varchar(50));
Query OK, 0 rows affected (0.30 sec)
mysql> delimiter $$
mysql> create trigger after_upt after update on book for each row begin insert into
book_af_up(acc_no,ISBN,title,author,ACTION)
values(NEW.acc_no,NEW.ISBN,NEW.title,NEW.author,'AFTER UPDATE');
-> END$$
Query OK, 0 rows affected (0.08 sec)
mysql> delimiter ;
mysql> UPDATE book set ISBN='104562' where acc_no='1';
Query OK, 1 row affected (0.36 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> UPDATE book set title='THE COMPLETE REFERENCE JAVA 7E' where ISBN='105671';
Query OK, 1 row affected (0.09 sec)
mysql> SELECT * from book_af_up;
+--------+--------+--------------------------------+-----------------+--------------+
| acc_no | ISBN | title | author | ACTION |
+--------+--------+--------------------------------+-----------------+--------------+
| 1 | 104562 | DATABASE SYSTEM CONCEPTS | SILBERSCHATZ | AFTER UPDATE |
| 2 | 105671 | THE COMPLETE REFERENCE JAVA 7E | HERBERT SCHILDT | AFTER UPDATE |
+--------+--------+--------------------------------+-----------------+--------------+
2 rows in set (0.00 sec)

TRIGGERS(AFTER DELETE):

mysql> create table book_af_de(acc_no varchar(30),ISBN varchar(20),title varchar(50),author


varchar(50),ACTION varchar(50),date date,time time);
Query OK, 0 rows affected (0.29 sec)
mysql>delimiter //
mysql> create trigger af_delete_book after delete on book for each row begin insert into
book_af_de(acc_no,ISBN,title,author,ACTION,time,date)
values(OLD.acc_no,OLD.ISBN,OLD.title,OLD.author,'AFTER DELETE',NOW(),SYSDATE());
-> end//
Query OK, 0 rows affected (0.10 sec)
mysql> delimiter ;
mysql> delete from book where acc_no='1';
Query OK, 1 row affected (0.26 sec)
mysql> delete from book where acc_no='3';
Query OK, 1 row affected (0.17 sec)
mysql> select * from book_af_de;
+--------+--------+--------------------------------------+--------------+---------------+------------+----------+
| acc_no | ISBN | title | author | ACTION | date | time |
+--------+--------+--------------------------------------+--------------+---------------+------------+----------+
| 1 | 104562 | DATABASE SYSTEM CONCEPTS | SILBERSCHATZ | AFTER DELETE | 2019-03-26 | 22:24:14 |
| 3 | 108965 | INTELLIGENT COMPLEX ADAPTIVE SYSTEMS | ANG YANG | AFTER DELETE | 2019-03-26 | 22:25:07 |
+--------+--------+--------------------------------------+--------------+---------------+------------+----------+
2 rows in set (0.01 sec)

STORED FUNCTION:

mysql> delimiter $$
mysql> create function dept_id(dept_name varchar(50)) returns varchar(20)
-> deterministic
-> begin
-> declare dept_id varchar(20);
-> if dept_name='IT' then
-> set dept_id='205';
-> elseif dept_name='CSE' then
-> set dept_id='204';
-> elseif dept_name='MECH' then
-> set dept_id='203';
-> end if;
-> return(dept_id);
-> end$$
Query OK, 0 rows affected (0.00 sec)
mysql>delimiter ;
mysql> select dept,dept_id(dept) from student;
+------+---------------+
| dept | dept_id(dept) |
+------+---------------+
| CSE | 204 |
| IT | 205 |
| MECH | 203 |
| CSE | 204 |
+------+---------------+
4 rows in set (0.00 sec)

Stored Procedure(Without Parameter):

mysql> delimiter $$
mysql> create procedure getavailablebook()
-> select title as AVAIL_BOOK,price as PRICE from book;
-> $$
Query OK, 0 rows affected (0.08 sec)
mysql> delimiter ;
mysql> call getavailablebook;
+--------------------------------------+-------+
| AVAIL_BOOK | PRICE |
+--------------------------------------+-------+
| DATABASE SYSTEM CONCEPTS | 480 |
| THE COMPLETE REFERENCE JAVA 7E | 560 |
| INTELLIGENT COMPLEX ADAPTIVE SYSTEMS | 320 |
+--------------------------------------+-------+
3 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

STORED PROCEDURE(WITH PARAMETER):

mysql> delimiter $$
mysql> create procedure staffs(IN b varchar(35))
-> begin
-> select * from staff where designation=b;
-> end$$
Query OK, 0 rows affected (0.00 sec)
mysql>delimiter ;
mysql> call staffs('ASSISSTANT MANAGER');
+----------+--------+--------------------+
| staff_id | name | designation |
+----------+--------+--------------------+
| 102 | SANKAR | ASSISSTANT MANAGER |
+----------+--------+--------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

CURSOR:

mysql> delimiter $$
mysql> create procedure book_list(INOUT n_list varchar(4000))
-> begin
-> declare finish integer default 0;
-> declare book_name varchar(100) default "";
-> declare book_cursor cursor for
-> select title from book;
-> declare continue handler for not found set finish=1;
-> open book_cursor;
-> get_name: loop
-> fetch book_cursor into boo_name;
-> if finish=1 then
-> leave get_name;
-> end if;
-> set n_list=concat(book_name,";",n_list);
-> end loop get_name;
-> close book_cursor;
-> end$$
Query OK, 0 rows affected (0.39 sec)
mysql> delimiter ;
mysql> set @n_list="";
Query OK, 0 rows affected (0.00 sec)
mysql> call book_list(@n_list);
Query OK, 0 rows affected (0.04 sec)
mysql> select @n_list AS BOOK_LIST;
+-----------------------------------------------------------------------------------------------+
| BOOK_LIST |
+-----------------------------------------------------------------------------------------------+
| INTELLIGENT COMPLEX ADAPTIVE SYSTEMS;THE COMPLETE REFERENCE JAVA 7E;DATABASE SYSTEM CONCEPTS; |
+-----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

INDEX:

mysql> create index ind on book(ISBN,title);


Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
show indexes from book;

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| book | 0 | PRIMARY | 1 | ISBN | A | 1 | NULL | NULL | | BTREE | |
| book | 1 | ind | 1 | ISBN | A | 1 | NULL | NULL | | BTREE | |
| book | 1 | ind | 2 | title | A | 1 | NULL | NULL | YES | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.00 sec)

RESULT:
EX.NO: RETAIL SHOP DATABASE MANAGEMENT SYSTEM

DATE:

AIM:

CREATION OF DATABASE:

mysql> create database retailshop;


Query OK, 1 row affected (0.00 sec)
mysql> use retailshop;
Database changed
mysql> create table dept(Dept_id int NOT NULL,Dept_name varchar(25),Floor_no varchar(25),primary
key(Dept_id));
Query OK, 0 rows affected (0.49 sec)
mysql> desc dept;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Dept_id | int(11) | NO | PRI | NULL | |
| Dept_name | varchar(25) | YES | | NULL | |
| Floor_no | varchar(25) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into dept(Dept_id,Dept_name,Floor_no) values('101','Cosmetics','2nd Floor');
Query OK, 1 row affected (0.06 sec)
mysql> insert into dept values('102','Snacks','1st Floor'),('103','Vegetables','3rdFloor'),
('104','Giftsection','2nd Floor'),('105','Fruits section','Ground Floor');
Query OK, 4 row affected (0.44 sec)
mysql> select * from dept;
+---------+----------------+--------------+
| Dept_id | Dept_name | Floor_no |
+---------+----------------+--------------+
| 101 | Cosmetics | 2nd Floor |
| 102 | Snacks | 1st Floor |
| 103 | Vegetables | 3rd Floor |
| 104 | Gift section | 2nd Floor |
| 105 | Fruits section | Ground Floor |
+---------+----------------+--------------+
5 rows in set (0.00 sec)
mysql> create table employee(Emp_id int NOT NULL,Emp_name varchar(25),Dept_id int,Address
varchar(30),primary key(Emp_id),foreign key(Dept_id)references dept(Dept_id));
Query OK, 0 rows affected (0.32 sec)
mysql> desc employee;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| Emp_id | int(11) | NO | PRI | NULL | |
| Emp_name | varchar(25) | YES | | NULL | |
| Dept_id | int(11) | YES | MUL | NULL | |
| Address | varchar(30) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into employee(Emp_id,Emp_name,Dept_id,Address)
Values('20031','D.Vijay','103','Netaji nagar');
Query OK, 1 row affected (0.04 sec)
mysql> insert into employee Values('20032','S.Fathima','101','Savitha Apartments'),
('20033','K.Lakshmanan','105','Rajeev Gandhi street'),('20034','G.Raveena','104','EB Colony'),
('20035','T.Nataraj','102','St Thomas Road');
Query OK, 4 row affected (0.44 sec)
mysql> select * from employee;
+--------+--------------+---------+----------------------+
| Emp_id | Emp_name | Dept_id | Address |
+--------+--------------+---------+----------------------+
| 20031 | D.Vijay | 103 | Netaji nagar |
| 20032 | S.Fathima | 101 | Savitha Apartments |
| 20033 | K.Lakshmanan | 105 | Rajeev Gandhi street |
| 20034 | G.Raveena | 104 | EB Colony |
| 20035 | T.Nataraj | 102 | St Thomas Road |
+--------+--------------+---------+----------------------+
5 rows in set (0.00 sec)
mysql> create table customer(Cust_Id int NOT NULL,Cust_name varchar(25),Emp_id int,Cust_email
varchar(30),primary key(Cust_id),foreign key(Emp_id)references employee(Emp_id));
Query OK, 0 rows affected (0.34 sec)
mysql> desc customer;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Cust_Id | int(11) | NO | PRI | NULL | |
| Cust_name | varchar(25) | YES | | NULL | |
| Emp_id | int(11) | YES | MUL | NULL | |
| Cust_email | varchar(30) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> insert into customer(Cust_id,Cust_name,Emp_id,Cust_email)
values('8001','F.Gomathi','20034','gomathi29@gmail');
Query OK, 1 row affected (0.05 sec)
mysql> insert into customer values('8002','P.Chidambaram','20032','chidambaram@gmail'),
('8003','R.Seetha','20031','seetharaman@gmail'),
('8004','F.Jenifer','20033','jeniferfrancis@gmail'),
('8005','R.Kavya','20035','kavyashankar@gmail');
Query OK, 4 row affected (0.44 sec)
mysql> select * from customer;
+---------+---------------+--------+----------------------+
| Cust_Id | Cust_name | Emp_id | Cust_email |
+---------+---------------+--------+----------------------+
| 8001 | F.Gomathi | 20034 | gomathi29@gmail |
| 8002 | P.Chidambaram | 20032 | chidambaram@gmail |
| 8003 | R.Seetha | 20031 | seetharaman@gmail |
| 8004 | F.Jenifer | 20033 | jeniferfrancis@gmail |
| 8005 | R.Kavya | 20035 | kavyashankar@gmail |
+---------+---------------+--------+----------------------+
5 rows in set (0.00 sec)
mysql> create table item(Product_id int NOT NULL,Product_name varchar(25),Cust_id int,Price int,
primary key(Product_id),foreign key(Cust_id) references customer(Cust_Id));
Query OK, 0 rows affected (0.31 sec)
mysql> desc item;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| Product_id | int(11) | NO | PRI | NULL | |
| Product_name | varchar(25) | YES | | NULL | |
| Cust_id | int(11) | YES | MUL | NULL | |
| Price | int(11) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into item(Product_id,Product_name,Cust_id,Price) values('6001','Eyeliner','8003','120');
Query OK, 1 row affected (0.04 sec)
mysql> insert into item values('6067','Onion','8002','45'),('6150','Biscuit','8005','20'),
('6500','Toy','8004','435'),('6206','Apple','8001','110');
Query OK, 4 row affected (0.44 sec)
mysql> select * from item;
+------------+--------------+---------+-------+
| Product_id | Product_name | Cust_id | Price |
+------------+--------------+---------+-------+
| 6001 | Eyeliner | 8003 | 120 |
| 6067 | Onion | 8002 | 45 |
| 6150 | Biscuit | 8005 | 20 |
| 6206 | Apple | 8001 | 110 |
| 6500 | Toy | 8004 | 435 |
+------------+--------------+---------+-------+
5 rows in set (0.00 sec)
mysql> create table orderdetails(Order_id int NOT NULL,Product_id int,Customer_name varchar(25),
Quantity int,Address varchar(25),primary key(Order_id),
foreign key(Product_id) references item(Product_id));
Query OK, 0 rows affected (0.32 sec)
mysql> desc orderdetails;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| Order_id | int(11) | NO | PRI | NULL | |
| Product_id | int(11) | YES | MUL | NULL | |
| Customer_name | varchar(25) | YES | | NULL | |
| Quantity | int(11) | YES | | NULL | |
| Address | varchar(25) | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into orderdetails(Order_id,Product_id,Customer_name,Quantity,Address)
values('1200','6150','J.Sindhu','20','Thiru vi ka street');
Query OK, 1 row affected (0.05 sec)
mysql> insert into orderdetails values('1201','6206','L.Gayathri','12','Meenakshi nagar'),
('1202','6500','N.Rajavel','45','Chandru colony'),
('1203','6067','R.Shankar','32','Sangeetha apartments'),
('1204','6001','M.Akila','45','Francis hostel');
Query OK, 4 row affected (0.44 sec)
mysql> select * from orderdetails;
+----------+------------+---------------+----------+----------------------+
| Order_id | Product_id | Customer_name | Quantity | Address |
+----------+------------+---------------+----------+----------------------+
| 1200 | 6150 | J.Sindhu | 20 | Thiru vi ka street |
| 1201 | 6206 | L.Gayathri | 12 | Meenakshi nagar |
| 1202 | 6500 | N.Rajavel | 45 | Chandru colony |
| 1203 | 6067 | R.Shankar | 32 | Sangeetha apartments |
| 1204 | 6001 | M.Akila | 45 | Francis hostel |
+----------+------------+---------------+----------+----------------------+
5 rows in set (0.00 sec)
mysql> update customer set Cust_name='N.Seetharaman' where Cust_Id=8003;
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from customer;
+---------+---------------+--------+----------------------+
| Cust_Id | Cust_name | Emp_id | Cust_email |
+---------+---------------+--------+----------------------+
| 8001 | F.Gomathi | 20034 | gomathi29@gmail |
| 8002 | P.Chidambaram | 20032 | chidambaram@gmail |
| 8003 | N.Seetharaman | 20031 | seetharaman@gmail |
| 8004 | F.Jenifer | 20033 | jeniferfrancis@gmail |
| 8005 | R.Kavya | 20035 | kavyashankar@gmail |
+---------+---------------+--------+----------------------+
5 rows in set (0.00 sec)
mysql> delete from orderdetails where Order_id=1204;Product_id
Query OK, 1 row affected (0.05 sec)
mysql> select * from orderdetails;
+----------+------------+---------------+----------+----------------------+
| Order_id | Product_id | Customer_name | Quantity | Address |
+----------+------------+---------------+----------+----------------------+
| 1200 | 6150 | J.Sindhu | 20 | Thiru vi ka street |
| 1201 | 6206 | L.Gayathri | 12 | Meenakshi nagar |
| 1202 | 6500 | N.Rajavel | 45 | Chandru colony |
| 1203 | 6067 | R.Shankar | 32 | Sangeetha apartments |
+----------+------------+---------------+----------+----------------------+
4 rows in set (0.00 sec)
mysql> alter table customer add column Gender varchar(10);
Query OK, 0 rows affected (1.55 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from customer;
+---------+---------------+--------+----------------------+--------+
| Cust_Id | Cust_name | Emp_id | Cust_email | Gender |
+---------+---------------+--------+----------------------+--------+
| 8001 | F.Gomathi | 20034 | gomathi29@gmail | NULL |
| 8002 | P.Chidambaram | 20032 | chidambaram@gmail | NULL |
| 8003 | N.Seetharaman | 20031 | seetharaman@gmail | NULL |
| 8004 | F.Jenifer | 20033 | jeniferfrancis@gmail | NULL |
| 8005 | R.Kavya | 20035 | kavyashankar@gmail | NULL |
+---------+---------------+--------+----------------------+--------+
5 rows in set (0.00 sec)Product_id
mysql> update customer set Gender='Female' where Cust_Id='8001';
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update customer set Gender='Male' where Cust_Id='8002';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update customer set Gender='Male' where Cust_Id='8003';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update customer set Gender='Female' where Cust_Id='8004';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update customer set Gender='Female' where Cust_Id='8005';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from customer;
+---------+---------------+--------+----------------------+--------+
| Cust_Id | Cust_name | Emp_id | Cust_email | Gender |
+---------+---------------+--------+----------------------+--------+
| 8001 | F.Gomathi | 20034 | gomathi29@gmail | Female |
| 8002 | P.Chidambaram | 20032 | chidambaram@gmail | Male |
| 8003 | N.Seetharaman | 20031 | seetharaman@gmail | Male |
| 8004 | F.Jenifer | 20033 | jeniferfrancis@gmail | Female |
| 8005 | R.Kavya | 20035 | kavyashankar@gmail | Female |
+---------+---------------+--------+----------------------+--------+
5 rows in set (0.00 sec)
mysql> alter table employee drop column Address;
Query OK, 0 rows affected (0.71 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from employee;
+--------+--------------+---------+
| Emp_id | Emp_name | Dept_id |
+--------+--------------+---------+
| 20031 | D.Vijay | 103 |
| 20032 | S.Fathima | 101 |
| 20033 | K.Lakshmanan | 105 |
| 20034 | G.Raveena | 104 |
| 20035 | T.Nataraj | 102 |
+--------+--------------+---------+
5 rows in set (0.00 sec)

VIEWS:

mysql> create view Dept_det as select Dept_id,Dept_name from dept;


Query OK, 0 rows affected (0.04 sec)
mysql> select * from Dept_det;
+---------+----------------+
| Dept_id | Dept_name |
+---------+----------------+
| 101 | Cosmetics |
| 102 | Snacks |
| 103 | Vegetables |
| 104 | Gift section |
| 105 | Fruits section |
+---------+----------------+
5 rows in set (0.00 sec)
mysql> create view Emp_det as select Emp_id,Emp_name from employee;
Query OK, 0 rows affected (0.02 sec)
mysql> select * from Emp_det;
+--------+--------------+
| Emp_id | Emp_name |
+--------+--------------+
| 20031 | D.Vijay |
| 20032 | S.Fathima |
| 20033 | K.Lakshmanan |
| 20034 | G.Raveena |
| 20035 | T.Nataraj |
+--------+--------------+
5 rows in set (0.00 sec)
TRIGGERS:

mysql> create table t1(id int auto_increment primary key,Order_id int,Product_id int,
Customer_name varchar(20),Quantity int,Address varchar(20),changedate datetime,action varchar(20));
Query OK, 0 rows affected (0.28 sec)
BEFORE INSERT:

mysql> delimiter //
mysql> create trigger before_shop_insert before insert on orderdetails for each row begin insert into t1
set action='BEFOREINSERT',Order_id=NEW.Order_id,Product_id=NEW.Product_id,
Customer_name=NEW.Customer_name,Quantity=NEW.Quantity,Address=NEW.Address,changedate=NOW();
-> end//
Query OK, 0 rows affected (0.10 sec)
mysql> delimiter ;
mysql> insert into orderdetails values(101,20,'shaki',4,'SS street');
Query OK, 1 row affected (0.04 sec)
mysql> select * from t1;
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| id | Order_id | Product_id | Customer_name | Quantity | Address | changedate | action |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| 1 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:26:08 | BEFORE INSERT |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
1 row in set (0.00 sec)
mysql> insert into orderdetails values(102,13,'indhu',6,'KK street');
Query OK, 1 row affected (0.07 sec)
mysql> select * from t1;
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| id | Order_id | Product_id | Customer_name | Quantity | Address | changedate | action |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| 1 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:26:08 | BEFORE INSERT |
| 2 | 102 | 13 | indhu | 6 | KK street | 2019-03-27 16:26:50 | BEFORE INSERT |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
2 rows in set (0.00 sec)

BEFORE UPDATE:

mysql> delimiter //
mysql> create trigger before_shop_update before update on orderdetails for each row begin insert into t1 then
set action='BEFORE UPDATE',Order_id=OLD.Order_id,Product_id=OLD.Product_id,
Customer_name=OLD.Customer_name,Quantity=OLD.Quantity,Address=OLD.Address,changedate=NOW();
-> end//
Query OK, 0 rows affected (0.10 sec)
mysql> delimiter ;
mysql> update orderdetails set Customer_name='sindhu' where Order_id='101';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t1;
---++----+----------+------------+---------------+----------+-----------+---------------------+-----------+
| id | Order_id | Product_id | Customer_name | Quantity | Address | changedate | action |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| 1 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:26:08 | BEFORE INSERT |
| 2 | 102 | 13 | indhu | 6 | KK street | 2019-03-27 16:26:50 | BEFORE INSERT |
| 3 | 101 | 20 | sindhu | 4 | SS street | 2019-03-27 16:31:40 | BEFORE UPDATE |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
3 rows in set (0.00 sec)
mysql> update orderdetails set Customer_name='manikkam' where Order_id='102';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t1;
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| id | Order_id | Product_id | Customer_name | Quantity | Address | changedate | action |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| 1 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:26:08 | BEFORE INSERT |
| 2 | 102 | 13 | indhu | 6 | KK street | 2019-03-27 16:26:50 | BEFORE INSERT |
| 3 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:31:40 | BEFORE UPDATE |
| 4 | 102 | 13 | manikkam | 6 | KK street | 2019-03-27 16:32:15 | BEFORE UPDATE |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
4 rows in set (0.00 sec)
mysql> select * from orderdetails;
+----------+------------+---------------+----------+-----------+
| Order_id | Product_id | Customer_name | Quantity | Address |
+----------+------------+---------------+----------+-----------+
| 101 | 20 | sindhu | 4 | SS street |
| 102 | 13 | manikkam | 6 | KK street |
+----------+------------+---------------+----------+-----------+
2 rows in set (0.00 sec)

BEFORE DELETE:

mysql> delimiter //
mysql> create trigger before_shop_delete before delete on orderdetails for each row begin insert into t1
set action='BEFOREDELETE',Order_id=OLD.Order_id,Product_id=OLD.Product_id,
Customer_name=OLD.Customer_name,Quantity=OLD.Quantity,Address=OLD.Address,changedate=NOW();
-> end//
Query OK, 0 rows affected (0.10 sec)
mysql> delimiter ;
mysql> select * from orderdetails;
+----------+------------+---------------+----------+-----------+
| Order_id | Product_id | Customer_name | Quantity | Address |
+----------+------------+---------------+----------+-----------+
| 101 | 20 | sindhu | 4 | SS street |
| 102 | 13 | manikkam | 6 | KK street |
+----------+------------+---------------+----------+-----------+
2 rows in set (0.01 sec)
mysql> delete from orderdetails where Order_id='101';
Query OK, 1 row affected (0.08 sec)
mysql> select * from t1;
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| id | Order_id | Product_id | Customer_name | Quantity | Address | changedate | action |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| 1 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:26:08 | BEFORE INSERT |
| 2 | 102 | 13 | indhu | 6 | KK street | 2019-03-27 16:26:50 | BEFORE INSERT |
| 3 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:31:40 | BEFORE UPDATE |
| 4 | 102 | 13 | indhu | 6 | KK street | 2019-03-27 16:32:15 | BEFORE UPDATE |
| 5 | 101 | 20 | sindhu | 4 | SS street | 2019-03-27 16:38:09 | BEFORE DELETE |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
5 rows in set (0.00 sec)

AFTER INSERT:

mysql> delimiter //
mysql> create trigger after_shop_insert after insert on orderdetails for each row begin insert into t1
set action='AFTERINSERT',Order_id=NEW.Order_id,Product_id=NEW.Product_id,
Customer_name=NEW.Customer_name,Quantity=NEW.Quantity,Address=NEW.Address,changedate=NOW(); end//
Query OK, 0 rows affected (0.13 sec)
mysql> delimiter ;
mysql> select * from orderdetails;
+----------+------------+---------------+----------+-----------+
| Order_id | Product_id | Customer_name | Quantity | Address |
+----------+------------+---------------+----------+-----------+
| 102 | 13 | manikkam | 6 | KK street |
+----------+------------+---------------+----------+-----------+
1 row in set (0.00 sec)
mysql> insert into orderdetails values('103',15,'nithya',8,'JJ street');
Query OK, 1 row affected (0.06 sec)
mysql> insert into orderdetails values('104',14,'sandy',10,'MM street');
Query OK, 1 row affected (0.06 sec)
mysql> select * from t1;
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| id | Order_id | Product_id | Customer_name | Quantity | Address | changedate | action |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| 1 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:26:08 | BEFORE INSERT |
| 2 | 102 | 13 | indhu | 6 | KK street | 2019-03-27 16:26:50 | BEFORE INSERT |
| 3 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:31:40 | BEFORE UPDATE |
| 4 | 102 | 13 | indhu | 6 | KK street | 2019-03-27 16:32:15 | BEFORE UPDATE |
| 5 | 101 | 20 | sindhu | 4 | SS street | 2019-03-27 16:38:09 | BEFORE DELETE |
| 6 | 103 | 15 | nithya | 8 | JJ street | 2019-03-27 16:44:00 | BEFORE INSERT |
| 7 | 103 | 15 | nithya | 8 | JJ street | 2019-03-27 16:44:00 | AFTER INSERT |
| 8 | 104 | 14 | sandy | 10 | MM street | 2019-03-27 16:44:24 | BEFORE INSERT |
| 9 | 104 | 14 | sandy | 10 | MM street | 2019-03-27 16:44:24 | AFTER INSERT |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
9 rows in set (0.00 sec)

AFTER UPDATE:

mysql> delimiter //
mysql> create trigger after_shop_update after update on orderdetails for each row begin insert into t1
set action='AFTER UPDATE',Order_id=NEW.Order_id,Product_id=NEW.Product_id,
Customer_name=NEW.Customer_name,Quantity=NEW.Quantity,Address=NEW.Address,changedate=NOW();
-> end//
Query OK, 0 rows affected (0.12 sec)
mysql> delimiter ;
mysql> update orderdetails set Customer_name='sankar' where Order_id='104';
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select *from orderdetails;
+----------+------------+---------------+----------+-----------+
| Order_id | Product_id | Customer_name | Quantity | Address |
+----------+------------+---------------+----------+-----------+
| 102 | 13 | manikkam | 6 | KK street |
| 103 | 15 | nithya | 8 | JJ street |
| 104 | 14 | sankar | 10 | JJ street |
+----------+------------+---------------+----------+-----------+
3 rows in set (0.00 sec)
mysql> select *from t1;
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| id | Order_id | Product_id | Customer_name | Quantity | Address | changedate | action |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| 1 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:26:08 | BEFORE INSERT |
| 2 | 102 | 13 | indhu | 6 | KK street | 2019-03-27 16:26:50 | BEFORE INSERT |
| 3 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:31:40 | BEFORE UPDATE |
| 4 | 102 | 13 | indhu | 6 | KK street | 2019-03-27 16:32:15 | BEFORE UPDATE |
| 5 | 101 | 20 | sindhu | 4 | SS street | 2019-03-27 16:38:09 | BEFORE DELETE |
| 6 | 103 | 15 | nithya | 8 | JJ street | 2019-03-27 16:44:00 | BEFORE INSERT |
| 7 | 103 | 15 | nithya | 8 | JJ street | 2019-03-27 16:44:00 | AFTER INSERT |
| 8 | 104 | 14 | sandy | 10 | MM street | 2019-03-27 16:44:24 | BEFORE INSERT |
| 9 | 104 | 14 | sandy | 10 | MM street | 2019-03-27 16:44:24 | AFTER INSERT |
| 13 | 104 | 14 | sandy | 10 | MM street | 2019-03-27 17:02:56 | BEFORE UPDATE |
| 14 | 104 | 14 | sankar | 10 | MM street | 2019-03-27 17:02:56 | AFTER UPDATE |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
11 rows in set (0.00 sec)

AFTER DELETE:

mysql> delimiter //
mysql> create trigger after_shop_delete after delete on orderdetails for each row begin insert into t1
set action='AFTER DELETE',Order_id=OLD.Order_id,Product_id=OLD.Product_id,
Customer_name=OLD.Customer_name,Quantity=OLD.Quantity,Address=OLD.Address,changedate=NOW();
-> end//
Query OK, 0 rows affected (0.16 sec)
mysql>delimiter ;
mysql> delete from orderdetails where Order_id='104';
Query OK, 1 row affected (0.07 sec)
mysql> delete from orderdetails where Order_id='103';
Query OK, 1 row affected (0.10 sec)
mysql> select *from t1;
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| id | Order_id | Product_id | Customer_name | Quantity | Address | changedate | action |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
| 1 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:26:08 | BEFORE INSERT |
| 2 | 102 | 13 | indhu | 6 | KK street | 2019-03-27 16:26:08 | BEFORE INSERT |
| 3 | 101 | 20 | shaki | 4 | SS street | 2019-03-27 16:31:40 | BEFORE UPDATE |
| 4 | 102 | 13 | indhu | 6 | KK street | 2019-03-27 16:32:15 | BEFORE UPDATE |
| 5 | 101 | 20 | sindhu | 4 | SS street | 2019-03-27 16:38:09 | BEFORE DELETE |
| 6 | 103 | 15 | nithya | 8 | JJ street | 2019-03-27 16:44:00 | BEFORE INSERT |
| 7 | 103 | 15 | nithya | 8 | JJ street | 2019-03-27 16:44:00 | AFTER INSERT |
| 8 | 104 | 14 | sandy | 10 | MM street | 2019-03-27 16:44:24 | BEFORE INSERT |
| 9 | 104 | 14 | sandy | 10 | MM street | 2019-03-27 16:44:24 | AFTER INSERT |
| 13 | 104 | 14 | sandy | 10 | MM street | 2019-03-27 17:02:56 | BEFORE UPDATE |
| 14 | 104 | 14 | sankar | 10 | MM street | 2019-03-27 17:02:56 | AFTER UPDATE |
| 15 | 104 | 14 | sankar | 10 | MM street | 2019-03-27 17:21:02 | BEFORE DELETE |
| 16 | 104 | 14 | sankar | 10 | MM street | 2019-03-27 17:21:02 | AFTER DELETE |
| 17 | 103 | 15 | nithya | 8 | MM street | 2019-03-27 17:21:23 | BEFORE DELETE |
| 18 | 103 | 15 | nithya | 8 | MM street | 2019-03-27 17:21:23 | AFTER DELETE |
+----+----------+------------+---------------+----------+-----------+---------------------+---------------+
15 rows in set (0.00 sec)

PROCEDURE:

mysql> create procedure GetallDept() Select * from dept;


Query OK, 0 rows affected (0.12 sec)
mysql> call GetallDept();
+---------+----------------+--------------+
| Dept_id | Dept_name | Floor_no |
+---------+----------------+--------------+
| 101 | Cosmetics | 2nd Floor |
| 102 | Snacks | 1st Floor |
| 103 | Vegetables | 3rd Floor |
| 104 | Gift section | 2nd Floor |
| 105 | Fruits section | Ground Floor |
+---------+----------------+--------------+
5 rows in set (0.01 sec)
mysql> create procedure Dept_in(IN b int) select * from dept limit b;
Query OK, 0 rows affected (0.00 sec)
mysql> call Dept_in(4);
+---------+--------------+-----------+
| Dept_id | Dept_name | Floor_no |
+---------+--------------+-----------+
| 101 | Cosmetics | 2nd Floor |
| 102 | Snacks | 1st Floor |
| 103 | Vegetables | 3rd Floor |
| 104 | Gift section | 2nd Floor |
+---------+--------------+-----------+
4 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> create procedure Dept_out(OUT DeptId int) Select count(*) into DeptId from dept;
Query OK, 0 rows affected (0.00 sec)
mysql> call Dept_out(@K);
Query OK, 1 row affected (0.00 sec)
mysql> select @K;
+------+
| @K |
+------+
| 5 |
+------+
1 row in set (0.00 sec)
mysql> create procedure allCustomer() select * from customer;
Query OK, 0 rows affected (0.00 sec)
mysql> CALL allCustomer();
+---------+---------------+--------+----------------------+--------+
| Cust_Id | Cust_name | Emp_id | Cust_email | Gender |
+---------+---------------+--------+----------------------+--------+
| 8001 | F.Gomathi | 20034 | gomathi29@gmail | Female |
| 8002 | P.Chidambaram | 20032 | chidambaram@gmail | Male |
| 8003 | N.Seetharaman | 20031 | seetharaman@gmail | Male |
| 8004 | F.Jenifer | 20033 | jeniferfrancis@gmail | Female |
| 8005 | R.Kavya | 20035 | kavyashankar@gmail | Female |
+---------+---------------+--------+----------------------+--------+
5 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> create procedure Cust_in(In a int) select * from customer limit a;
Query OK, 0 rows affected (0.00 sec)
mysql> call Cust_in(3);
+---------+---------------+--------+-------------------+--------+
| Cust_Id | Cust_name | Emp_id | Cust_email | Gender |
+---------+---------------+--------+-------------------+--------+
| 8001 | F.Gomathi | 20034 | gomathi29@gmail | Female |
| 8002 | P.Chidambaram | 20032 | chidambaram@gmail | Male |
| 8003 | N.Seetharaman | 20031 | seetharaman@gmail | Male |
+---------+---------------+--------+-------------------+--------+
3 rows in set (0.00 sec)
mysql> create procedure Cust_out(OUT CustId int) Select count(*) into CustId from customer;
Query OK, 0 rows affected (0.02 sec)
mysql> call Cust_out(@M);
Query OK, 1 row affected (0.02 sec)
mysql> select @M;
+------+
| @M |
+------+
| 5 |
+------+
1 row in set (0.00 sec)

FUNCTION:

mysql> select * from customer;


+---------+---------------+--------+----------------------+-------+----------+
| Cust_Id | Cust_name | Emp_id | Cust_email | price | discount |
+---------+---------------+--------+----------------------+-------+----------+
| 8001 | F.Gomathi | 20034 | gomathi29@gmail | 1000 | 100 |
| 8002 | P.Chidambaram | 20032 | chidambaram@gmail | 2000 | 100 |
| 8003 | R.Seetha | 20031 | seetharaman@gmail | 5000 | 100 |
| 8004 | F.Jenifer | 20033 | jeniferfrancis@gmail | 4500 | 100 |
+---------+---------------+--------+----------------------+-------+----------+
4 rows in set (0.00 sec)
mysql> delimiter $$
mysql> create function cust(price int,discount int)returns int
-> begin
-> declare bill int;
-> set bill=price-discount;
-> return bill;
-> end$$
Query OK, 0 rows affected (0.59 sec)
mysql> delimiter ;
mysql> select *, cust(price,discount)as bill from customer;
+---------+---------------+--------+----------------------+-------+----------+------+
| Cust_Id | Cust_name | Emp_id | Cust_email | price | discount | bill |
+---------+---------------+--------+----------------------+-------+----------+------+
| 8001 | F.Gomathi | 20034 | gomathi29@gmail | 1000 | 100 | 900 |
| 8002 | P.Chidambaram | 20032 | chidambaram@gmail | 2000 | 100 | 1900 |
| 8003 | R.Seetha | 20031 | seetharaman@gmail | 5000 | 100 | 4900 |
| 8004 | F.Jenifer | 20033 | jeniferfrancis@gmail | 4500 | 100 | 4400 |
+---------+---------------+--------+----------------------+-------+----------+------+
4 rows in set (0.00 sec)

INDICES:

mysql> create unique index Emp_index on employee(Emp_id,Emp_name);


Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from employee;
+----------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type |
+----------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
| employee | 0 | PRIMARY | 1 | Emp_id | A | 5 | NULL | NULL | | BTREE |
| employee | 0 | Emp_index | 1 | Emp_id | A | 5 | NULL | NULL | | BTREE |
| employee | 0 | Emp_index | 2 | Emp_name | A | 5 | NULL | NULL | YES | BTREE |
| employee | 1 | Dept_id | 1 | Dept_id | A | 5 | NULL | NULL | YES | BTREE |
+----------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
4 rows in set (0.00 sec)
CURSOR:

mysql> DELIMITER $$
mysql> create procedure email_build(INOUT email_list varchar(4000))
->BEGIN DECLARE v_finished INTEGER DEFAULT 0;
->DECLARE v_email varchar(100) DEFAULT "";
->DECLARE email_cursor CURSOR FOR SELECT Cust_email FROM customer;
->DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished=1;
->OPEN email_cursor;
->Get_email:LOOP FETCH email_cursor INTO v_email;
->IF v_finished=1 THEN LEAVE Get_email;
->END IF;
->SET email_list=CONCAT(v_email,";",email_list);
->END LOOP Get_email;
->CLOSE email_cursor;
->END$$
Query OK, 0 rows affected (0.00 sec)
mysql> SET @email_list=""$$
Query OK, 0 rows affected (0.00 sec)
mysql> CALL email_build(@email_list) $$
Query OK, 0 rows affected (0.02 sec)
mysql> SELECT @email_list $$
+----------------------------------------------------------------------------------------------+
| @email_list |
+----------------------------------------------------------------------------------------------+
| kavyashankar@gmail;jeniferfrancis@gmail;seetharaman@gmail;chidambaram@gmail;gomathi29@gmail; |
+----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

RESULT:
EX,NO: STUDENT DATABASE MANAGEMENT SYSYTEM

DATE:

AIM:

CREATION OF DATABASE:

mysql>create database student;


Query OK, 1 row affected (0.00 sec)
mysql>use student;
Database changed
mysql>create table department(dept_id int(10),dept_name varchar(20),building varchar(15),budget
numeric(12,2),primary key(dept_id));
Query OK, 0 rows affected (0.26 sec)
mysql> desc department;
+-----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+-------+
| dept_id | int(10) | NO | PRI | NULL | |
| dept_name | varchar(20) | NO | | NULL | |
| building | varchar(15) | YES | | NULL | |
| budget | numeric(12,2) | YES | | NULL | |
+-----------+---------------+------+-----+---------+-------+
3 rows in set (0.32 sec)
mysql>insert into department
values(,100,'CSE','bno_03',9800),(101,'ECE','bno_02',8700),(102,'IT','bno_01',8000);
Query OK 3 rows affected (0.44 sec)
mysql> select*from department;
+---------+-----------+----------+---------+
|dept_id | dept_name | building | budget |
+---------|-----------+----------+---------+
| 100 | CSE | bno_03 | 9800.00 |
| 101 | ECE | bno_02 | 8700.00 |
| 102 | IT | bno_01 | 8000.00 |
+---------+-----------+----------+---------+
3 rows in set (0.00 sec)
mysql>create table course(course_id int(20),title varchar(50),dept_id int(10),credits numeric(4,0),primary
key(course_id),foreign key(dept_id)references department(dept_id));
Query OK 0 rows affected (0.60 sec)
mysql> desc course;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| course_id | int(20) | NO | PRI | NULL | |
| title | varchar(50) | YES | | NULL | |
| dept_id | int(10) | YES | MUL | NULL | |
| credits | numeric(4,0) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>insert into course
values('cs001','OS','100',10),('EC002','EC2','101',10),('IT003','DBMS','102',10);
Query OK 3 rows affected (0.44 sec affected)
mysql> select*from course;
+-----------+-------+-----------+---------+
| course_id | title | dept_id | credits |
+-----------+-------+-----------+---------+
| cs001 | OS | 100 | 10 |
| EC002 | EC2 | 101 | 10 |
| IT003 | DBMS | 102 | 10 |
+-----------+-------+-----------+---------+
3 rows in set (0.00 sec)
mysql>create table instructors(ID int(5),name varchar(20),dept_id int(20),salary numeric(8,2),primary
key(ID),foreign key(dept_id)references department(dept_id));
Query OK 0 rows affected (0.70 sec)
mysql> desc instructors;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| ID | int(5) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| dept_id | int(20) | YES | MUL | NULL | |
| salary | numeric(8,2) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>insert into instructors
values(1001,'MR.Kumar','100',90),(1002,'MR.Raj'.'101',70),(1003,'MR.Ram','102',60);
Query OK 3 rows affected (0.44 sec)
mysql> select*from instructors;
+------+----------+-----------+--------+
| ID | name | dept_id | salary |
+------+----------+-----------+--------+
| 1001 | MR.kumar | 100 | 90.00 |
| 1002 | MR.Raj | 101 | 70.00 |
| 1003 | MR.Ram | 102 | 60.00 |
+------+----------+-----------+--------+
3 rows in set (0.01 sec)
mysql>create table section(course_id int(8),sec_id int(8),semester varchar(6),year decimal(4,0),building
varchar(15),room_no int(7),primary key(course_id,sec_id,semester,year));
Query OK 0 rows affected (0.71 sec)
mysql> desc section;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| course_id | int(8) | NO | PRI | NULL | |
| sec_id | int(8) | NO | PRI | NULL | |
| semester | varchar(6) | NO | PRI | NULL | |
| year | decimal(4,0) | NO | PRI | NULL | |
| building | varchar(15) | YES | | NULL | |
| room_no | int(7) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql>insert into section values('cs001','A1',1,2017,'bno_03',12),('EC002','A2',1,2018,'bno_02',13),
('IT003','A3',1,2018,'bno_01',14);
Query OK 3 rows affected (0.44 sec)
mysql> select*from section;
+-----------+--------+----------+------+----------+---------+
| course_id | sec_id | semester | year | building | room_no |
+-----------+--------+----------+------+----------+---------+
| cs001 | A1 | 1 | 2017 | bno_03 | 12 |
| EC002 | A2 | 1 | 2018 | bno_02 | 13 |
| IT003 | A3 | 1 | 2018 | bno_01 | 14 |
+-----------+--------+----------+------+----------+---------+
mysql>create table teaches(ID int(5),course_id int(8),sec_id int(8),semester varchar(6),year
decimal(4,0),primary key(ID,course_id,sec_id,semester,year));
Query OK 0 rows affected (0.71 sec)
mysql> desc teaches;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| ID | int(5) | NO | PRI | NULL | |
| course_id | int(8) | NO | PRI | NULL | |
| sec_id | int(8) | NO | PRI | NULL | |
| semester | varchar(6) | NO | PRI | NULL | |
| year | decimal(4,0) | NO | PRI | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql>insert into teaches
values(1001,'cs001','A1',1,2017),(1002,'EC002','A2',1,2018),(1003,'IT003','A3',1,2018);
Query OK 3 rows affected (0.44 sec)
mysql> select*from teaches;
+------+-----------+--------+----------+------+
| ID | course_id | sec_id | semester | year |
+------+-----------+--------+----------+------+
| 1001 | cs001 | A1 | 1 | 2017 |
| 1002 | EC002 | A2 | 1 | 2018 |
| 1003 | IT003 | A3 | 1 | 2018 |
+------+-----------+--------+----------+------+
3 rows in set (0.00 sec)
mysql> update instructors set dept_name='ECE' where ID='1002';
Query OK, 1 row affected (0.18 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update instructors set dept_name='IT' where ID='1003';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select*from instructors;
+------+----------+-----------+--------+
| ID | name | dept_name | salary |
+------+----------+-----------+--------+
| 1001 | MR.kumar | CSE | 90.00 |
| 1002 | MR.Raj | ECE | 70.00 |
| 1003 | MR.Ram | IT | 60.00 |
+------+----------+-----------+--------+
3 rows in set (0.00 sec)
mysql> alter table teaches add email varchar(20);
Query OK, 0 rows affected (0.89 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select*from teaches;
+------+-----------+--------+----------+------+-------+
| ID | course_id | sec_id | semester | year | email |
+------+-----------+--------+----------+------+-------+
| 1001 | cs001 | A1 | 1 | 2017 | NULL |
| 1002 | EC002 | A2 | 1 | 2018 | NULL |
| 1003 | IT003 | A3 | 1 | 2018 | NULL |
+------+-----------+--------+----------+------+-------+
3 rows in set (0.00 sec)
mysql> update teaches set email='[email protected]' where ID='1001';
Query OK, 1 row affected (0.42 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update teaches set email='[email protected]' where ID='1002';
Query OK, 1 row affected (0.38 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update teaches set email='[email protected]' where ID='1003';
Query OK, 1 row affected (0.38 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select*from teaches;
+------+-----------+--------+----------+------+---------------+
| ID | course_id | sec_id | semester | year | email |
+------+-----------+--------+----------+------+---------------+
| 1001 | cs001 | A1 | 1 | 2017 | [email protected] |
| 1002 | EC002 | A2 | 1 | 2018 | [email protected] |
| 1003 | IT003 | A3 | 1 | 2018 | [email protected] |
+------+-----------+--------+----------+------+---------------+
3 rows in set (0.00 sec)
mysql> rename table instructors to instructor;
Query OK, 0 rows affected (0.45 sec)
mysql> select*from instructor;
+------+----------+-----------+--------+
| ID | name | dept_name | salary |
+------+----------+-----------+--------+
| 1001 | MR.kumar | CSE | 90.00 |
| 1002 | MR.Raj | ECE | 70.00 |
| 1003 | MR.Ram | IT | 60.00 |
+------+----------+-----------+--------+
3 rows in set (0.00 sec)
mysql> alter table teaches drop column email;
Query OK, 0 rows affected (0.90 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select*from teaches;
+------+-----------+--------+----------+------+
| ID | course_id | sec_id | semester | year |
+------+-----------+--------+----------+------+
| 1001 | cs001 | A1 | 1 | 2017 |
| 1002 | EC002 | A2 | 1 | 2018 |
| 1003 | IT003 | A3 | 1 | 2018 |
+------+-----------+--------+----------+------+
3 rows in set (0.00 sec)
mysql> drop table teaches;
Query OK, 0 rows affected (2.06 sec)

VIEWS:

mysql> create view dept as select course_id,sec_id,semester,year from section where room_no='14';
Query OK, 0 rows affected (0.42 sec)
mysql> select*from dept;
+-----------+--------+----------+------+
| course_id | sec_id | semester | year |
+-----------+--------+----------+------+
| IT003 | A3 | 1 | 2018 |
+-----------+--------+----------+------+
1 row in set (0.00 sec)

TRIGGERS:
BEFORE INSERT:

mysql> delimiter //
mysql> create trigger sal_check before insert on instructors for each row if new.salary<70 then set
new.salary=70;
-> end if;
-> end//
Query OK, 0 rows affected (0.11 sec)
mysql>delimiter ;
mysql> insert into instructors values(1004,'MR.Rohan','ECE',50.00);
Query OK, 1 row affected (0.46 sec)
mysql> select*from instructors;
+------+----------+-----------+--------+
| ID | name | dept_name | salary |
+------+----------+-----------+--------+
| 1001 | MR.Kumar | CSE | 90.00 |
| 1002 | MR.Raj | IT | 70.00 |
| 1003 | MR.Ram | ECE | 60.00 |
| 1004 | MR.Rohan | ECE | 70.00 |
+------+----------+-----------+--------+
4 rows in set (0.00 sec)

BEFORE UPDATE:

mysql> delimiter //
mysql> create trigger upd_check before update on instructors for each row begin if new.salary<50 then set
new.salary=1000;
-> elseif new.salary>50 then set new.salary=500;
-> end if;
-> end//
Query OK, 0 rows affected (0.50 sec)
mysql>delimiter ;
mysql> update instructors set salary=100 where ID='1001';
Query OK, 1 row affected (0.43 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from instructors;
+------+----------+-----------+--------+
| ID | name | dept_name | salary |
+------+----------+-----------+--------+
| 1001 | MR.Kumar | CSE | 500.00 |
| 1002 | MR.Raj | IT | 70.00 |
| 1003 | MR.Ram | ECE | 60.00 |
| 1004 | MR.Rohan | ECE | 70.00 |
+------+----------+-----------+--------+
4 rows in set (0.00 sec)

BEFORE DELETE:

mysql> create table removable_sal(ID varchar(5),name varchar(20),dept_name varchar(20),salary


decimal(7,3));
Query OK, 0 rows affected (0.83 sec)
mysql> delimiter //
mysql> create trigger remove_sal before delete on instructors for each row begin insert into
removable_sal(ID,name,dept_name,salary)values(old.id,old.name,old.dept_name,old.salary);
-> end//
Query OK, 0 rows affected (0.53 sec)
mysql> delimiter ;
mysql> delete from instructors where ID='1001';
Query OK, 1 row affected (0.06 sec)
mysql> select * from removable_sal;
+------+----------+-----------+---------+
| ID | name | dept_name | salary |
+------+----------+-----------+---------+
| 1001 | MR.Kumar | CSE | 500.000 |
+------+----------+-----------+---------+
1 row in set (0.00 sec)
AFTER INSERT:
mysql> create table course_aft(course_id varchar(20),tittle varchar(50),dept_name varchar(20),ACTION
varchar(50));
Query OK, 0 rows affected (0.81 sec)
mysql> delimiter //
mysql> create trigger af_insert after insert on course for each row begin insert into
course_aft(course_id,tittle,dept_name,ACTION)values(NEW.course_id,NEW.tittle,NEW.dept_name,
'AFTER INSERT'); end//
Query OK, 0 rows affected (0.13 sec)
mysql> delimiter ;
mysql> insert into course values('cs003','cs2','CSE',9);
Query OK, 1 row affected (0.35 sec)
mysql> insert into course values('EC004','EE2','ECE',9);
Query OK, 1 row affected (0.35 sec)
mysql> select *from course_aft;
+-----------+--------+-----------+--------------+
| course_id | tittle | dept_name | ACTION |
+-----------+--------+-----------+--------------+
| cs003 | cs2 | CSE | AFTER INSERT |
| EC004 | EE2 | ECE | AFTER INSERT |
+-----------+--------+-----------+--------------+
2 rows in set (0.00 sec)

AFTER UPDATE:

mysql>create table course_af_up(course_id varchar(20),tittle varchar(20),dept_name varchar(20),ACTION


varchar(20));
Query OK, 0 rows affected (0.66 sec)
mysql>delimiter //
mysql> create trigger after_up after update on course for each row begin insert into
course_af_up(course_id,tittle,dept_name,ACTION)values(NEW.course_id,NEW.tittle,NEW.dept_name,
'AFTER UPDATE'); end//
Query OK, 0 rows affected (0.47 sec)
mysql> delimiter ;
mysql> update course set tittle='OS' where course_id='cs001';
Query OK, 0 rows affected (0.10 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> update course set tittle='DAA' where course_id='IT003';
Query OK, 1 row affected (0.42 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from course_af_up;
+-----------+--------+-----------+--------------+
| course_id | tittle | dept_name | ACTION |
+-----------+--------+-----------+--------------+
| cs001 | OS | CSE | AFTER UPDATE |
| IT003 | DAA | IT | AFTER UPDATE |
+-----------+--------+-----------+--------------+
2 rows in set (0.00 sec)

AFTER DELETE:

mysql> create table course_af_del(course_id varchar(20),tittle varchar(20),dept_name varchar(20),action


varchar(20),date date,time time);
Query OK, 0 rows affected (0.64 sec)
mysql> delimiter //
mysql> create trigger after_remove_course after delete on course for each row begin insert into
course_af_del(course_id,tittle,dept_name,date,time,action)values(OLD.course_id,OLD.tittle,
OLD.dept_name,NOW(),SYSDATE(),'AFTER DELETE');end//
Query OK, 0 rows affected (0.19 sec)
mysql> delimiter ;
mysql> delete from course where course_id='cs001';
Query OK, 1 row affected (0.44 sec)

mysql> select * from course_af_del;


+-----------+--------+-----------+----------------+------------+----------+
| course_id | tittle | dept_name | action | date | time |
+-----------+--------+-----------+----------------+------------+----------+
| cs001 | OS | CSE | AFTER DELETE | 2019-03-27 | 09:38:06 |
+-----------+--------+-----------+----------------+------------+----------+
1 row in set (0.00 sec)

mysql> select * from course;


+-----------+-------+-----------+---------+------+------+
| course_id | title | dept_name | credits | IA | sem |
+-----------+-------+-----------+---------+------+------+
| cs001 | OS | CSE | 10 | 18 | 50 |
| EC002 | EC2 | ECE | 10 | 17 | 55 |
| IT003 | DBMS | IT | 10 | 19 | 60 |
+-----------+-------+-----------+---------+------+------+
3 rows in set (0.00 sec)

FUNCTION:
mysql> delimiter $$
mysql> create function crs(IA int,sem int)returns int begin declare total int; -> set total=IA+sem;
-> return total;
-> end$$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> select *,crs(IA,sem)as total from course;
+-----------+-------+-----------+---------+------+------+-------+
| course_id | title | dept_name | credits | IA | sem | total |
+-----------+-------+-----------+---------+------+------+-------+
| cs001 | OS | CSE | 10 | 18 | 50 | 68 |
| EC002 | EC2 | ECE | 10 | 17 | 55 | 72 |
| IT003 | DBMS | IT | 10 | 19 | 60 | 79 |
+-----------+-------+-----------+---------+------+------+-------+
3 rows in set (0.00 sec)

STORED_PROCEDURE:

mysql> create procedure inst()


-> select * from instructor;
Query OK, 0 rows affected (0.12 sec)
mysql> call inst();
+------+----------+-----------+--------+
| ID | name | dept_name | salary |
+------+----------+-----------+--------+
| 1001 | MR.kumar | CSE | 90.00 |
| 1002 | MR.Raj | ECE | 70.00 |
| 1003 | MR.Ram | IT | 60.00 |
+------+----------+-----------+--------+
3 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

mysql> create procedure sec (IN room_no int) select*from section limit room_no;
Query OK, 0 rows affected (0.44 sec)
mysql> call sec(2);
+-----------+--------+----------+------+----------+---------+
| course_id | sec_id | semester | year | building | room_no |
+-----------+--------+----------+------+----------+---------+
| cs001 | A1 | 1 | 2017 | bno_03 | 12 |
| EC002 | A2 | 1 | 2018 | bno_02 | 13 |
+-----------+--------+----------+------+----------+---------+
2 rows in set (0.36 sec)
Query OK, 0 rows affected (0.40 sec)
mysql> create procedure budget1(OUT high_budget int)
-> select max(budget) into high_budget from department;
Query OK, 0 rows affected (0.31 sec)
mysql> call budget1(@g);
Query OK, 1 row affected (0.30 sec)
mysql> select @g;
+------+
| @g |
+------+
| 9800 |
+------+
1 row in set (0.00 sec)
mysql> create procedure budget2(OUT low_budget int) select min(budget) into low_budget from department;
Query OK, 0 rows affected (0.00 sec)
mysql> call budget2(@y);
Query OK, 1 row affected (0.00 sec)
mysql> select @y;
+------+
| @y |
+------+
| 8000 |
+------+
1 row in set (0.00 sec)

INDEX:

mysql> select * from department;


+-----------+----------+---------+
| dept_name | building | budget |
+-----------+----------+---------+
| CSE | bno_03 | 9800.00 |
| ECE | bno_02 | 8700.00 |
| IT | bno_01 | 8000.00 |
+-----------+----------+---------+
3 rows in set (0.00 sec)

mysql> create index depts on department(budget);


Query OK, 0 rows affected (0.31 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> show indexes from department where key_name='depts';


+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
| department | 1 | depts | 1 | budget | A | 3 | NULL | NULL | YES | BTREE |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+
1 row in set (0.00 sec)
CURSOR:

mysql> select * from instructor;


+------+----------+-----------+--------+
| ID | name | dept_name | salary |
+------+----------+-----------+--------+
| 1001 | MR.kumar | CSE | 90.00 |
| 1002 | MR.Raj | ECE | 70.00 |
| 1003 | MR.Ram | IT | 60.00 |
+------+----------+-----------+--------+
3 rows in set (0.00 sec)
mysql> delimiter $$
mysql> create procedure myproc()
-> reads sql data
-> begin
-> declare 1_last_row int default 0;
-> declare 1_dept_id int
-> declare c_dept cursor for
-> select id
-> from instructor;
-> declare continue handler for not found set 1_last_row=1;
-> open c_dept;
-> dept_cursor:loop
-> fetch c_dept into 1_dept_id;
-> if(1_last_row=1) then
-> leave dept_cursor;
-> end if;
-> select 1_dept_id;
-> end loop dept_cursor;
-> close c_dept;
-> end$$
Query OK, 0 rows affected (0.05 sec)
mysql> delimiter ;
mysql> call myproc();
+-----------+
| 1_dept_id |
+-----------+
| 1001 |
+-----------+
1 row in set (0.30 sec)
+-----------+
| 1_dept_id |
+-----------+
| 1002 |
+-----------+
1 row in set (0.30 sec)
+-----------+
| 1_dept_id |
+-----------+
| 1003 |
+-----------+
1 row in set (0.30 sec)
Query OK, 0 rows affected (0.30 sec)

RESULT:
EX.NO:
STORED PROCEDURE WITHOUT TABLE
DATE:

AIM:

FACTORIAL USING STORED PROCEDURE:

mysql> delimiter $$
mysql> CREATE PROCEDURE fact(IN x INT)
-> BEGIN
-> DECLARE result INT;
-> DECLARE i INT;
-> SET result=1;
-> SET i =1;
-> WHILE i<= x DO
-> SET result =result * i;
-> SET i=i+1;
-> END WHILE;
-> SELECT x AS Number,result as factorial;
-> END$$
Query OK, 0 rows affected (0.06 sec)

mysql> delimiter ;
mysql> CALL Fact(6);
+--------+-----------+
| Number | factorial |
+--------+-----------+
| 6 | 720 |
+--------+-----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

FINDING ODD OR EVEN NUMBER USING STORED PROCEDURE:

mysql> delimiter $$
mysql> CREATE PROCEDURE num(IN x INT)
-> BEGIN
-> IF (SELECT LEFT(num,1))=1 OR (SELECT LEFT(num,1))=3 OR
->(SELECT LEFT(num,1))=5 OR (SELECT LEFT(num,1))=7 THEN
-> SELECT 'number is odd';
-> ELSEIF (SELECT LEFT(num,1))=2 OR (SELECT LEFT(num,1))=4 OR (SELECT LEFT(num,1))=6 OR
->(SELECT LEFT(num,1))=8 THEN
-> SELECT 'number is even';
-> END IF;
-> END$$
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter ;
mysql> CALL num(4);
'number is even'

1 row in set (0.00 sec)


Query OK, 0 rows affected (0.00 sec)
SUBTRACTION USING STORED PROCEDURE:

mysql> delimiter $$
mysql> CREATE PROCEDURE my_procedure_User_Variables()
-> BEGIN
-> SET @x=15;
-> SET @y=10;
-> SELECT @x,@y,@x-@y;
-> END$$
Query OK, 0 rows affected (0.01 sec)

mysql> DELIMITER ;
mysql> CALL my_procedure_User_Variables();

+------+------+-------+
| @x | @y | @x-@y |
+------+------+-------+
| 15 | 10 | 5 |
+------+------+-------+

1 row in set (0.00 sec)


Query OK, 0 rows affected (0.01 sec)

SUM OF NUMBERS USING STORED PROCEDURE:

mysql> DELIMITER $$
mysql> CREATE PROCEDURE my_proc_REPEAT (IN n INT)
->BEGIN
->SET @sum = 0;
->SET @x = 1;
->REPEAT
->IF mod(@x, 2) = 0
->THEN
->SET @sum = @sum + @x;
->END IF;
->SET @x = @x + 1;
->UNTIL @x > n
->END REPEAT;
->END $$
mysql> SELECT @sum;

+------+
| @sum |
+------+
| 6 |
+------+
1 row in set (0.00 sec)
FIBONACCI SERIES USING STORED PROCEDURE:

mysql> delimiter $$
mysql> create procedure fibonacci(n int)
-> begin
-> declare n1 int default not null;
-> declare n2 int default not null;
-> declare n3 int default 0;
-> declare seq text;
-> declare count int default 0;
-> set count=0;
-> set n1=1;
-> set n2=0;
-> set n3=null;
-> set seq='';
-> while count<n do
-> set count=count+1;
-> set n3 = n1 + n2;
-> set n1=n2;
-> set n2 = n3;
-> set seq = (select CONCAT(seq,n3,', ') from dual);
-> end while;
-> SELECT seq AS fibonacci_numbers;
-> end$$

Query OK, 0 rows affected (0.05 sec)

mysql> delimiter ;

mysql> CALL fibonacci(10);

+------------------------------------+
| fibonacci_numbers |
+------------------------------------+
| 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, |
+------------------------------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
ARMSTRONG NUMBER USING STORED PROCEDURE:

mysql> delimiter $$
mysql> create procedure Armstrong(n int)
-> declare
-> n number:=407;
-> s number:=0;
-> r number;
-> len number;
-> m number;
-> begin
-> m:=n;
->
-> len:=length(to_char(n));
->
-> while n>0
-> loop
-> r:=mod(n,10);
-> s:=s+power(r,len);
-> n:=trunc(n/10);
-> end loop;
-> if m=s
-> then
-> dbms_output.put_line('armstrong number');
-> else
-> dbms_output.put_line('not armstrong number');
-> end if;
-> end$$

Query OK, 0 rows affected(0.02 sec)

mysql> delimiter;
mysql> CALL Armstrong(153);

'armstrong number'

1 row is set (0.00 sec)


Query OK, 0 rows affected (0.00 sec)

RESULT:
EX.NO: FUNGTION WITHOUT TABLE

DATE:

AIM:

WEIGHTED_AVERAGE:

mysql> delimiter //
mysql> CREATE FUNCTION WEIGHTED_AVERAGE (n1 INT, n2 INT, n3 INT, n4 INT)
-> RETURNS INT
-> DETERMINISTIC
-> BEGIN
-> DECLARE avg INT;
-> SET avg = (n1+n2+n3*2+n4*4)/8;
-> RETURN avg;
-> end //
Query OK, 0 rows affected (0.04 sec)
mysql> SELECT WEIGHTED_AVERAGE(70,65,65,60)\G
*************************** 1. row ***************************
WEIGHTED_AVERAGE(70,65,65,60): 63
1 row in set (0.03 sec)
************************************************************

FACTORIAL:

mysql> CREATE FUNCTION factorial(n INT)


-> RETURNS INT(11)
-> BEGIN
-> DECLARE factorial INT;
-> SET factorial = n ;
-> IF n <= 0 THEN
-> RETURN 1;
-> END IF;
->
-> bucle: LOOP
-> SET n = n - 1 ;
-> IF n<1 THEN
-> LEAVE bucle;
-> END IF;
-> SET factorial = factorial * n ;
-> END LOOP bucle;
->
-> RETURN factorial;
-> end //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> select factorial(10);
+---------------+
| factorial(10) |
+---------------+
| 3628800 |
+---------------+
1 row in set (0.00 sec)

*******************************************************
CALCULATE INCOME:

mysql> delimiter //
mysql> CREATE FUNCTION CalcIncome ( starting_value INT )
-> RETURNS INT
-> BEGIN
-> DECLARE income INT;
-> SET income = 0;
-> label1: WHILE income <= 3000 DO
-> SET income = income + starting_value;
-> END WHILE label1;
-> RETURN income;
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> SELECT CalcIncome (1000);
+-------------------+
| CalcIncome (1000) |
+-------------------+
| 4000 |
+-------------------+
1 row in set (0.00 sec)
**********************************************

MYSQUARE:

mysql>delimiter //
-> create function mysquare(val int)
-> returns int
-> begin
-> declare result int;
-> set result =0;
-> set result = val*val;
-> return result;
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> select mysquare(8);
+-------------+
| mysquare(8) |
+-------------+
| 64 |
+-------------+
1 row in set (0.00 sec)
*********************************************

SUM OF TWO NUMBER:

mysql> delimiter $$
mysql> create function abc(a int, b int)
-> returns int
-> deterministic
-> begin
-> declare c int ;
-> set c= a+b;
-> return c;
-> end $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> select abc(2,3);
+----------+
| abc(2,3) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)

RESULT:
EX.NO: MENU DESIGN

DATE:

AIM:

SOURSE CODE:

import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
class menu extends Frame
{
String fn;
MenuBar m;
Menu f,e;
MenuItem n,o,s,ex,c,cop,pa;

TextArea t1;
Clipboard Clip=getToolkit().getSystemClipboard();
menu()

{
m=new MenuBar();
f=new Menu("file");
m.add(f);
n=new MenuItem("new");
f.add(n);
n.addActionListener(new New());
o=new MenuItem("open");
f.add(o);
o.addActionListener(new Open());
s=new MenuItem("save");
f.add(s);
s.addActionListener(new Save());
ex=new MenuItem("exit");
f.add(ex);
ex.addActionListener(new Exit());
e=new Menu("edit");
m.add(e);
c=new MenuItem("cut");
e.add(c);
c.addActionListener(new Cut());
cop=new MenuItem("copy");
e.add(cop);
cop.addActionListener(new Copy());
pa=new MenuItem("paste");
e.add(pa);
pa.addActionListener(new Paste());
t1=new TextArea();
add(t1);
setMenuBar(m);
mylistener mylist=new mylistener();
addWindowListener(mylist);
}
class mylistener extends WindowAdapter
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}
class New implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
t1.setText("");
setTitle(fn);
}
}
class Open implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(menu.this,"Select file",FileDialog.LOAD);
fd.show();
if(fd.getFile()!=null)
{
fn=fd.getDirectory()+fd.getFile();
setTitle(fn);
Readfile();
}
t1.requestFocus();
}
}
class Save implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(menu.this,"Save file",FileDialog.SAVE);
fd.show();
if(fd.getFile()!=null)
{
fn=fd.getDirectory()+fd.getFile();
setTitle(fn);
try
{
DataOutputStream d=new DataOutputStream(new FileOutputStream(fn));
String line=t1.getText();
BufferedReader br=new BufferedReader(new StringReader(line));
while((line=br.readLine())!=null)
{
d.writeBytes(line+"\r\n");
d.close();
}
}
catch(Exception ef)
{
System.out.println("File not found");
}
t1.requestFocus();
}
}
}
class Exit implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
void Readfile()
{
BufferedReader d;
StringBuffer sb=new StringBuffer();
try
{
d=new BufferedReader(new FileReader(fn));
String line;
while((line=d.readLine())!=null)
sb.append(line+"\n");
t1.setText(sb.toString());
d.close();
}
catch(FileNotFoundException fe)
{
System.out.println("File Not Found");
}
catch(Exception ie)
{
}
}
class Cut implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String se1=t1.getSelectedText();
StringSelection ss=new StringSelection(se1);
Clip.setContents(ss,ss);
t1.replaceRange ("",t1.getSelectionStart(), t1.getSelectionEnd());

}
}
class Copy implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String se1=t1.getSelectedText();
StringSelection clipString=new StringSelection(se1);
Clip.setContents(clipString,clipString);

}
}
class Paste implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Transferable Cliptran=Clip.getContents(menu.this);
try
{
String se1=(String) Cliptran.getTransferData(DataFlavor.stringFlavor);
t1.replaceRange(se1,t1.getSelectionStart(),t1.getSelectionEnd());
}
catch(Exception exc)
{
System.out.println("not string flavor");
}
}
}

public static void main(String arg[])


{
Frame f=new menu();
f.setSize(500,400);
f.setVisible(true);
f.show();
}

OUTPUT:
RESULT:
EX.NO: FORMS AND MENU DESIGN

DATE:

AIM:

TABLE CREATION:

create table sdetails(name varchar(40),regno bigint(12),dept enum('IT','CSE','ECE','MECH-EM','MECH-TM'),


year enum('1','2','3','4'),fname varchar(40),mname varchar(40),dob date,
gender enum('Male','Female'),mobile bigint(11),email varchar(30));

JAVA CODING:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.awt.Dialog;
import javax.swing.JOptionPane;
import java.io.*;
import javax.swing.event.*;
import java.lang.*;
import java.net.*;
public class Mymenu3 extends JFrame
{
public static JMenuBar mb;
public static JMenu file;
public static JMenuItem rm,sm,em;
public static JPanel p0,p,p2;
public static JButton nxtb,prvb;
public static String n,d,y,f,m,dob,g,co,e,sr,sr1;
public static double ca;
public static JLabel nl,rl,dl,yl,fl,ml,dobl,gl,cl,el,welcome,sreg;
public static JTextField nt,rt,ft,mt,dobt,ct,et,sregt;
public static Choice dc,yc,gc;
public static JButton sb,nb,eb;
public static int a;
public static int msgType = JOptionPane.QUESTION_MESSAGE;
public static Connection con;
public static Statement st;
public static ResultSet rs;
Container c;
Mymenu3()
{
c = getContentPane();
c.setLayout(new BorderLayout());
mb = new JMenuBar();
//--------------------------------------------------------------
p0=new JPanel();
p0.setLayout(null);
p0.setBounds(30,0,500,500);
welcome=new JLabel("<html><p><h1>WELCOME FOR STUDENT DETAILS MANAGEMENT SYSTEM</h1></html>");
welcome.setBounds(30,0,500,500);
p0.add(welcome);
//--------------------------------------------------------------
p=new JPanel();
p.setLayout(null);
p.setBounds(30,0,500,500);
nl = new JLabel("Name ");
nt= new JTextField(50);
nl.setBounds(70,50,80,25);
nt.setBounds(190,50,250,25);
rl = new JLabel("Reg No ");
rt= new JTextField(12);
rl.setBounds(70,90,80,25);
rt.setBounds(190,90,220,25);
dl = new JLabel("Department ");
dc=new Choice();
dc.add("IT");
dc.add("CSE");
dc.add("ECE");
dc.add("MECH-EM");
dc.add("Mech-TM");
dl.setBounds(70,130,100,25);
dc.setBounds(190,130,200,25);
yl = new JLabel("Year ");
yc=new Choice();
yc.add("1");
yc.add("2");
yc.add("3");
yc.add("4");
yl.setBounds(70,170,100,25);
yc.setBounds(190,170,125,25);
fl = new JLabel("Father Name ");
ft= new JTextField(40);
fl.setBounds(70,210,200,25);
ft.setBounds(190,210,250,25);
ml = new JLabel("Mother Name ");
mt= new JTextField(40);
ml.setBounds(70,250,200,25);
mt.setBounds(190,250,250,25);
dobl=new JLabel("Date of Birth(YYYY-MM-DD)");
dobt=new JTextField(10);
dobl.setBounds(70,290,250,25);
dobt.setBounds(260,290,150,25);
gl=new JLabel("Gender");
gc=new Choice();
gc.add("Male");
gc.add("Female");
gl.setBounds(70,330,100,25);
gc.setBounds(190,330,100,25);
cl=new JLabel("Mobile No ");
ct=new JTextField(11);
cl.setBounds(70,370,100,25);
ct.setBounds(190,370,150,25);
el=new JLabel("e-mail ID ");
et=new JTextField(50);
el.setBounds(70,410,100,25);
et.setBounds(190,410,250,25);
sb=new JButton("Save");
sb.setBounds(110,450,75,35);
nb=new JButton(" New ");
nb.setBounds(230,450,75,35);
eb=new JButton(" Exit ");
eb.setBounds(350,450,75,35);
p.add(nl);
p.add(nt);
p.add(rl);
p.add(rt);
p.add(dl);
p.add(dc);
p.add(yl);
p.add(yc);
p.add(fl);
p.add(ft);
p.add(ml);
p.add(mt);
p.add(dobl);
p.add(dobt);
p.add(gl);
p.add(gc);
p.add(cl);
p.add(ct);
p.add(el);
p.add(et);
p.add(sb);
p.add(nb);
p.add(eb);
sb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String sst=event.getActionCommand();
if(sst.equals("Save"))
{
String ns=nt.getText();
String rs=rt.getText();
double regno=Double.parseDouble(rs);
String ds=dc.getSelectedItem();
String ys=yc.getSelectedItem();
String fs=ft.getText();
String ms=mt.getText();
String dobs=dobt.getText();
String gs=gc.getSelectedItem();
String cs=ct.getText();
double mobile=Double.parseDouble(cs);
String es=et.getText();
// Database Connectivity (MySQL)
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
st=con.createStatement();
st.executeUpdate("insert into sdetails
values('"+ns+"',"+rs+",'"+ds+"','"+ys+"','"+fs+"','"+ms+"','"+dobs+"','"+gs+"',"+cs+",'"+es+"')");
JOptionPane.showMessageDialog(null," Record insert sucessfully","Hello ",msgType);
con.close();
st.close();
}
catch(Exception e)
{
System.out.println("SqlExceptionCaught:"+e);
JOptionPane.showMessageDialog(null," sql Error is occured");
}
}
}
}
);

nb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
nt.setText("");
rt.setText("");
ft.setText("");
mt.setText("");
dobt.setText("");
ct.setText("");
et.setText("");
}
}
);
eb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
);
//-------------------------------------------------------------------------
p2 = new JPanel( );
p2.setLayout(null);
sreg=new JLabel("Enter the Register No : ");
sreg.setFont(new Font("Arial",Font.BOLD,18));
sreg.setBounds(50,30,430,25);
sregt=new JTextField(13);
sregt.setFont(new Font("Arial",Font.BOLD,18));
sregt.setBounds(300,28,200,30);
nl= new JLabel("Name : ");
nl.setBounds(70,65,350,25);
rl = new JLabel("Reg No : ");
rl.setBounds(70,105,350,25);
dl = new JLabel("Department : ");
dl.setBounds(70,145,350,25);
yl = new JLabel("Year : ");
yl.setBounds(70,185,350,25);
fl = new JLabel("Father Name : ");
fl.setBounds(70,225,350,25);
ml = new JLabel("Mother Name : ");
ml.setBounds(70,265,350,25);
dobl=new JLabel("Date of Birth(YYYY-MM-DD) : ");
dobl.setBounds(70,305,350,25);
gl=new JLabel("Gender : ");
gl.setBounds(70,345,350,25);
cl=new JLabel("Mobile No : ");
cl.setBounds(70,385,350,25);
el=new JLabel("e-mail ID : ");
el.setBounds(70,425,350,25);
nxtb=new JButton("Search");
nxtb.setFont(new Font("Arial",Font.BOLD,15));
nxtb.setBounds(510,28,105,30);
p2.add(sreg);
p2.add(sregt);
p2.add(nl);
p2.add(rl);
p2.add(dl);
p2.add(yl);
p2.add(fl);
p2.add(ml);
p2.add(dobl);
p2.add(gl);
p2.add(cl);
p2.add(el);
p2.add(nxtb);
nxtb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String sst=event.getActionCommand();
if(sst.equals("Search"))
{
// Database Connectivity (MySQL)
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
st=con.createStatement();
st.executeQuery("select * from sdetails;");
rs=st.getResultSet();
while(rs.next())
{
sr=sregt.getText();
ca=Double.parseDouble(sr);
sr1=rs.getString("regno");
double a1=Double.parseDouble(sr1);
if(a1==ca)
{
n=rs.getString("name");
d=rs.getString("dept");
y=rs.getString("year");
f=rs.getString("fname");
m=rs.getString("mname");
dob=rs.getString("dob");
g=rs.getString("gender");
co=rs.getString("mobile");
e=rs.getString("email");
nl.setText("Name : "+n);
nl.setFont(new Font("Arial",Font.BOLD,13));
nl.setBounds(70,65,400,35);
rl.setText("Reg No : "+sr1);
rl.setFont(new Font("Arial",Font.BOLD,13));
rl.setBounds(70,105,400,35);
dl.setText("Department : "+d);
dl.setFont(new Font("Arial",Font.BOLD,13));
dl.setBounds(70,145,400,35);
yl.setText("Year : "+y);
yl.setFont(new Font("Arial",Font.BOLD,13));
yl.setBounds(70,185,400,35);
fl.setText("Father Name : "+f);
fl.setFont(new Font("Arial",Font.BOLD,13));
fl.setBounds(70,225,400,35);
ml.setText("Mother Name : "+m);
ml.setFont(new Font("Arial",Font.BOLD,13));
ml.setBounds(70,265,400,35);
dobl.setText("Date of Birth(YYYY-MM-DD) : "+dob);
dobl.setFont(new Font("Arial",Font.BOLD,13));
dobl.setBounds(70,305,400,35);
gl.setText("Gender : "+g);
gl.setFont(new Font("Arial",Font.BOLD,13));
gl.setBounds(70,345,400,35);
cl.setText("Mobile No : "+co);
cl.setFont(new Font("Arial",Font.BOLD,13));
cl.setBounds(70,385,400,35);
el.setText("e-mail ID : "+e);
el.setFont(new Font("Arial",Font.BOLD,13));
el.setBounds(70,425,600,35);
}
}
rs.close();
con.close();
st.close();
}
catch(Exception e)
{
System.out.println("SqlExceptionCaught:"+e);
JOptionPane.showMessageDialog(null," sql Error is occured");
}
}
}
}
);
//---------------------------------------------------------------------------------
c.add("North",mb);//to add menubar in the container
c.add(p0);// to add the panel in the container
file = new JMenu("FILE");
mb.add(file);
rm = new JMenuItem("Register");
sm = new JMenuItem("Search");
em=new JMenuItem("Exit");
file.add(rm);
file.add(sm);
file.add(em);
cl.setEnabled(true);
//to add action listener for Register menuitem
rm.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
p0.setVisible(false);
p2.setVisible(false);
p.setVisible(true);
//p.repaint();
c.add(p);
}});
OUTPUT:
RESULT:

You might also like