You are on page 1of 38

NAME—RIMSHA

ROLL NO—21/1278
PAPER- DATABASE MANAGEMENT SYSTEM

PRACTICAL QUESTIONS

QUESTION 1.
Create a database having two tables with the specified fields, to computerize a
library system of a Delhi University College.
LibraryBooks (Accession number, Title, Author, Department, PurchaseDate,
Price)
IssuedBooks (Accession number, Borrower)
mysql>create database lib;

mysql> use lib;

Database changed

mysql> create table Library_Books(Acc_No int, Title varchar(38), Author varchar(38), Department
varchar(38),Purchase_Date date, Price int)

-> ;

Query OK, 0 rows affected (2.52 sec)

mysql> alter table Library_Books

-> add primary key(Acc_No);


Query OK, 0 rows affected (1.70 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> desc Library_Books;

+---------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------------+-------------+------+-----+---------+-------+

| Acc_No | int | NO | PRI | NULL | |

| Title | varchar(38) | YES | | NULL | |

| Author | varchar(38) | YES | | NULL | |

| Department | varchar(38) | YES | | NULL | |

| Purchase_Date | date | YES | | NULL | |

| Price | int | YES | | NULL | |

+---------------+-------------+------+-----+---------+-------+

6 rows in set (0.38 sec)

mysql> create table Issued_Books(Acc_No int, Borrower varchar(38));

Query OK, 0 rows affected (0.37 sec)

mysql> insert into Library_Books values(1210,"Database Systems Concepts","Korth","CS","2021-10-


15",400);

Query OK, 1 row affected (0.15 sec)

mysql> insert into Library_Books values(1211,"computer networks","william stallings","ECE","2020-11-


09",350),(1212,"Database Management System","Navathe","CS","2020-09-20",500),(1213,"Automata
Theory","Peter Linz","IT","2018-12-15",600);

Query OK, 3 rows affected (0.18 sec)

Records: 3 Duplicates: 0 Warnings: 0


mysql> select * from Library_Books;

+--------+----------------------------+-------------------+------------+---------------+-------+

| Acc_No | Title | Author | Department | Purchase_Date | Price |

+--------+----------------------------+-------------------+------------+---------------+-------+

| 1210 | Database Systems Concepts | Korth | CS | 2021-10-15 | 400 |

| 1211 | computer networks | william stallings | ECE | 2020-11-09 | 350 |

| 1212 | Database Management System | Navathe | CS | 2020-09-20 | 500 |

| 1213 | Automata Theory | Peter Linz | IT | 2018-12-15 | 600 |

+--------+----------------------------+-------------------+------------+---------------+-------+

4 rows in set (0.00 sec)

mysql> insert into Library_Books values(1209,"discrete maths","R D Sharma","CS","2021-08-18", 200);

Query OK, 1 row affected (0.07 sec)

mysql> select * from Library_Books;

+--------+----------------------------+-------------------+------------+---------------+-------+

| Acc_No | Title | Author | Department | Purchase_Date | Price |

+--------+----------------------------+-------------------+------------+---------------+-------+

| 1209 | discrete maths | R D Sharma | CS | 2021-08-18 | 200 |

| 1210 | Database Systems Concepts | Korth | CS | 2021-10-15 | 400 |

| 1211 | computer networks | william stallings | ECE | 2020-11-09 | 350 |

| 1212 | Database Management System | Navathe | CS | 2020-09-20 | 500 |

| 1213 | Automata Theory | Peter Linz | IT | 2018-12-15 | 600 |

+--------+----------------------------+-------------------+------------+---------------+-------+

5 rows in set (0.00 sec)

mysql> insert into IssuedBooks values(1212,"Disha"),(1213,"Vidya"),(1210,"Asha");

Query OK, 3 rows affected (0.15 sec)


Records: 3 Duplicates: 0 Warnings: 0

mysql> insert into Issued_Books values(1212,"Disha"),(1213,"Vidya"),(1210,"Asha");

Query OK, 3 rows affected (0.51 sec)

Records: 3 Duplicates: 0 Warnings: 0

mysql> insert into Issued_Books values(1209, "Meena"),(1211,"Shee");

Query OK, 2 rows affected (0.14 sec)

Records: 2 Duplicates: 0 Warnings: 0

mysql> select * from Issued_Books;

+--------+----------+

| Acc_No | Borrower |

+--------+----------+

| 1212 | Disha |

| 1213 | Vidya |

| 1210 | Asha |

| 1209 | Meena |

| 1211 | Shee |

+--------+----------+

5 rows in set (0.00 sec)

B))
Delete the record of book titled “Database System Concepts”.

mysql> delete from Library_Books where Title="Database Systems Concepts";


Query OK, 1 row affected (0.17 sec)

mysql> select * from Library_Books;

+--------+----------------------------+-------------------+------------+---------------+-------+

| Acc_No | Title | Author | Department | Purchase_Date | Price |

+--------+----------------------------+-------------------+------------+---------------+-------+

| 1209 | discrete maths | R D Sharma | CS | 2021-08-18 | 200 |

| 1211 | computer networks | william stallings | ECE | 2020-11-09 | 350 |

| 1212 | Database Management System | Navathe | CS | 2020-09-20 | 500 |

| 1213 | Automata Theory | Peter Linz | IT | 2018-12-15 | 600 |

+--------+----------------------------+-------------------+------------+---------------+-------+

4 rows in set (0.00 sec)

C))
CHANGE THE DEPARTMENT OF THE BOOK TITLED “DTATABASE MANAGEMENT
CONCEPTS”.

mysql> update Library_Books set Department="CS" where Title="discrete maths";

Query OK, 0 rows affected (0.08 sec)

Rows matched: 1 Changed: 0 Warnings: 0

mysql> select * from Library_Books;

+--------+----------------------------+-------------------+------------+---------------+-------+

| Acc_No | Title | Author | Department | Purchase_Date | Price |

+--------+----------------------------+-------------------+------------+---------------+-------+

| 1209 | discrete maths | R D Sharma | CS | 2021-08-18 | 200 |

| 1211 | computer networks | william stallings | ECE | 2020-11-09 | 350 |

| 1212 | Database Management System | Navathe | CS | 2020-09-20 | 500 |


| 1213 | Automata Theory | Peter Linz | IT | 2018-12-15 | 600 |

+--------+----------------------------+-------------------+------------+---------------+-------+

4 rows in set (0.00 sec)

D))
LIST ALL BOOKS THAT BELONG TO “CS” DEPARTMENT.
mysql> select * from Library_Books where Department="CS";

+--------+----------------------------+------------+------------+---------------+-------+

| Acc_No | Title | Author | Department | Purchase_Date | Price |

+--------+----------------------------+------------+------------+---------------+-------+

| 1209 | discrete maths | R D Sharma | CS | 2021-08-18 | 200 |

| 1212 | Database Management System | Navathe | CS | 2020-09-20 | 500 |

+--------+----------------------------+------------+------------+---------------+-------+

2 rows in set (0.00 sec)

E)) LIST ALL BOOKS THAT BELONG TO “CS” DEPARTMENT AND ARE
WRITTEN BY AUTHOR “NAVATHE”.

mysql> select * from Library_Books where Department="CS" and author="Navathe";

+--------+----------------------------+---------+------------+---------------+-------+

| Acc_No | Title | Author | Department | Purchase_Date | Price |

+--------+----------------------------+---------+------------+---------------+-------+

| 1212 | Database Management System | Navathe | CS | 2020-09-20 | 500 |

+--------+----------------------------+---------+------------+---------------+-------+

1 row in set (0.00 sec)


F))
LIST ALL BOOKS WHICH HAVE A PRICE LESS THAN 500 OR PURCHASED BETWEEN
“01/01/1999” AND “01/01/2004”.

mysql> select title from Library_Books as lb, Issued_Books as ib where Department="CS" and
lb.Acc_No=ib.Acc_No;

+----------------------------+

| title |

+----------------------------+

| Database Management System |

| discrete maths |

+----------------------------+

2 rows in set (0.08 sec)

QUESTION2

Create a database having three tables to store the details of students


of Computer Department in your college, as per the given schema.
Personal information about
Student (College roll number, Name of student, Date of birth,
Address, Marks(rounded off to whole number) in percentage at 10 +
2, Phone number)
Paper Details (Paper code, Name of the Paper)
Student’s Academic and Attendance details (College roll number,
Paper code, Attendance, Marks in home examination).
mysql> create database SI;

Query OK, 1 row affected (0.21 sec)

mysql> use SI;

Database changed

mysql> create table PI(clg_roll varchar(3),name varchar(255),dob date, address varchar(38),marks


int,phone_no varchar(10),primary key(clg_roll));

Query OK, 0 rows affected (0.70 sec)

mysql> create table paper_details(paper_code varchar(3), paper_name varchar(255),primary


key(paper_code));

Query OK, 0 rows affected (0.35 sec)

mysql> create table AAI(clg_roll varchar(3), paper_code varchar(3), attendance int, marks int, constraint
FK1 Foreign Key(clg_roll) references PI(clg_roll), Constraint FK2 Foreign Key(paper_code) references
paper_details(paper_code), primary key(clg_roll,paper_code));

Query OK, 0 rows affected (0.49 sec)

mysql> desc PI;

+----------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+--------------+------+-----+---------+-------+

| clg_roll | varchar(3) | NO | PRI | NULL | |

| name | varchar(255) | YES | | NULL | |

| dob | date | YES | | NULL | |

| address | varchar(38) | YES | | NULL | |

| marks | int | YES | | NULL | |


| phone_no | varchar(10) | YES | | NULL | |

+----------+--------------+------+-----+---------+-------+

6 rows in set (0.06 sec)

mysql> desc AAI;

+------------+------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+------------+------+-----+---------+-------+

| clg_roll | varchar(3) | NO | PRI | NULL | |

| paper_code | varchar(3) | NO | PRI | NULL | |

| attendance | int | YES | | NULL | |

| marks | int | YES | | NULL | |

+------------+------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

mysql> desc paper_details;

+------------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+--------------+------+-----+---------+-------+

| paper_code | varchar(3) | NO | PRI | NULL | |

| paper_name | varchar(255) | YES | | NULL | |

+------------+--------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

mysql> insert into PI values("1","Heena","2006-06-12","Delhi",70,"9898876587"),("2","Disha","2005-07-


08","Jaipur",74,"9878879098"),("3","Asha","2006-01-03","Delhi",80,"9878898709"),("4","Priya","2006-
08-23","Delhi",85,"8988876789"),("5","Seema","2005-06-04","Agra",72,"9887678950");

Query OK, 5 rows affected (0.15 sec)

Records: 5 Duplicates: 0 Warnings: 0


mysql> select * from PI;

+----------+-------+------------+---------+-------+------------+

| clg_roll | name | dob | address | marks | phone_no |

+----------+-------+------------+---------+-------+------------+

|1 | Heena | 2006-06-12 | Delhi | 70 | 9898876587 |

|2 | Disha | 2005-07-08 | Jaipur | 74 | 9878879098 |

|3 | Asha | 2006-01-03 | Delhi | 80 | 9878898709 |

|4 | Priya | 2006-08-23 | Delhi | 85 | 8988876789 |

|5 | Seema | 2005-06-04 | Agra | 72 | 9887678950 |

+----------+-------+------------+---------+-------+------------+

5 rows in set (0.00 sec)

mysql> insert into paper_details values("P1","Introduction of Computers"),("P2","THEORETICAL


COMPUTER SCIENCE"),("P3","Database System"),("P4","Computer Networks"),("P5","MultiMedia
System");

Query OK, 5 rows affected (0.08 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from paper_details;

+------------+------------------------------+

| paper_code | paper_name |

+------------+------------------------------+

| P1 | Introduction of Computers |

| P2 | THEORETICAL COMPUTER SCIENCE |

| P3 | Database System |

| P4 | Computer Networks |

| P5 | MultiMedia System |

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

mysql> insert into AAI values(1,'P1',75,87),(1,'P2',67,89),(1,'P4',80,67),(2,'P2',87,78),(2,'P5',85,76),


(3,'P1',77,59),(3,'P3',90,90),(4,'P2',79,69),(4,'P4',84,88),(5,'P2',68,55),(5,'P3',54,58);

Query OK, 11 rows affected (0.06 sec)

Records: 11 Duplicates: 0 Warnings: 0

mysql> desc AAI;

+------------+------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+------------+------+-----+---------+-------+

| clg_roll | varchar(3) | NO | PRI | NULL | |

| paper_code | varchar(3) | NO | PRI | NULL | |

| attendance | int | YES | | NULL | |

| marks | int | YES | | NULL | |

+------------+------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

mysql> select * from AAI;

+----------+------------+------------+-------+

| clg_roll | paper_code | attendance | marks |

+----------+------------+------------+-------+

|1 | P1 | 75 | 87 |

|1 | P2 | 67 | 89 |

|1 | P4 | 80 | 67 |

|2 | P2 | 87 | 78 |
|2 | P5 | 85 | 76 |

|3 | P1 | 77 | 59 |

|3 | P3 | 90 | 90 |

|4 | P2 | 79 | 69 |

|4 | P4 | 84 | 88 |

|5 | P2 | 68 | 55 |

|5 | P3 | 54 | 58 |

+----------+------------+------------+-------+

11 rows in set (0.00 sec)

B))
DESIGN A QUERY THAT WILL RETURN THE RECORDS ALONG WOTH THE NAME
OF THE STUDENT FROM THE FIRST TABLE, RELATED TO STUDENTS WHO HAVE
MORE THAN 75% ATTENDANCE AND MORE THAN 60%MARKS IN PAPER2.

mysql> select P.*, I.name from paper_details as P, PI as I, AAI as V where I.clg_roll=V.clg_roll and
P.paper_code=V.paper_code and V.attendance>75 and V.marks>60 and P.paper_code="P2";

+------------+------------------------------+-------+

| paper_code | paper_name | name |

+------------+------------------------------+-------+

| P2 | THEORETICAL COMPUTER SCIENCE | Disha |

| P2 | THEORETICAL COMPUTER SCIENCE | Priya |

+------------+------------------------------+-------+

2 rows in set (0.05 sec)


C))
LIST ALL STUDENTS WHO LIVE IN “DELHI” AND HAVE AMRKS GREATER
THAN 60 IN PAPER 1.

mysql> select I.name, V.marks as PI_marks, I.address from paper_details as P, PI as I, AAI as V where
I.clg_roll=V.clg_roll and P.paper_code=V.paper_code and I.address="Delhi" and V.marks>60 and
P.paper_code="P1";

+-------+----------+---------+

| name | PI_marks | address |

+-------+----------+---------+

| Heena | 87 | Delhi |

+-------+----------+---------+

1 row in set (0.00 sec)

D))
FIND THE TOTAL ATTENDANCE AND TOTAL MARKS OBTAINED BY
EACH STUDENT.

mysql> select I.clg_roll, I.name, Sum(V.marks) as TotalMarks, Sum(V.attendance) as TotalAttendance


from PI as I, AAI as V where I.clg_roll=V.clg_roll group by V.clg_roll;

+----------+-------+------------+-----------------+

| clg_roll | name | TotalMarks | TotalAttendance |

+----------+-------+------------+-----------------+

|1 | Heena | 243 | 222 |

|2 | Disha | 154 | 172 |

|3 | Asha | 149 | 167 |


|4 | Priya | 157 | 163 |

|5 | Seema | 113 | 122 |

+----------+-------+------------+-----------------+

5 rows in set (0.00 sec)

E))
LIST THE NAME OF STUDENT WHO HAS GOT THE HIGHEST MARKS IN
PAPER2.

mysql> select I.clg_roll, I.name, max(V.marks) as Highest_Marks from paper_details as P, PI as I, AAI as V


where I.clg_roll=V.clg_roll and P.paper_code=V.paper_code and P.paper_code="P2";

+----------+-------+---------------+

| clg_roll | name | Highest_Marks |

+----------+-------+---------------+

|1 | Heena | 89 |

+----------+-------+---------------+

1 row in set (0.05 sec)

QUESTION3.

Create the following tables and answer the queries given below:
Customer (CustID, email, Name, Phone, ReferrerID) Bicycle (BicycleID,
DatePurchased, Color, CustID, ModelNo) BicycleModel (ModelNo,
Manufacturer, Style) Service (StartDate, BicycleID, EndDate)

mysql> create table Customer(CustID varchar(30), email varchar(30), Name varchar(40), Phone
varchar(40),ReferrerID varchar(30));

Query OK, 0 rows affected (0.82 sec)

mysql> create table BicycleModel(ModelNo varchar(30), Manufacturer varchar(30), Style varchar(50));

Query OK, 0 rows affected (0.62 sec)

mysql> create table Service(StartDate date, BicycleID varchar(30), EndDate date);

Query OK, 0 rows affected (0.48 sec)

mysql> insert into Customer values("C1","disha22@gmail.com","Disha","8777899400","C1"),


("C2","asha@gmail.com","Asha","9866678769","C1"),
("C3","arpita@gmai.com","Arpita","8776856730","C2"),
("C4","priya@gmail.com","Priya","8276856990","C1"),
("C5","hinag@gmail,com","Hina","8276766790","C4");

Query OK, 5 rows affected (0.15 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> insert into BicycleModel values("BM0001","HONDA","MOUNTAIN BIKE"),


("BM0002","MARUTI","DUTCH BIKE");

Query OK, 2 rows affected (0.14 sec)

Records: 2 Duplicates: 0 Warnings: 0

mysql> insert into BicycleModel values("BM0003","Bajaj","Road Bike"),("BM0004","FIREFOX","SPORTS


BIKE"),("BM0005","HONDA","KIDS BIKE");

Query OK, 3 rows affected (0.04 sec)

Records: 3 Duplicates: 0 Warnings: 0


mysql> insert into Bicycle; values("B1","2021/09/08","Red","C1","BM0001"),
("B2","2020/08/05","Blue","C3","BM0004"),("B3","2021/08/13","Black","C1","BM0005"),
("B4","2020/08/17","Grey","C2","BM0001"),("B5","2018/08/14","Green","C4","BM0005"),
("B6","2016/04/11","Orange","C5","BM0003");

Query OK, 6 rows affected, 6 warnings (0.11 sec)

Records: 6 Duplicates: 0 Warnings: 6

mysql> insert into Service; values("2022/01/03","B1","2022/01/05"),("2021/02/06","B1","2021/02/07"),


("2021/03/16","B2","2021/03/18"),("2022/03/13","B6","2022/03/16"),
("2022/04/20","B4","2022/04/22");

Query OK, 5 rows affected, 10 warnings (0.10 sec)

Records: 5 Duplicates: 0 Warnings: 10

mysql> select * from Customer;

+--------+-------------------+--------+------------+------------+

| CustID | email | Name | Phone | ReferrerID |

+--------+-------------------+--------+------------+------------+

| C1 | disha22@gmail.com | Disha | 8777899400 | C1 |

| C2 | asha@gmail.com | Asha | 9866678769 | C1 |

| C3 | arpita@gmai.com | Arpita | 8776856730 | C2 |

| C4 | priya@gmail.com | Priya | 8276856990 | C1 |

| C5 | hinag@gmail,com | Hina | 8276766790 | C4 |

+--------+-------------------+--------+------------+------------+

5 rows in set (0.00 sec)

mysql> select * from Bicycle;

+-----------+---------------+--------+--------+---------+

| BicycleID | DatePurchased | Color | CustID | ModelNo |

+-----------+---------------+--------+--------+---------+

| B1 | 2021-09-08 | Red | C1 | BM0001 |


| B2 | 2020-08-05 | Blue | C3 | BM0004 |

| B3 | 2021-08-13 | Black | C1 | BM0005 |

| B4 | 2020-08-17 | Grey | C2 | BM0001 |

| B5 | 2018-08-14 | Green | C4 | BM0005 |

| B6 | 2016-04-11 | Orange | C5 | BM0003 |

+-----------+---------------+--------+--------+---------+

6 rows in set (0.00 sec)

mysql> select * from BicycleModel;

+---------+--------------+---------------+

| ModelNo | Manufacturer | Style |

+---------+--------------+---------------+

| BM0001 | HONDA | MOUNTAIN BIKE |

| BM0002 | MARUTI | DUTCH BIKE |

| BM0003 | Bajaj | Road Bike |

| BM0004 | FIREFOX | SPORTS BIKE |

| BM0005 | HONDA | KIDS BIKE |

+---------+--------------+---------------+

5 rows in set (0.00 sec)

mysql> select * from Service;

+------------+-----------+------------+

| StartDate | BicycleID | EndDate |

+------------+-----------+------------+

| 2022-01-03 | B1 | 2022-01-05 |

| 2021-02-06 | B1 | 2021-02-07 |

| 2021-03-16 | B2 | 2021-03-18 |

| 2022-03-13 | B6 | 2022-03-16 |

| 2022-04-20 | B4 | 2022-04-22 |
+------------+-----------+------------+

5 rows in set (0.00 sec)

mysql> Alter table Customer

-> add primary key(CustID);

Query OK, 0 rows affected (1.72 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> Alter table Bicycle

-> add primary key(BicycleID);

Query OK, 0 rows affected (1.09 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> Alter table Bicycle

-> add constraint FK1 foreign key(CustID) references Customer(CustID);

Query OK, 6 rows affected (1.47 sec)

Records: 6 Duplicates: 0 Warnings: 0

mysql> Alter table BicycleModel

-> add primary key(ModelNo)

-> ;

Query OK, 0 rows affected (0.80 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> Alter table Bicycle

-> add constraint FK2 foreign key(ModelNo) references BicycleModel(ModelNo);

Query OK, 6 rows affected (1.90 sec)

Records: 6 Duplicates: 0 Warnings: 0


mysql> Alter table Service

-> add constraint FK3 foreign key(BicycleID) references Bicycle(BicycleID);

Query OK, 5 rows affected (0.99 sec)

Records: 5 Duplicates: 0 Warnings: 0

B))
All the customers who have bikes manufactured by honda:

mysql> select distinct c.* from Customer as C, BicycleModel as BM, Bicycle as B where
C.CustID=B.CustID and B.ModelNo=BM.ModelNo and BM.Manufacturer="Honda";

+--------+-------------------+-------+------------+------------+

| CustID | email | Name | Phone | ReferrerID |

+--------+-------------------+-------+------------+------------+

| C1 | disha22@gmail.com | Disha | 8777899400 | C1 |

| C2 | asha@gmail.com | Asha | 9866678769 | C1 |

| C4 | priya@gmail.com | Priya | 8276856990 | C1 |

+--------+-------------------+-------+------------+------------+

C))
Bicycles purchased by customers referred to by "C1":
mysql> select B.* from Bicycle as B, Customer as C, BicycleModel as BM where C.CustID=B.CustID and
B.ModelNo=BM.ModelNo and C.ReferrerID="C1";

+-----------+---------------+-------+--------+---------+

| BicycleID | DatePurchased | Color | CustID | ModelNo |

+-----------+---------------+-------+--------+---------+

| B1 | 2021-09-08 | Red | C1 | BM0001 |

| B3 | 2021-08-13 | Black | C1 | BM0005 |

| B4 | 2020-08-17 | Grey | C2 | BM0001 |

| B5 | 2018-08-14 | Green | C4 | BM0005 |

+-----------+---------------+-------+--------+---------+

4 rows in set (0.00 sec)

D)) Manufacturer of red coloured bicycles

mysql> select BM.Manufacturer from Bicycle as B, BicycleModel as BM where B.ModelNo=BM.ModelNo


and B.Color="Red";

+--------------+

| Manufacturer |

+--------------+

| HONDA |

+--------------+

1 row in set (0.00 sec)

E))
Models of the bicycles given for service::
mysql> select distinct(bicycle.modelno), bicyclemodel.style from bicycle, service, bicyclemodel where
bicycle.bicycleid=service.bicycleid and bicycle.modelno=bicyclemodel.modelno;

+---------+---------------+

| modelno | style |

+---------+---------------+

| BM0001 | MOUNTAIN BIKE |

| BM0004 | SPORTS BIKE |

| BM0003 | Road Bike |

+---------+---------------+

3 rows in set (0.00 sec)

QUESTION 4.

Create the following tables, enter at least 5 records in each table and answer
the queries given below.
EMPLOYEE ( Person_Name, Street, City ) 11
WORKS ( Person_Name, Company_Name, Salary )
COMPANY ( Company_Name, City )
MANAGES ( Person_Name, Manager_Name )

mysql> create database Company_Work;

Query OK, 1 row affected (4.60 sec)

mysql> use Company_Work;


Database changed

mysql> create table Employee(Employee_ID varchar(10),Person_Name varchar(255),Street


varchar(255),City varchar(255),primary key(Employee_ID));

Query OK, 0 rows affected (0.52 sec)

mysql> create table Company(Company_ID varchar (30),Company_Name varchar(255),City


varchar(255),primary key(Company_ID));

Query OK, 0 rows affected (0.47 sec)

mysql> create table Works(Employee_ID varchar(10),Company_ID varchar(30),Salary_$ int,constraint


FK1 foreign key(Employee_ID) references Employee(Employee_ID),constraint FK2 foreign
key(Company_ID) references Company(Company_ID));

Query OK, 0 rows affected (0.55 sec)

mysql> create table Manages(Employee_ID varchar(10),Manager_ID varchar(10),constraint FK3 foreign


key(Employee_ID) references Employee(Employee_ID),constraint FK4 foreign key(Manager_ID)
references Employee(Employee_ID));

Query OK, 0 rows affected (1.37 sec)

mysql> insert into Employee values('121','Ram','Rajajipuram','Lucknow'),('122','Neha','National


Park','Delhi'),('123','Soumya','Alambagh','Lucknow'),('124','Hemant','Bandra','Mumbai'),
('125','Abha','Vaishali Nagar','Jaipur');

Query OK, 5 rows affected (0.27 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> insert into Company values('10183','Samba Bank','Lucknow'),('10184','NCB Bank','Delhi'),


('10185','BOB Bank','Lucknow'),('10186','HDFC Bank','Mumbai'),('10187','Bhim Bank','Banglore');

Query OK, 5 rows affected (0.13 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> insert into Works values('123','10183',20000),('122','10186',15000),('121','10187',9000),


('124','10184',17000),('125','10185',8000),('123','10184',15000),('124','10183',9000),
('125','10184',7000), ('121','10185','6000'), ('122','10187','11000');

Query OK, 10 rows affected (0.12 sec)

Records: 10 Duplicates: 0 Warnings: 0


mysql> insert into Manages values('124','123'),('125','123'),('121','125'),('122','121'),('125','124');

Query OK, 5 rows affected (0.10 sec)

Records: 5 Duplicates: 0 Warnings: 0

B))
Alter table employee, add a column “email” of type varchar(20).
mysql> alter table Employee add(Email_ID varchar(255) default 'nalimsingh8@gmail.com');

Query OK, 0 rows affected (0.78 sec)

Records: 0 Duplicates: 0 Warnings: 0

C))
Find the name of all managers who work for both Samba Bank and
NCB Bank.

mysql> select distinct m.Manager_ID, e.Person_Name from Employee as e,Manages as m,Works as


w1,Works as w2 where w1.Company_ID='10183' and w2.Company_ID='10184' and
e.Employee_ID=w1.Employee_ID and e.Employee_ID=w2.Employee_ID and
e.Employee_ID=m.Manager_ID;

+------------+-------------+

| Manager_ID | Person_Name |

+------------+-------------+

| 123 | Soumya |

| 124 | Hemant |

+------------+-------------+

2 rows in set (0.07 sec)

D))
Find the names, street address and cities of residence and salary of
employees who work for “Samba Bank” and earn more than $10,000.
mysql> select
E.Employee_ID,E.Person_Name,E.Street,E.city,W.Salary_$,C.Company_Name,C.Company_ID from
Employee as E,Works as W,Company as C where C.Company_Name='Samba Bank' and
W.Salary_$>10000 and E.Employee_ID=W.Employee_ID and C.Company_ID=W.Company_ID;

+-------------+-------------+----------+---------+----------+--------------+------------+

| Employee_ID | Person_Name | Street | city | Salary_$ | Company_Name | Company_ID |

+-------------+-------------+----------+---------+----------+--------------+------------+

| 123 | Soumya | Alambagh | Lucknow | 20000 | Samba Bank | 10183 |

+-------------+-------------+----------+---------+----------+--------------+------------+

1 row in set (0.17 sec)

E))
Find the name of all employees who live in the same city as the
company for which they work.

mysql> select E.Employee_ID,E.Person_Name,E.City from Employee as E,Company as C,Works as W


where E.City=C.City and E.Employee_Id=W.Employee_ID and C.Company_ID=W.Company_ID;

+-------------+-------------+---------+

| Employee_ID | Person_Name | City |

+-------------+-------------+---------+

| 121 | Ram | Lucknow |

| 123 | Soumya | Lucknow |

+-------------+-------------+---------+

2 rows in set (0.14 sec)

F))
Find the highest salary, lowest salary and average salary paid by each
company.
mysql> select c.Company_Name,c.Company_ID,max(w.Salary_$) as Highest_Salary,min(w.Salary_$) as
Lowest_Salary,avg(w.Salary_$) as Average_Salary from Company as c,Works as w where
c.Company_ID=w.Company_ID group by c.Company_ID;

+--------------+------------+----------------+---------------+----------------+

| Company_Name | Company_ID | Highest_Salary | Lowest_Salary | Average_Salary |

+--------------+------------+----------------+---------------+----------------+

| Samba Bank | 10183 | 20000 | 9000 | 14500.0000 |

| NCB Bank | 10184 | 17000 | 7000 | 13000.0000 |

| BOB Bank | 10185 | 8000 | 6000 | 7000.0000 |

| HDFC Bank | 10186 | 15000 | 15000 | 15000.0000 |

| Bhim Bank | 10187 | 11000 | 9000 | 10000.0000 |

+--------------+------------+----------------+---------------+----------------+

5 rows in set (0.08 sec)

G))
Find the sum of salary and number of employees in each company.

mysql> select C.Company_Name, C.Company_ID, sum(W.Salary_$) as Total_Salary,


count(w.Employee_Id) as Total_Employees from Company as C, Works as W, Employee as E where
E.Employee_ID=W.Employee_ID and C.Company_ID=W.Company_ID group by C.Company_ID;

+--------------+------------+--------------+-----------------+

| Company_Name | Company_ID | Total_Salary | Total_Employees |

+--------------+------------+--------------+-----------------+

| Samba Bank | 10183 | 29000 | 2|

| NCB Bank | 10184 | 39000 | 3|


| BOB Bank | 10185 | 14000 | 2|

| HDFC Bank | 10186 | 15000 | 1|

| Bhim Bank | 10187 | 20000 | 2|

+--------------+------------+--------------+-----------------+

5 rows in set (0.00 sec)

h))
Find the name of the company that pays the highest salary.

mysql> select C.company_name, max(W.salary_$) Highest_Salary from Company as C, Works as W


where C.Company_ID=W.Company_ID;

+--------------+----------------+

| company_name | Highest_Salary |

+--------------+----------------+

| Samba Bank | 20000 |

+--------------+----------------+

1 row in set (0.14 sec)

QUESTION 5.

Create the following tables, enter at least 5 records in each


table and answer the queries given below.
Suppliers (SNo, Sname, Status, SCity)
Parts (PNo, Pname, Colour, Weight, City)
Project (JNo, Jname, Jcity)
Shipment (Sno, Pno, Jno, Qunatity)

mysql> use GEII;

Database changed

mysql> create table suppliers (SNo char(2) primary key, SName varchar (50) not null, Status int not null,

-> SCity varchar(50) not null);

Query OK, 0 rows affected (1.14 sec)

mysql> create table Parts (PNo char(2) primary key, PName varchar (100) not null, Colour varchar (50)
not

-> null, Weight_kg int not null, PCity varchar(50) not null);

Query OK, 0 rows affected (0.47 sec)

mysql> create table Project (JNo char(2) primary key, JName varchar (100) not null, JCity varchar (50)
not

-> null);

Query OK, 0 rows affected (0.82 sec)

mysql> create table Shipment(SNo char(2) not null, PNo char(2) not null, JNo char(2) not null, Quantity
int not null, constraint Ft1 foreign key(SNo) references Suppliers(SNo) on delete cascade on update
cascade, constraint Ft2 foreign key(PNo) references Parts(PNo) on delete cascade on update cascade,
constraint FJ foreign key(JNo) references Project(JNo) on delete cascade on update cascade, primary
key(Sno, PNo, JNo));

Query OK, 0 rows affected (1.14 sec)

mysql> select * from parts;

+-----+-------------+--------+--------+----------+

| Pno | PName | Colour | weight | city |


+-----+-------------+--------+--------+----------+

| P1 | Clutch | Red | 19 | Paris |

| P2 | Radiator | Red | 23 | London |

| P3 | Switch | Pink | 12 | New York |

| P4 | Accelerator | Red | 22 | Raipur |

| P5 | Spindle | Yellow | 34 | Berlin |

+-----+-------------+--------+--------+----------+

5 rows in set (0.00 sec)

mysql> INSERT INTO PARTS VALUES("P1","CLUTCH","RED",19,"PARIS"),


("P2","RADIATOR","RED",23,"Paris"),("P3","SWITCH","RED",12,"NEW YORK"),
("P4","ACCELERATOR","RED",22,"LONDON"),("P5","SPINDLE","YELLOW",34,"BERLIN"),
("P6","HUTCH","RED",50,"PARIS"),("P7","MATCHES","RED",30,"PARIS"),("P8","SORT","RED",40,"PARIS"),
("P9","MOJI","ORANGE",50,"LONDON");

Query OK, 9 rows affected (0.09 sec)

Records: 9 Duplicates: 0 Warnings: 0

select distinct(S.Sname), P.Pname, Pj.Jname from Parts as P, Project as Pj, Suppliers as S, Shipment as Sh
where Sh.Sno=S.Sno and P.Pno=Sh.Pno and Sh.Jno=Sh.Jno;

mysql> select * from suppliers;

+-----+--------+--------+----------+

| SNo | sname | status | scity |

+-----+--------+--------+----------+

| 1 | Mori | 36 | Paris |

| 2 | Shori | 20 | London |

| 3 | Chhori | 22 | Paris |

| 4 | Vori | 30 | New York |

| 5 | Nori | 12 | Berlin |
+-----+--------+--------+----------+

5 rows in set (0.00 sec)

insert into Suppliers values("S1","MORI",36,"PARIS"),("S2","SHORI",20,"LONDON"),


("S3","CHHORI",22,"PARIS"),("S4","VORI",22,"NEW YORK"),("S5","NORI",12,"BERLIN"),
("S6","LORI",25,"LONDON"),("S7","TORI",19,"RAIPUR"),("S8","SORI",19,"PARIS"),("S9","HLORI",21,"NEW
YORK");

mysql> select * from project;

+-----+----------+----------+

| JNo | Jname | Jcity |

+-----+----------+----------+

| J1 | Machines | London |

| J2 | Sano | Paris |

| J3 | Mano | New York |

| J4 | Shona | Berlin |

| J5 | Rona | Paris |

+-----+----------+----------+

5 rows in set (0.00 sec)

INSERT INTO PROJECT VALUES("J1","MACHINES","LONDON"),("J2","SANO","PARIS"),


("J3","MANO","NEW YORK"),("J4","SHONA","BERLIN"),("J5","RONA","PARIS"),
("J6","MACHE","LONDON"),("J7","SOCHE","BERLIN"),("J8","OCHE","RAIPUR");

mysql> select * from shipment;

+------+------+------+----------+

| Sno | Pno | Jno | Quantity |

+------+------+------+----------+
| S1 | P1 | J2 | 500 |

| S2 | P3 | J3 | 400 |

| S2 | P4 | J1 | 750 |

| S5 | P2 | J5 | 200 |

| S1 | P2 | J4 | 360 |

+------+------+------+----------+

5 rows in set (0.07 sec)

INSERT INTO SHIPMENT VALUES("S1","P1","J2",500),("S2","P3","J3",400),("S2","P4","J1",750),


("S5","P2","J5",200),("S1","P2","J4",360),("S4","P3","J1",100),("S5","P2","J6",200),("S6","P4","J2",1500),
("S3","P6","J8",2500),("S3","P4","J8",900),("S5","P2","J3",100),("S6","P7","J7",300),("S6","P6","J8",50),
("S7","P7","J7",200),("S8","P3","J2",57),("S4","P8","J2",500),("S9","P2","J2",200),("S1","P1","J1",100),
("S6","P5","J4",300),("S5","P4","J3",200),("S4","P3","J2",100),("S7","P6","J5",400),("S5","P1","J2",100);

b))
Get supplier numbers for suppliers in Paris with status>20.

mysql> select Sno from Suppliers where SCity="Paris" and status>20;

+-----+

| Sno |

+-----+

| S1 |

| S3 |

+-----+

2 rows in set (0.07 sec)

c))
Get suppliers details for suppliers who supply part P2. Display the
supplier list in increasing order of supplier numbers.

mysql> select distinct S.* from Suppliers as S, Parts as P, Shipment as Sh where Sh.PNo="P2" and
S.Sno=Sh.Sno and P.Pno=Sh.Pno order by S.Sno;

+-----+-------+--------+----------+

| SNo | SName | Status | SCity |

+-----+-------+--------+----------+

| S1 | MORI | 36 | PARIS |

| S5 | NORI | 12 | BERLIN |

| S9 | HLORI | 21 | NEW YORK |

+-----+-------+--------+----------+

3 rows in set (0.00 sec)

d))
Get suppliers names for suppliers who do not supply part P2

mysql> Select * from suppliers where SNo not in(Select distinct S.Sno from suppliers s, shipment h
where

-> h.PNo='P2' and s.SNo=h.SNo);

+-----+--------+--------+----------+

| SNo | SName | Status | SCity |

+-----+--------+--------+----------+

| S2 | SHORI | 20 | LONDON |

| S3 | CHHORI | 22 | PARIS |

| S4 | VORI | 22 | NEW YORK |

| S6 | LORI | 25 | LONDON |
| S7 | TORI | 19 | RAIPUR |

| S8 | SORI | 19 | PARIS |

+-----+--------+--------+----------+

6 rows in set (0.09 sec)

e))
For each shipment get full shipment details, including total shipment
weights.

mysql> select h.*, p.Weight_kg, (p.Weight_kg*h.Quantity) Total_Weight from shipment h, parts p where
p.PNo=h.PNo;

+-----+-----+-----+----------+-----------+--------------+

| SNo | PNo | JNo | Quantity | Weight_kg | Total_Weight |

+-----+-----+-----+----------+-----------+--------------+

| S1 | P1 | J1 | 100 | 19 | 1900 |

| S1 | P1 | J2 | 500 | 19 | 9500 |

| S5 | P1 | J2 | 100 | 19 | 1900 |

| S1 | P2 | J4 | 360 | 23 | 8280 |

| S5 | P2 | J3 | 100 | 23 | 2300 |

| S5 | P2 | J5 | 200 | 23 | 4600 |

| S5 | P2 | J6 | 200 | 23 | 4600 |

| S9 | P2 | J2 | 200 | 23 | 4600 |

| S2 | P3 | J3 | 400 | 12 | 4800 |

| S4 | P3 | J1 | 100 | 12 | 1200 |

| S4 | P3 | J2 | 100 | 12 | 1200 |

| S8 | P3 | J2 | 57 | 12 | 684 |

| S2 | P4 | J1 | 750 | 22 | 16500 |
| S3 | P4 | J8 | 900 | 22 | 19800 |

| S5 | P4 | J3 | 200 | 22 | 4400 |

| S6 | P4 | J2 | 1500 | 22 | 33000 |

| S6 | P5 | J4 | 300 | 34 | 10200 |

| S3 | P6 | J8 | 2500 | 50 | 125000 |

| S6 | P6 | J8 | 50 | 50 | 2500 |

| S7 | P6 | J5 | 400 | 50 | 20000 |

| S6 | P7 | J7 | 300 | 30 | 9000 |

| S7 | P7 | J7 | 200 | 30 | 6000 |

| S4 | P8 | J2 | 500 | 40 | 20000 |

+-----+-----+-----+----------+-----------+--------------+

23 rows in set (0.06 sec)

f))
Get all the shipments where the quantity is in the range 300 to 750
inclusive

mysql> select Sh.* from shipment as Sh where Sh.quantity>=300 and Sh.quantity<=750;

+-----+-----+-----+----------+

| SNo | PNo | JNo | Quantity |

+-----+-----+-----+----------+

| S1 | P1 | J2 | 500 |

| S1 | P2 | J4 | 360 |

| S2 | P3 | J3 | 400 |
| S2 | P4 | J1 | 750 |

| S4 | P8 | J2 | 500 |

| S6 | P5 | J4 | 300 |

| S6 | P7 | J7 | 300 |

| S7 | P6 | J5 | 400 |

+-----+-----+-----+----------+

8 rows in set (0.00 sec)

g))
Get part nos. for parts that either weigh more than 16 pounds or are
supplied by
suppliers S2, or both.

mysql> (select distinct(P.Pno) from parts as P, shipment as Sh, suppliers as S where P.weight_kg>16 and
P.Pno=Sh.PNo and S.Sno=Sh.Sno) union (select distinct(P.Pno) from parts as P, shipment as Sh, suppliers
as S where S.Sno="S2" and P.Pno=Sh.Pno and S.Sno=Sh.Sno);

+-----+

| Pno |

+-----+

| P1 |

| P2 |

| P4 |

| P5 |

| P6 |
| P7 |

| P8 |

| P3 |

+-----+

8 rows in set (0.07 sec)

h)
Get the names of cities that store more than five red parts.

mysql> select PCity, count(PNo) Number from parts group by PCity having count(PNo)>5;

+-------+--------+

| PCity | Number |

+-------+--------+

| PARIS | 6|

+-------+--------+

1 row in set (0.00 sec)

i)
Get full details of parts supplied by a supplier in London.

mysql> select P.* from Parts as P, Suppliers as S, Shipment as Sh where S.scity="London" and
P.Pno=Sh.Pno and S.Sno=Sh.Sno;

+-----+-------------+--------+-----------+--------+

| PNo | PName | Colour | Weight_kg | PCity |

+-----+-------------+--------+-----------+--------+
| P3 | SWITCH | RED | 12 | PARIS |

| P4 | ACCELERATOR | RED | 22 | LONDON |

| P4 | ACCELERATOR | RED | 22 | LONDON |

| P5 | SPINDLE | YELLOW | 34 | BERLIN |

| P6 | HUTCH | RED | 50 | PARIS |

| P7 | MATCHES | RED | 30 | PARIS |

+-----+-------------+--------+-----------+--------+

6 rows in set (0.00 sec)

j)
Get part numbers for part supplied by a supplier in London to a
project in London.

mysql> select P.Pno from Parts as P, Suppliers as S, project as pj, Shipment as Sh where S.Scity="London"
and Jcity="London" and P.Pno=Sh.Pno and S.Sno=Sh.Sno and pj.Jno=Sh.Jno;

+-----+

| Pno |

+-----+

| P4 |

+-----+

1 row in set (0.06 sec)

k)
Get the total number of project supplied by a supplier (say, S1)
mysql> select SNo, count(distinct JNo)Number_of_projects from shipment where Sno='S1';

+------+--------------------+

| SNo | Number_of_projects |

+------+--------------------+

| S1 | 3|

+------+--------------------+

1 row in set (0.05 sec)

l)
Get the total quantity of a part (say, P1) supplied by a supplier (say,
S1)

mysql> select SNo, PNo, sum(Quantity)Total_Quantity from shipment where SNo='S6' and PNo='P7';

+------+------+----------------+

| SNo | PNo | Total_Quantity |

+------+------+----------------+

| S6 | P7 | 300 |

+------+------+----------------+

1 row in set (0.05 sec)

You might also like