DATABASE SYSTEMS Assignment-3
SQL
Submitted To: Prof. Shoaib Farooq Submitted By: Farhan Saleem Reg. No :
L1F07BSCS2007
University of Central Punjab
Question # 1) 1. Select title from product where price>20; 2. Select count(catagory) from product where catagory=’COLD DRINK’; 3. Select title from product where catagory='BISCUITS' and price>=8; 4. select title,price*qty as TotalPrice from product; 5. select title from product where price=(select min(price) from product); 6. select sum(price) from product; 7. select sum(price) from product group by (catagory); 8. select avg(price) from product group by (catagory),(psize); 9. Select sum(qty) from product group by (catagory) HAVING sum(qty)>1000; 10. SELECT PSIZE,SUM(QTY) FROM PRODUCT GROUP BY PSIZE;
Question # 2) 1. (Maker because cars have more than one maker, Color because cars have more than one color,HrPower because cars have many horsePower speeds,Price because cars have different prices according to their brands or enginePower wise etc.) 2. (Title because different Books have different Titles, Course because different Books are required for course,Publisher
because Books are published by many Publishers,Price because Books have different prices) 3. (JOB is best column to choose it for group by clause beause it differentiate the employees job wise which is more prominent as compared to others).
Question # 3) 1. CONSTRAINTS PRIMARY KEYS alter table Person add constraint person_name_pk primary key(name); alter table Dealer add constraint dealer_dealerid_pk primary key(dealerid); alter table Car add constraint Car_carid_pk primary key(carid); alter table Location add constraint Location_locid_pk primary key(locid); FOREIGN KEYS alter table Person add constraint person_carid_fk foreign key(carid) references car(carid); alter table Person add constraint person_dealerid_fk foreign key(dealerid) references Dealer(dealerid);
alter table Dealer add constraint Dealer_locid_fk foreign key(locid) references Location(locid); alter table Car add constraint Car_locid_fk foreign key(locid) references Location(locid);
2. VIEW ALL CONSTRAINTS SELECT constraint_name, column_name FROM user_cons_columns WHERE table_name = 'person'; SELECT constraint_name, column_name FROM user_cons_columns WHERE table_name = 'dealer'; SELECT constraint_name, column_name FROM user_cons_columns WHERE table_name = 'car'; SELECT constraint_name, column_name FROM user_cons_columns WHERE table_name = 'location'; 3. (5) Dummy Entries Person (1) insert into person values('usman',1,2,9600000,'17JAN-2006'); (2) insert into person values('ali',5,1,4550000,'14-AUG1947'); (3) insert into person values('arsal',3,3,970000,'28-SEP2007'); (4) insert into person values('arshad',4,1,1500000,'09FEB-2009');
(5)
insert into person values('umer',2,4,2080000,'29JAN-2010'); Dealer (1) insert into dealer values(1,'hameed',1,50000); (2) insert into dealer values(2,'gul',2,60000); (3) insert into dealer values(3,'ahmad',3,70000); (4) insert into dealer values(4,'sana',4,80000); (5) insert into dealer values(5,'farha',5,80000); Car (1) insert into car values(1,'bmw','x700',1,'9000000'); (2) insert into car values(2,'diablo','sv2',4,'2000000'); (3) insert into car values(3,'suzuki','cultus',5,'900000'); (4) insert into car values(4,'mercedes','benz',3,'1450000'); (5) insert into car values(5,'honda','accord',2,'4500000'); Location (1) insert into location values(1,'karachi clifton'); (2) insert into location values(2,'Lahore cantt'); (3) insert into location values(3,'Lahore Defence'); (4) insert into location values(4,'Islamabad'); (5) insert into location values(5,'multan'); 4. select company,locationname from car,location where car.locid=location.locid and company='bmw' and locationname='karachi clifton'; 5. select dealername from dealer,car,person where dealer.dealerid=person.dealerid and car.carid=person.carid and company=(select company from car where company='diablo' and model='sv2'); 6. select name,locationname from person,location,car where person.carid=car.carid and car.locid=location.locid and locationname='Lahore cantt'; 7. select dealername,buyingdate from dealer,person where person.dealerid=dealer.dealerid and buyingdate='17-JAN2006';
8. select company,model from person,car where person.carid=car.carid and buyingdate='17-JAN-2006'; 9. select name,dealername,buyingdate,locationname from person,dealer,location where person.dealerid=dealer.dealerid and dealer.locid=location.locid and dealername='hameed' and buyingdate='14-AUG-1947' and locationname='karachi clifton'; 10. select count(company) from car,person where person.carid=car.carid and company='suzuki' and name='arshad'; 11. select name,company,model,priceandtax as totalprice,dealername,locationname from person,dealer,car,location where person.carid=car.carid and car.locid=location.locid and person.dealerid=dealer.dealerid and name='arshad' and company=(select company from car where company='mercedes' and model='benz') and priceandtax =1500000 and dealername='hameed' and locationname='Lahore Defence';