Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
BVRIT Hyderabad College of Engineering for Women
Rajiv Gandhi Nagar, Bachupally, Hyderabad -90
Department of Information Technology
Database Management Systems Lab Record
II Year – II Semester (R18 Regulation)
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
SYLLABUS
List of Experiments:
1. Concept design with E-R Model
2. Relational Model
3. Normalization
4. Practicing DDL commands
5. Practicing DML commands
6. Querying (using ANY, ALL, IN, Exists, NOT EXISTS, UNION, INTERSECT, Constraints etc.)
7. Queries using Aggregate functions, GROUP BY, HAVING and Creation and dropping of Views.
8. Triggers (Creation of insert trigger, delete trigger, update trigger)
9. Procedures
10. Usage of Cursors
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
TABLE OF CONTENTS
EXP 1.CONCEPT DESIGN WITH E-R MODEL
EXP 2. RELATIONAL MODEL
EXP 3. NORMALIZATION
EXP 4. PRACTICING DDL COMMANDS
EXP 5. PRACTICING DML COMMANDS
EXP 6. QUERYING (USING ANY, ALL, IN, EXISTS, NOT EXISTS, UNION, INTERSECT,
CONSTRAINTS ETC.)
EXP 7. QUERIES USING AGGREGATE FUNCTIONS, GROUP BY, HAVING AND CREATION
AND DROPPING OF VIEWS.
EXP 8. TRIGGERS (CREATION OF INSERT TRIGGER, DELETE TRIGGER, UPDATE TRIGGER)
EXP 9. PROCEDURES
EXP 10. USAGE OF CURSORS
THE ABOVE EXPERIMENTS WILL BE DONE WITH FOLLOWING TWO TASKS
TASK1 : ROADWAY TRAVELS
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
TASK – 1: ROADWAY TRAVELS
Roadway Travels: "Roadway Travels" is in business since 1997 with several buses connecting different
places in India. Its main office is located in Hyderabad.
The company wants to computerize its operations in the following areas:
• Reservations and Ticketing
• Cancellations
Reservations & Cancellation: Reservations are directly handled by booking office. Reservations can be
made 30 days in advance and tickets issued to passenger. One Passenger/person can book many tickets
(to his/her family).
Cancellations are also directly handed at the booking office.
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
EXPERIMENT 1:E-R MODEL
Aim – To design model of Ticket Reservation System Concept design with E-R Model
EXPERIMENT 2 : RELATIONAL MODEL
Aim – To represent the passenger tables . This is an example to add more attributes
based on our E-R model . This is not a normalized table.
1) Bus(BusNoint , source varchar(10), destination varchar(10))
2) Passenger(P.Noint, pnamevarchar(10), p.ageint, p.sexvarchar(2))
3) Ticket(TNoint, jdate date, p.ageint,p.sexint)
4)Reservation(TNo, P.No, (TNo, Pno) FK,Jdate, num of seats, contact num,
address, status)
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
EXPERIMENT- 3
Aim:- NORMALIZATION
Normalization: The process of decomposing relations that contain anomalies (insertion,Updation,Deletion
anomalies) to produce smaller well structured relations, to reduce data redundancy.
Normal Form: A state of a relation that results from applying simple rules regarding functional
dependencies to that relation.
Functional Dependencies: A functional dependency is a constraint between two attributes or two sets of
attributes. A functional dependency of B on A represented by an arrow A->B
Eg: Bus no->Bname, Type
The types of normal forms:
1st Normal Form: A relation in the 1st normal form if it contains no multi valued attributes.
2nd Normal Form : A relation is in 2nd normal form if it is in 1st normal form and every non-key attribute
is fully functionally dependent on the primary key.
Partial Dependency is functional dependencies in which one or more non-key attributes are functionally
depends on part of the primary key.
3rd Normal Form : A relation is in 3rd normal form if it is in 2nd normal form and no transitive dependency
exists.
Transitive dependency in a relation is a functional dependency between two (or more) non-key attributes.
Boyce Codd Normal Form: A relation in a Boyce Codd normal form iff every determinant in the relation is
a candidate key.it is strict version of 3NF,so also called 3.5NF
4th Normal Form : When a relation is in BCNF there are no longer any anomalies that result from functional
dependency.it comes from concept of Multivalued dependency.
Eg: 1st Normal Form:
PNR no Pname Age Gender Addr Phno
457 Satish 28 M Plot No. 5 RK. Puram, Hyd 9008745625,9959678987
746 Sheela 25 F H.No:1-9-88,DilsukhNagar,Hyderabad 9000998683
Multivalued attributes are normalized into single value attributes.
PNR no Pname Age Gender Addr Phno
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
457 Satish 27 M Plot No. 5 RK. Puram, Hyderabad 9008745625
457 Satish 27 M Plot No. 5 RK. Puram, Hyderabad 9959678987
746 Sheela 25 F H.No:1-9-88,DilsukhNagar,Hyderabad 9000998683
Eg: 2nd Normal Form:
PNR no Pname Age Gender Addr Phno
457 Satish 27 MPlot No. 5 RK. Puram, Hyderabad 9008745625
457 Satish 27 M Plot No. 5 RK. Puram, Hyderabad 9008745625
746 Sheela 25 F H.No:1-9-88,DilsukhNagar,Hyderabad 9000998683
PNR no is the only primary key .so the partial dependency is as follows:
PNR no -> Pname, Age, Gender
PNR no -> Addr, Phno
PNR no Pname Age Gender
457 Satish 27M
746 Sheela 25F
PNR no Addr Phno
457 Plot No. 5 RK. Puram, Hyderabad 9008745625
746 H.No:1-9-88,DilsukhNagar,Hyderabad 9000998683
Eg: 3rd Normal Form:
Tno Journey Date Day Seatno Src Dest Deptime Gender Bus no
2100 15/08/10 Sun 35 Hyd Delhi 10hrs M AP1000
2710 20/08/10 Mon28 B’lore Hyd 15hrs F AP2014
1020 22/08/10 Wed 38 Delhi B’lore 18hrs F AP2387
6100 10/08/10 Tue 20 Hyd Chennai 14hrs M AP09525
1478 25/08/10 Wed 15 Chennai Delhi 06hrs F AP29457
Tno -> Journey Date
Journey Date -> Day
The Table is normalized as follows:
Tno Seatno Src Dest Deptime Gender Bus no
2100 35 Hyd Delhi 10:00 M AP1000
2710 28 B’lore Hyd 15:00 F AP2014
1020 38 Delhi B’lore 18:00 F AP2387
6100 20 Hyd Chennai 14:00 M AP09525
1478 15 Chennai Delhi 06:00 F AP29457
Journey Date Day
15/08/10 Sun
20/08/10 Mon
22/08/10 Wed
10/08/10 Tue
25/08/10 Wed
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
EXPERIMENT - 4
AIM: Installation of MySQL. Creating database tables, altering the database, dropping tables and
truncate commands. Recommended Hardware / Software Requirements:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster processor with
at least 1GB RAM and 500 MB free disk space. MySQL 8.0.19
➢ Installation Steps for MySQL:
1. Steps for installing
2. MySQL
➢ Step1:
Make sure you already downloaded the MySQL essential 8.0.19 win32.msi file. Double click
on the .msi file.
➢ Step2:
This is MySQL Server 8.0 setup wizard. The setup wizard will install MySQL Server 8.0 release
8.0.45 on your computer. To continue, click next
➢ Step3:
Choose the setup type that best suits your needs. For common program features select Typical
and it’s recommended for general use. To continue, click next
➢ Step4:
This wizard is ready to begin installation. Destination folder will be in C:\Program
Files\MySQL\MySQL Server 8.0.19\. To continue, click next
➢ Step5:
The program features you selected are being installed. Please wait while the setup wizard installs
MySQL 8.0. This may take several minutes.
➢ Step6:
To continue, click next.
➢ Step7:
To continue, click next.
➢ Step8:
Wizard Completed. Setup has finished installing MySQL 8.0. Check the configure the MySQL
server now to continue. Click Finish to exit the wizard.
➢ Step9:
The configuration wizard will allow you to configure the MySQL Server 8.0 server instance
➢ Step10:
Select a standard configuration and this will use a general purpose configuration for the server
that can be tuned manually. To continue, click next.
➢ Step 11:
Check on the install as windows service and include bin directory in windows path. To continue,
click next.
➢ Step12:
Please set the security options by entering the root password and confirm retype the password.
To continue, click next.
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
➢ Step13:
Ready to execute ? Clicks execute to continue.
➢ Step14:
Processing configuration in progress.
➢ Step 15 :
Configuration file created. Windows service MySQL8.0 installed. Press finish to close the Wizard.
DDL COMMANDS:
CREATE TABLE Passenger(PNo int primary key,PName varchar (15),PAge
int ,PSex Char(6), p_seatnum int);
insert into Passenger values(1211, 'Apu', 25, 'Female',1);
insert into Passenger values(1212, 'karthik', 20, 'Male',2);
insert into Passenger values(1213, 'bindu', 14, 'Female',3);
insert into Passenger values(1214, 'surya', 65, 'Male',4);
update Passenger set PName = 'Manju' where PNo = 1211;
alter table Passenger modify PName varchar(25);
alter table Passenger drop p_seatnum;
select * from Passenger;
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
EXPERIMENT - 5
AIM: PRACTICING DML COMMANDS
CREATE TABLE Ticket(Ticket_No int Primary Key,Journey_date Date,
Age int, Sex char(7), Source Varchar(19),Arrival_time varchar(9),
Destination Varchar(15), Dep_time varchar(6));
insert into Ticket values(1, '2021-08-30', 39, 'F', 'HYD', '8:35', 'KHM', '6:05');
insert into Ticket values(2, '2021-06-25', 54, 'M', 'SEC', '6:35', 'VIZ', '8:40');
update Ticket set Destination = 'Warangal' where Ticket_No = 2;
delete from Ticket where Ticket_No = 2;
select * from Ticket;
CREATE TABLE Reservation(PNo int, foreign key(PNo) references
passenger(PNo),Journey_date date, No_of_seats int,
Address Varchar(55),Contact_No int,Status Char(2));
insert into Reservation values(1211, '2021-05-12', 2, 'gandhi nagar', 89765, 'R');
insert into Reservation values(1212, '2021-06-15', 2, 'Dilsukh nagar', 89432, 'R');
insert into Reservation values(1213, '2021-08-19', 2, 'Sunju nagar', 89435, 'R');
select * from Reservation;
show tables;
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
EXPERIMENT - 6
AIM: QUERYING (USING ANY, ALL, IN, EXISTS, NOT EXISTS, UNION, INTERSECT,
CONSTRAINTS ETC.)
create database 19wh1a1214;
use 19wh1a1214;
create table customers(customer_id int primary key,name varchar(20));
create table Reservations(id int primary key,customer_id int,day date,foreign
key(customer_id) references customers(customer_id));
insert into customers values(1,"MANJU");
insert into customers values(2,"BINDU");
insert into customers values(3,"KARU");
insert into customers values(4,"SRIYA");
select * from customers;
insert into Reservations values(1,1,'2021-07-30');
insert into Reservations values(2,2,'2021-02-17');
insert into Reservations values(3,2,'2021-05-26');
insert into Reservations values(4,1,'2021-06-22');
insert into Reservations values(5,3,'2021-03-19');
select * from Reservations;
select customer_id from customers where customer_id <> ALL(select customer_id from
Reservations);
select customer_id from customers where customer_id = ANY(select customer_id from
Reservations);
select customer_id from customers where customer_id = Some(select customer_id from
Reservations);
select customer_id from customers where customer_id IN (select customer_id from
Reservations);
select customer_id from customers where customer_id not in (select customer_id from
Reservations);
select customer_id from customers where customer_id in (3,4);
select customer_id as id,name as Customer_name from customers where customer_id
in(select customer_id from Reservations where customer_id>2);
select * from customers where customer_id between 1 and 2;
select customer_id from customers where customer_id < 4 union select customer_id from
Reservations where day='2021-06-26';
select customer_id from customers where customer_id < 4 or customer_id in (select
customer_id from Reservations where day='2021-06-26');
select customer_id from customers where customer_id < 4 and customer_id in (select
customer_id from Reservations where day='2021-06-28');
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
AIM:QUERYING USING CONSTRAINTS
create database employee;
USE employee;
create table Employee(ssn int primary key,emp_name varchar(15),age int default 20);
insert into Employee values(1201, 'Harika',19);
insert into Employee values(1202, 'bhavya',17);
insert into Employee values(1203, 'appu',null);
insert into Employee(ssn, emp_name) values(1204, 'MANJU');
create table student(roll_num int, name varchar(20),check(roll_num>0));insert
into student values(1, 'bhargavi');
select * from student;
select * from Employee;
create table student1(ht_num int primary key,name varchar(20));
insert into student1 values(1201, 'BINDU'), (1202, 'HIMABINDU'),(1203,'HIMA');
create table Event(event_id int primary key,event_name varchar(10),coord_id int, foreign
key(coord_id) references student1(ht_num));
insert into Event values(101, 'dance' , 1203),(102, 'singing', 1202),(103, 'skit', 1201);
select * from Event;
select * from student1;
show tables;
OUTPUT:
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
EXPERIMENT – 7
AIM:QUERIES USING AGGREGATE FUNCTIONS, GROUP BY, HAVING AND CREATION
AND DROPPING OF VIEWS.
create table student(htnum int primary key, name char(10), cgpa real);
insert into student values(1, 'HIMA BINDU', 9.5), (2, 'APARNA', 9.3), (3, 'SRIYA', 9);
select min(cgpa) as MIN_CGPA from student;
select max(cgpa) as MAX_CGPA from student;
select avg(cgpa) as AVG_CGPA from student;
select sum(cgpa) as sum_CGPA from student;
select count(*) from student;
select * from student where cgpa<(select max(cgpa) from student);
select name from student where cgpa>(select avg(cgpa) from student);
select count(*) from student where cgpa>(select avg(cgpa) from student);
select count(*) from student where cgpa<=(select avg(cgpa) from student);
select * from student order by cgpa;
select * from student order by cgpa desc;
OUTPUT:
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
create table menu(sno int primary key, name char(10), cost real, weight real);
insert into menu values(1, 'diarymilk', 345, 20), (2, 'kitkat', 1020, 100), (3, 'fish',
200, 10), (4, 'milk', 200, 50),(5, 'cakes', 345, 56), (6, 'cholates', 120, 50);
select name as food_item from menu group by(name) having sum(cost) > 200;
output:
create table student(htnum int primary key, name char(10), cgpa real, busnum
int);
insert into student values(1, 'manju', 9.5, 1), (2, 'appu', 9, 1), (3, 'hima', 8.8, 2), (4,
'sriya', 8.5, 3),(5, 'fathima', 9.7, 4);
create view details as select * from student;
select htnum, name, cgpa from details where cgpa< 9;
insert into details values(6, 'bindu', 9.7, 4);
select htnum, name from details;
update details set busnum = 7 where htnum = 1;
delete from details where htnum = 6;
select htnum, name, busnum from details;
0utput:
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
EXPERIMENT- 8
AIM: TRIGGERS
EG: CREATE TRIGGER UPDCHECK BEFORE UPDATE ON
PASSENGER FOR EACH ROW
BEGIN
IF NEW.TickentNO > 60 THEN
SET New.Tickent no = Ticket no;
ELSE SET New.Ticketno = 0;
END IF;
END;
mysql> create table employee (employeeNumber INT(11), lastName varchar(50), firstName
varchar(50), extension varchar(10), email varchar(100), officeCode varchar(10), reportsTo INT(11),
jobTitle varchar(50));
Query OK, 0 rows affected (0.09 sec)
mysql> select * from employee;
Empty set (0.00 sec)
mysql> desc employee;
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| employeeNumber | int(11) | YES | | NULL | |
| lastName | varchar(50) | YES | | NULL | |
| firstName | varchar(50) | YES | | NULL | |
| extension | varchar(10) | YES | | NULL | |
| email | varchar(100) | YES | | NULL | |
| officeCode | varchar(10) | YES | | NULL | |
| reportsTo | int(11) | YES | | NULL | |
| jobTitle | varchar(50) | YES | | NULL | |
+ + + + + + + 8 rows in set (0.01 sec)
mysql> CREATE TABLE employees_audit (
BVRIT HYDERABAD COLLEGE OF ENGINEERING FOR WOMEN
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> employeeNumber INT NOT NULL,
-> lastname VARCHAR(50) NOT NULL,
-> changedat DATETIME DEFAULT NULL,
-> action VARCHAR(50) DEFAULT NULL
-> );
Query OK, 0 rows affected (0.08 sec)
mysql> DELIMITER $$
mysql> CREATE TRIGGER before_employee_update
-> BEFORE UPDATE ON employee
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO employees_audit
-> SET action = 'update',
-> employeeNumber = OLD.employeeNumber,
-> lastname = OLD.lastname,
-> changedat = NOW();
-> END$$
mysql> DELIMITER ;
mysql> show triggers;
+ + + +
+ + +
+ + + + +
| Trigger | Event | Table | Statement
| Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database
Collation |
+ + + +
+ + +
+ + + + +
| before_employee_update | UPDATE | employee | BEGIN INSERT INTO employees_audit SET
action = 'update', employeeNumber = OLD.employeeNumber, lastname = OLD.lastname,
changedat = NOW(); END | BEFORE | NULL | | root@localhost | utf8 | utf8_general_ci
| latin1_swedish_ci |
+ + + +
+ + +
+ + + + +1
row in set (0.01 sec)
mysql> insert into employee values(1, 'Abhiram', 'Anuhya', '123', '[email protected]', '1234', 5678,
'Manager');
Query OK, 1 row affected (0.04 sec)
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
mysql> insert into employee values(2, 'Penmetsa', 'Anvitha', '145', '
[email protected]', '5234', 2334,
'Manager');
Query OK, 1 row affected (0.04 sec)
mysql> update employee
-> set lastName = 'Kandoor'
-> where employeeNumber = 1;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from employees_audit;
+ + + + + +
| id | employeeNumber | lastname | changedat | action |
+ + + + + +
| 1| 1 | Abhiram | 2017-03-30 10:35:35 | update |
+ + + + + +
1 row in set (0.00 sec)
mysql> select * from employee;
+ + + + + + + + +
| employeeNumber | lastName | firstName | extension | email | officeCode | reportsTo | jobTitle |
+ + + + + + + + +
| 1 | Kandoor | Anuhya | 123 |
[email protected] | 1234 | 5678 | Manager |
| 2 | Penmetsa | Anvitha | 145 |
[email protected] | 5234 | 2334 | Manager |
+ + + + + + + + +
2 rows in set (0.00 sec)
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
EXPERIMENT- 9
AIM: PROCEDURES
EG: CREATE PROCEDURE MYPROC() BEGIN
SELECT COUNT (Tickets) FROM Ticket WHERE age>=40;
End;
mysql> delimiter $
mysql> create procedure samp(in pnr numeric(10))
-> begin
-> select name from passenger where pnr_num=pnr;
-> end $
mysql> delimiter ;
mysql> call samp(23654);
// OUT parameter
mysql> delimiter $
mysql> create procedure samp_out(out c numeric(3))
-> begin
-> select count(*) into c from passenger;
-> end $
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call samp_out(@k);
Query OK, 1 row affected (0.00 sec)
mysql> select @k;
+ +
| @k |
+------+
| 3|
+ +
1 row in set (0.00 sec)
// IN OUT parameter
mysql> delimiter $
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
mysql> create procedure samp_in_out(in n varchar(15),out a numeric(4))
-> begin
-> select age into a from passenger where name=n;
-> end $
mysql> delimiter ;
call
samp_in_out('anvitha',@age);
select ( @age);
+ +
| ( @age) |
+ +
| 19 |
+ +
mysql> create procedure samp_out(out c numeric(3))
-> begin
-> select count(*) into c from passenger;
-> end $
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call samp_out(@k);
Query OK, 1 row affected (0.00 sec)
mysql> select @k;
+ +
| @k |
+------+
| 3|
+ +
1 row in set (0.00 sec)
mysql> delimiter $
mysql> create procedure samp(in pnr numeric(10))
-> begin
-> select name from passenger where pnr_num=pnr;
-> end $
mysql> delimiter ;
mysql> call
samp(23654);
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
EXPERIMENT -10
AIM: CURSORS
CREATE PROCEDURE MYPROC(IN_CUSTOMER_ID INT) BEGIN
DECLARE v_id INT;
DECLARE v_name VARCHAR (30);
DECLARE c1 CURSOR FOR SELECT stdId,stdFirstname FROM students WHERE
stdId=in_customer_id;
OPEN c1;
FETCH c1 into v_id, v_name; Close c1;
END;
mysql> create table employees(eid integer(4),email varchar(100));
Query OK, 0 rows affected (0.07 sec)
mysql> insert into employees values(125,'[email protected]');
Query OK, 1 row affected (0.03 sec)
mysql> insert into employees values(123,'
[email protected]');
Query OK, 1 row affected (0.03 sec)
mysql> select * from employees;
+ + +
| eid | email |
+ + +
| 125 |
[email protected] |
| 123 |
[email protected] |
+ + +
1 rows in set (0.00 sec)
mysql> DELIMITER $$
mysql>
mysql> CREATE PROCEDURE build_email_list (INOUT email_list varchar(4000))
-> BEGIN
->
-> DECLARE v_finished INTEGER DEFAULT 0;
-> DECLARE v_email varchar(100) 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
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
->
-> 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.01 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, 1 warning (0.00 sec)
mysql> SELECT @email_list;
+ +
| @email_list |
+ +
|
[email protected];
[email protected]; |
+ +
1 row in set (0.00 sec)
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
(Beyond the Syllabus)
EXTRA QUERIES: WORKING ON EMP AND DEPT TABLES
Mysql>drop table if exists emp;
Mysql>create table emp (
empno decimal(4,0) primary key,
ename varchar(10) default null,
job varchar(9) default null,
mgr decimal(4,0) references empno(emp),
hiredate date default null,
sal decimal(7,2) default null,
comm decimal(7,2) default null,
deptno decimal(2,0) references deptno(dept)
);
Mysql>drop table if exists dept;
Mysql>create table dept (
deptno decimal(2,0) primary key,
dname varchar(14) default null,
loc varchar(13) default null
);
Mysql>insert into emp values ('7369','smith','clerk','7902','1980-12-17','800.00',null,'20');
Mysql>insert into emp values ('7499','allen','salesman','7698','1981-02-20','1600.00','300.00','30');
Mysql>insert into emp values ('7521','ward','salesman','7698','1981-02-22','1250.00','500.00','30');
Mysql>insert into emp values ('7566','jones','manager','7839','1981-04-02','2975.00',null,'20');
Mysql>insert into emp values ('7654','martin','salesman','7698','1981-09-28','1250.00','1400.00','30');
Mysql>insert into emp values ('7698','blake','manager','7839','1981-05-01','2850.00',null,'30');
Mysql>insert into emp values ('7782','clark','manager','7839','1981-06-09','2450.00',null,'10');
Mysql>insert into emp values ('7788','scott','analyst','7566','1982-12-09','3000.00',null,'20');
Mysql>insert into emp values ('7839','king','president',null,'1981-11-17','5000.00',null,'10');
Mysql>insert into emp values ('7844','turner','salesman','7698','1981-09-08','1500.00','0.00','30');
Mysql>insert into emp values ('7876','adams','clerk','7788','1983-01-12','1100.00',null,'20');
Mysql>insert into emp values ('7900','james','clerk','7698','1981-12-03','950.00',null,'30');
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
Mysql>insert into emp values ('7902','ford','analyst','7566','1981-12-03','3000.00',null,'20');
Mysql>insert into emp values ('7934','miller','clerk','7782','1982-01-23','1300.00',null,'10');
Mysql>select * from emp;
+ + + + + + + + +
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+ + + + + + + + +
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1983-01-12 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
+ + + + + + + + +
14 rows in set (0.00 sec)
Mysql>insert into dept values ('10','accounting','new york');
Mysql>insert into dept values ('20','research','dallas');
Mysql>insert into dept values ('30','sales','chicago');
Mysql>insert into dept values ('40','operations','boston');
Mysql>select * from dept;
+ + + +
| deptno | dname | loc |
+ + + +
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+ + + +
4 rows in set (0.00 sec)
Mysql>select empno,mgr from emp;
+ + +
| empno | mgr |
+ + +
| 7369 | 7902 |
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
| 7499 | 7698 |
| 7521 | 7698 |
| 7566 | 7839 |
| 7654 | 7698 |
| 7698 | 7839 |
| 7782 | 7839 |
| 7788 | 7566 |
| 7839 | NULL |
| 7844 | 7698 |
| 7876 | 7788 |
| 7900 | 7698 |
| 7902 | 7566 |
| 7934 | 7782 |
14 rows in set (0.00 sec)
Mysql>select empno,ename from emp where sal > 5000;
Empty set (0.00 sec)
Mysql> select empno,ename,sal from emp where sal > 3000;
+ + + +
| empno | ename | sal |
+ + + +
| 7369 | SMITH | 800.00 |
| 7499 | ALLEN | 1600.00 |
| 7521 | WARD | 1250.00 |
| 7566 | JONES | 2975.00 |
| 7654 | MARTIN | 1250.00 |
| 7698 | BLAKE | 2850.00 |
| 7782 | CLARK | 2450.00 |
| 7788 | SCOTT | 3000.00 |
| 7839 | KING | 5000.00 |
| 7844 | TURNER | 1500.00 |
| 7876 | ADAMS | 1100.00 |
| 7900 | JAMES | 950.00 |
| 7902 | FORD | 3000.00 |
| 7934 | MILLER | 1300.00 |
+ + + +
14 rows in set (0.00 sec)
Mysql> select empno,ename,sal from emp where sal < 5000;
+ + + +
| empno | ename | sal |
+ + + +
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
| 7369 | SMITH | 800.00 |
| 7499 | ALLEN | 1600.00 |
| 7521 | WARD | 1250.00 |
| 7566 | JONES | 2975.00 |
| 7654 | MARTIN | 1250.00 |
| 7698 | BLAKE | 2850.00 |
| 7782 | CLARK | 2450.00 |
| 7788 | SCOTT | 3000.00 |
| 7844 | TURNER | 1500.00 |
| 7876 | ADAMS | 1100.00 |
| 7900 | JAMES | 950.00 |
| 7902 | FORD | 3000.00 |
| 7934 | MILLER | 1300.00 |
+ + + +
13 rows in set (0.00 sec)
Mysql>select empno,ename,sal from emp where sal = 5000;
+ + + +
| empno | ename | sal |
+ + + +
| 7839 | KING | 5000.00 |
+ + + +
1 row in set (0.00 sec)
MySQL>select empno,ename,comm from emp where comm <= 500;
+ + + +
| empno | ename | comm |
+ + + +
| 7499 | ALLEN | 300.00 |
| 7521 | WARD | 500.00 |
| 7844 | TURNER | 0.00 |
+ + + +
3 rows in set (0.00 sec)
MySQL>select empno,ename,comm from emp where comm <> 500;
+ + + +
| empno | ename | comm |
+ + + +
| 7499 | ALLEN | 300.00 |
| 7654 | MARTIN | 1400.00 |
| 7844 | TURNER | 0.00 |
+ + + +
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
3 rows in set (0.00 sec)
MySQL>select empno,ename,sal from emp where sal between 1000 and 2000;
+ + + +
| empno | ename | sal |
+ + + +
| 7499 | ALLEN | 1600.00 |
| 7521 | WARD | 1250.00 |
| 7654 | MARTIN | 1250.00 |
| 7844 | TURNER | 1500.00 |
| 7876 | ADAMS | 1100.00 |
| 7934 | MILLER | 1300.00 |
+ + + +
6 rows in set (0.00 sec)
MySQL>select empno,ename,sal from emp where sal in( 1000, 2000);
Empty set (0.00 sec)
MySQL>select empno,ename,sal from emp where sal in( 1250, 1600);
+ + + +
| empno | ename | sal |
+ + + +
| 7499 | ALLEN | 1600.00 |
| 7521 | WARD | 1250.00 |
| 7654 | MARTIN | 1250.00 |
+ + + +
3 rows in set (0.00 sec)
MySQL> select deptno,dname,loc from dept where loc = 'BOSTON';
+ + + +
| deptno | dname | loc |
+ + + +
| 40 | OPERATIONS | BOSTON |
+ + + +
1 row in set (0.00 sec)
MySQL>select *from dept where loc like 'C%';
+ + + +
| deptno | dname | loc |
+ + + +
| 30 | SALES | CHICAGO |
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
+ + + +
1 row in set (0.00 sec)
MySQL>select *from dept where loc like '%S';
+ + + +
| deptno | dname | loc |
+ + + +
| 20 | RESEARCH | DALLAS |
+ + + +
1 row in set (0.00 sec)
MySQL>select empno,sal from emp order by sal;
+ + +
| empno | sal |
+ + +
| 7369 | 800.00 |
| 7900 | 950.00 |
| 7876 | 1100.00 |
| 7521 | 1250.00 |
| 7654 | 1250.00 |
| 7934 | 1300.00 |
| 7844 | 1500.00 |
| 7499 | 1600.00 |
| 7782 | 2450.00 |
| 7698 | 2850.00 |
| 7566 | 2975.00 |
| 7902 | 3000.00 |
| 7788 | 3000.00 |
| 7839 | 5000.00 |
+ + +
14 rows in set (0.00 sec)
MySQL>select empno,sal from emp order by sal desc ;
+ + +
| empno | sal |
+ + +
| 7839 | 5000.00 |
| 7788 | 3000.00 |
| 7902 | 3000.00 |
| 7566 | 2975.00 |
| 7698 | 2850.00 |
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
| 7782 | 2450.00 |
| 7499 | 1600.00 |
| 7844 | 1500.00 |
| 7934 | 1300.00 |
| 7654 | 1250.00 |
| 7521 | 1250.00 |
| 7876 | 1100.00 |
| 7900 | 950.00 |
| 7369 | 800.00 |
+ + +
14 rows in set (0.00 sec)
MySQL>select sum(sal) from emp ;
+ +
| sum(sal) |
+ +
| 29025.00 |
+ +
1 row in set (0.00 sec)
MySQL>select empno,ename,min(sal) from emp ;
+ + + +
| empno | ename | min(sal) |
+ + + +
| 7369 | SMITH | 800.00 |
+ + + +
1 row in set (0.00 sec)
MySQL>select max(sal) from emp ;
+ +
| max(sal) |
+ +
| 5000.00 |
+ +
1 row in set (0.00 sec)
MySQL>select avg(sal) from emp ;
+ +
| avg(sal) |
+ +
| 2073.214286 |
+ +
1 row in set (0.00 sec)
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
MySQL>select count(sal) from emp ;
+ +
| count(sal) |
+ +
| 14 |
+ +
1 row in set (0.00 sec)
MySQL>select count(comm) from emp ;
+ +
| count(comm) |
+ +
| 4|
+ +
1 row in set (0.00 sec)
MySQL>select sal+sal*15/100 as sal from emp;
+ +
| sal |
+ +
| 920.000000 |
| 1840.000000 |
| 1437.500000 |
| 3421.250000 |
| 1437.500000 |
| 3277.500000 |
| 2817.500000 |
| 3450.000000 |
| 5750.000000 |
| 1725.000000 |
| 1265.000000 |
| 1092.500000 |
| 3450.000000 |
| 1495.000000 |
+ +
14 rows in set (0.00 sec)
MySQL>select count(distinct sal) from emp;
+ +
| count(distinct sal) |
+ +
| 12 |
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
+ +
1 row in set (0.01 sec)
MySQL>select sal+sal*15/100 from emp;
+ +
| sal+sal*15/100 |
+ +
| 920.000000 |
| 1840.000000 |
| 1437.500000 |
| 3421.250000 |
| 1437.500000 |
| 3277.500000 |
| 2817.500000 |
| 3450.000000 |
| 5750.000000 |
| 1725.000000 |
| 1265.000000 |
| 1092.500000 |
| 3450.000000 |
| 1495.000000 |
+ +
14 rows in set (0.00 sec)
MySQL>select deptno from emp union select deptno from dept;
+ +
| deptno |
+ +
| 20 |
| 30 |
| 10 |
| 40 |
+ +
4 rows in set (0.00 sec)
MySQL>select sal from emp group by deptno;
+ +
| sal |
+ +
| 2450.00 |
| 800.00 |
| 1600.00 |
+ +
BVRIT HYDERABAD College Of Engineering For Women
Department Of Information DATE:
PAGE NO:
ROLL NUMBER:20WH1A1273
3 rows in set (0.00 sec)
MySQL>select deptno,sum(sal) from emp group by deptno;
+ + +
| deptno | sum(sal) |
+ + +
| 10 | 8750.00 |
| 20 | 10875.00 |
| 30 | 9400.00 |
+ + +
3 rows in set (0.00 sec)
MySQL>select deptno,sum(sal) from emp group by deptno having deptno = 10;
+ + +
| deptno | sum(sal) |
+ + +
| 10 | 8750.00 |
+ + +
1 row in set (0.00 sec)
BVRIT HYDERABAD College Of Engineering For Women
tems Lab Manual II B.Tech II Sem
BVRIT HYDERABAD College Of Engineering For Women