Database Management System Lab COE-317: Submitted by:-366/CO/14 SOMYA Sangal 374/CO/14 TWISHI Tyagi Coe-Iii
Database Management System Lab COE-317: Submitted by:-366/CO/14 SOMYA Sangal 374/CO/14 TWISHI Tyagi Coe-Iii
Database Management System Lab COE-317: Submitted by:-366/CO/14 SOMYA Sangal 374/CO/14 TWISHI Tyagi Coe-Iii
SYSTEM LAB
COE-317
Submitted by:-
366/CO/14 SOMYA
SANGAL
374/CO/14 TWISHI
TYAGI
COE-III
Query 1: Consider the following relational schema:
create table Sailors (sid int primary key, sname varchar(20) not null, rating int, date_of_birth DATE
not null);
insert into Sailors values ( ('301', 'Hulk', '300', '1989-03-20'), ('302', 'Horatio', '200', '1996-08-12'),
('303', 'Nobita', '500', '1993-11-08'));
create table Boats (bid int primary key, bname varchar(20) not null, colour varchar(20));
insert into Boats values ((101, EG, Green), (102, TH, Red));
create table Reserves (sid int, bid int, day_of_reserve date, PRIMARY KEY (sid, bid,
day_of_reserve), FOREIGN KEY (sid) REFERENCES Sailors(sid), foreign key (bid) references
Boats(bid));
insert into Reserves values ((301, 101, 2002-12-03), (305, 101, 2012-10-19));
QUERIES IN SQL
3) select distinct R1.sid from Reserves as R1, Reserves as R2, Boats as B1, Boats as B2
where R1.sid=R2.sid and R1.bid=B1.bid and R2.bid=B2.bid and B1.colour = 'Red' and
B2.colour='Green';
5) select sname from Sailors NATURAL JOIN Reserves where Reserves.bid = 103;
6) select S1.sid from Sailors as S1,Sailors as S2
where S1.rating>S2.rating and S2.sname='Horatio';
9) select S.rating, min(age) from Sailors as S natural join Ages group by S.rating having
count(*)>1
15)
CREATING AND INSERTING VALUES IN THE DATABASE
create table CUSTOMER_1 (CUST_NUM int primary key, CUST_LNAME varchar(20) not
null, CUST_FNAME varchar(20) not null, CUST_BALANCE int);
insert into customer_1 values ( ('1000', 'Raina', 'Suresh', '2000'), ('1001', 'Kohli', 'Virat',
'1000'), ('1002', 'Nehwal', 'Saina', '5000'), ('1003', 'Kom', 'Mary', '3000'), ('1004', 'Malik',
'Sakshi', '4000'), ('1005', 'Bindra', 'Abhinav', '1900'), ('1006', 'Singh', 'Yuvraj', '4500'), ('1007',
'Mirza', 'Sania', '2900'), ('1008', 'Ganguly', 'Saurav', '6300'));
create table customer_2 (CUST_NUM int primary key, CUST_LNAME varchar(20) not null,
CUST_FNAME varchar(20) not null, CUST_BALANCE int
insert into values (('2000', 'Thompson', 'Ken', '1200') ), ('2001', 'Lovelace', 'Ada', '1600'),
('2003', 'Carmack', 'John', '2500'), ('2004', 'Walt', 'Emma', '5600'), ('1001', 'Kohli', 'Virat',
'1000'), ('1008', 'Ganguly', 'Saurav', '6300'));
insert into PRODUCT values (('13-Q2/P2', '250', 'Earplugs'), ('23109-H', '700', 'Headset'),
('A-10899', '500', 'Charger'), ('QU-10090', '819', 'Memory Card'), ('120-QR', '600', 'Selfie
Stick'), ('QWERT-12', '200', 'Screenguard')) ;
create table VENDOR(vendor_id int primary key, location varchar(20), vendor_name
varchar(20) not null, address varchar(50));
insert into VENDOR values (('145', 'Memphis', 'Charles Baron', '598 Piccard Drive'), ('143',
'Santa Clara', 'Baum', '5089 Calero Avenue'), ('146', 'Kalamazoo', 'Gunther Spilberg', '1095
Highway 99 South'), ('148', 'LA Rochelle', 'YA Chavanon', '103 Rue Degmont'), ('149',
'Vienna', 'George Shaw', 'Salguero 2345'), ('151', 'Minnesota', 'Hugh Jackman', '12, Central
Parkview'));
create table invoice_1(inv_num int primary key, prod_num varchar(20), unit_sold int not
null, cust_num int, inv_date date not null, inv_amount int not null, vendor_id int,
foreign key (prod_num) references product(PROD_NUM), foreign key (vendor_id)
references vendor(vendor_id));
insert into invoice_1 values (('11270', '120-QR', '1', '1000', '2016/10/18', '0', '143'), ('11271',
'13-Q2/P2', '1', '1000', '2016/10/24', '0', '145'), ('11273', '23109-H', '2', '1001', '2016/11/01', '0',
'143'), ('11276', 'A-10899', '3', '2000', '2016/11/11', '0', '146'), ('11278', '120-QR', '1', '2001',
'2016/11/08', '0', '146'), ('11280', '13-Q2/P2', '2', '1008', '2016/12/05', '0', '148'), ('11282', '13-
Q2/P2', '3', '1004', '2016/12/31', '0', '151'), ('11285', 'QU-10090', '2', '2003', '2017/01/05', '0',
'143'), ('11287', '120-QR', '2', '1008', '2017/01/25', '0', '143'));
update invoice_1
invoice_1 inner join product on invoice_1.prod_num = product.PROD_NUM
set inv_amount = unit_sold*price
QUERIES IN SQL
update customer_1
set cust_age = (datediff(current_date, cust_dob)/365.25)
7) delimiter $$
create trigger update_custbalance
after insert on invoice_1
for each row
begin
update all_customers
set cust_balance = cust_balance + new.inv_amount
where all_customers.cust_num = new.cust_num;
end $$
delimiter ;
8) select * from product where price >= (select avg(price) from product)
12) select *from product as P1 where P1.price>any(select P2.price from product as P2,
vendor, invoice_1where P2.PROD_NUM = invoice_1.prod_num and
vendor.vendor_id=invoice_1.vendor_id and vendor.location = 'Florida')
13) select cust_num from invoice_1 where prod_num = '23109-H' and cust_num in (select
cust_num from invoice_1 where prod_num ='13-Q2/P2');
14) select prod_num, (select avg(price) from product) as average, price-(select avg(price)
from product) as diff from product;
QUERIES IN RELATIONAL ALGEBRA