Você está na página 1de 17

DATABASE MANAGEMENT

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:

Sailors (sid, sname, rating, date_of_birth, PRIMARY KEY sid)


Boats (bid, bname, color, PRIMARY KEY bid)
Reserves (sid, bid, date, PRIMARY KEY (sid, bid, date))

1) Find sailors who have reserved at least one boat


2) Find sids of sailors who have reserved a red or green boat
3) Find sids of sailors who have reserved a red and a green boat
4) Find sids of sailors who have not reserved a boat.
5) Names of sailors who have reserved boat #103
6) Find sailors whose rating is greater than that of some sailor called Horatio
7) Find sailors who have reserved all boats
8) Find name and age of oldest sailor(s)
9) Find the age of the youngest sailor for each rating with at least 2 such sailors
10) Find those ratings for which the average age is the minimum over all ratings.
11) Create a trigger to count sailors with voting age
CREATING AND INSERTING VALUES IN THE DATABASE

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

1) select sid from Reserves group by sid having count(*)>=1;

2) select distinct sid from Reserves, Boats


where Reserves.bid = Boats.bid and Boats.colour = 'Red' or Boats.colour = 'Green';

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';

4) select sid from Sailors


where sid NOT IN (select sid from Reserves group by sid having count(*)>=1);

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';

7) select sid from Reserves


group by sid having Count(bid) =(Select count(*) from Boats);

8) create view Ages(sid, age) as


select sid, date_format(from_days(datediff(current_date, date_of_birth)), '%y Years %m
Months %d Days') as age from Sailors;

select sname, age from Sailors, Ages


where Sailors.sid=Ages.sid and age = (select max(age) from Ages)

9) select S.rating, min(age) from Sailors as S natural join Ages group by S.rating having
count(*)>1

10) select rating from Sailors natural join Ages


group by rating
having avg(age) <= all(select avg(age) from Sailors natural join Ages group by rating)
11) delimiter $$
create trigger voting after insert on Sailors
for each row
begin
insert into Voters(sid) select Sailors.sid from Sailors natural join Ages
where age>=18;
select count(distinct sid) from Voters;
END $$
delimiter ;

insert into Sailors values ('310', 'Columbus', '500', '2000-12-08');


QUERIES IN RELATIONAL ALGEBRA
Query 2: Consider the following relational schema.

CUSTOMER_1(CUST_NUM PRIMARY KEY, CUST_LNAME, CUST_FNAME,


CUST_BALANCE)
CUSTOMER_2(CUST_NUM PRIMARY KEY, CUST_LNAME, CUST_FNAME,
CUST_BALANCE)
PRODUCT(PROD_NUM PRIMARY KEY, PROD_NAME, PRICE)
VENDOR(vendor_id, location, vendor_name, address)
INVOICE_1(INV_NUM PRIMARY KEY, PROD_NUM, UNIT_SOLD, CUST_NUM,
INV_DATE, INV_AMOUNT, vendor_id)

Write the following queries in SQL:


1) Write the query that will generate a combined list of customers to include the duplicate list of
records
2) Write a query that will show only the duplicate customer records
3) Write a query that will generate only the records that are unique to customer_2 table
4) Write a query that will show the invoice number, the average invoice amount and the
difference between invoice amount and actual invoice amount
5) Modify the CUSTOMER table to include two new attributes CUST_DOB and CUST_AGE.
Customer 1000 was born on March 15, 1969 and Customer 1001 was born on December 22,
1977.
6) Assuming that the CUSTOMER table contains a CUST_AGE attribute write the query that
will update the values of this attribute
7) Write the trigger to update CUST_BALANCE in the CUSTOMER table when a new invoice
record is entered (Assume that the sale is a credit sale)
8) Find all products with price greater or equal to average product price
9) Find all customers who have purchased any kind of hammer or saw
10) Find all products with total quantity sold greater than average quantity sold.
11) List all products that have product cost that is greater than all individual product costs for
individual products provided by various vendors from Florida
12) Now if we change the problem to list all products that have product cost that is greater than
any individual product costs for individual products provided by various vendors from
Florida
13) Produce a list of all customers who have purchased the products 13-Q2/P2 and 23109-H
14) Produce a listing of product code, product price, average price and the difference between
products price and average product price.

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'));

create table PRODUCT(PROD_NUM varchar(20) primary key, PROD_NAME varchar(20)


not null, PRICE int not null);

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));

alter table invoice_1 add constraint foreign key(cust_num) references


all_customers(CUST_NUM);

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

1) select * from customer_1


union all
select * from customer_2

2) select * from customer_1 where customer_1.CUST_NUM in (select


customer_2.CUST_NUM from customer_2)

3) select * from customer_2 where customer_2.CUST_NUM not in (select


customer_1.CUST_NUM from customer_1)

4) select inv_num, (select avg(inv_amount) from invoice_1) as average,


(select avg(inv_amount) from invoice_1)-inv_amount as diff
from invoice_1;
5) alter table customer_1 add cust_dob date not null;

alter table customer_1 add cust_age int not null;

update customer_1set cust_dob='1969/03/15' WHERE `CUST_NUM`='1000';

update customer_1 set cust_dob='1977/12/22' WHERE `CUST_NUM`='1001';

6) select * from customer_1;

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)

9) select cust_num from invoice_1 inner join product on invoice_1.prod_num =


product.PROD_NUM where product.PROD_NAME = 'hammer' or
product.PROD_NAME = 'saw'

10) select prod_num from invoice_1 as I1 group by prod_num


having sum(unit_sold) > (select avg(unit_sold) from invoice_1 as I2 group by prod_num
having I1.prod_num = I2.prod_num) ;
11) select *from product as P1 where P1.price>all(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')

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

Você também pode gostar