Faculty of Engineering and Architecture Computer Engineering Department DATABASE MANAGEMENT SYSTEMS POSTLAB #3 SOLUTION
CREATING TABLES DVD Company Table create table dvd_company ( name varchar(15) not null unique, constraint pk_dvd primary key (code)); DVD Compnay Branch Table create table dvd_comp_branch ( code int, branch_id number(2), street varchar(10), state varchar(10), city varchar(10), phonenumber varchar(10), constraint pk_branch primary key (code,branch_id), constraint fk foreign key (code) references dvd_company(code) on delete cascade ); Customer Table create table customer ( customer_id number(4), name varchar(20), address varchar(10), registerdate date not null, branch_id number(2) not null, constraint pk_customer primary key (customer_id), constraint fk_comp foreign key (code,branch_id) references dvd_comp_branch(code,branch_id) on delete cascade ); DVD Table create table DVD ( catalog_number number(3), title varchar(20), category varchar(10), constraint pk_movie primary key (catalog_number)); Has Table create table HAS( branch_id number(2) not null, catalog_number number(3) not null, dvd_number int not null, status char(2), primary key (code,branch_id,catalog_number,dvd_number), foreign key (code,branch_id) references dvd_comp_branch(code,branch_id) on delete cascade, foreign key (catalog_number) references dvd(catalog_number) on delete cascade); create table RENT(
rent_id int not null, customerid number(4) not null, catalog_number number(3) not null, dvd_number int not null, rentdate date not null, primary key (rent_id), foreign key (customerid) references customer(customer_id ) on delete cascade, foreign key (catalog_number) references dvd(catalog_number) on delete cascade); ADDING RECORDS TO TABLES Adding DVD companies insert into DVD_COMPANY values(1,'gediz DVD'); insert into DVD_COMPANY values(2,'menemen DVD'); Adding DVD company branches values(1,10,'john','virginia','fairfax','9333333333'); values(1,11,'marry','virginia','tonn','9233333333'); values(2,10,'kazim','dirik','izmir','921111111'); values(2,11,'sevgi','uskudar','istanbul','911111111'); Adding customers insert into CUSTOMER values(1000,'sevgi','johnstreet',to_date('15/jan/2015'),1,10); insert into CUSTOMER values(1000,'sevgi','johnstreet',to_date('15/jan/2015'),1,10); insert into CUSTOMER values(1001,'kenan','johnstreet','13/jan/2015',1,10); insert into CUSTOMER values(1002,'sevda','marystreet',('1/jan/2014'),1,11); insert into CUSTOMER values(1003,'esra','red Street','1/Jan/2013',1,11); insert into CUSTOMER values(1004,'canan','kazmstreet','15/jan/2015',2,10); insert into CUSTOMER values(1005,'ufuk','tonstreet','13/feb/2015',2,10); insert into CUSTOMER values(1006,'yildiz','sevstreet','11/feb/2014',2,11); insert into CUSTOMER values(1007,'utku','guvstreet','13/feb/2014',2,11); Adding DVDs insert into DVD values(100,'babam ve Oglum','Drama'); insert into DVD values(101,'titanic','romantic'); insert into DVD values(102,'crazy','comedy'); Adding records to HAS tables insert into HAS values(1,10,100,1,'a'); insert into HAS values(1,10,101,1,'a'); insert into HAS values(1,10,101,2,'na'); insert into HAS values(1,10,101,3,'a'); insert into HAS values(1,11,102,1,'a'); insert into HAS values(2,10,101,1,'na'); insert into HAS values(2,10,101,2,'a');
insert into HAS values(2,10,102,1,'a'); insert into HAS values(2,11,100,1,'a'); insert into HAS values(2,11,101,1,'a'); insert into HAS values(2,11,102,1,'na'); Adding records to Rent table insert into RENT values(1,1001,101,2,'14/jan/2015'); insert into RENT values(2,1004,101,1,'16/jan/2015'); insert into RENT values(3,1007,102,1,'14/jan/2015'); QUERIES 1) Display branch id, street and phone number of all branches that locate in city whose name begins with I. select branch_id, street,phonenumber from DVD_COMP_BRANCH where city like 'I%'; 2) Find all customers who have registered to any branch after 12/JAN/2015. List the result as customer name and register date. select name,registerdate from CUSTOMER where REGISTERDATE>='13/JAN/2015'; 3) Find all Dvds are belonged to the company whose code is 2 and list the results with company name, dvd title, status and category order by dvd title. select dc.name COMPANY_NAME, d.title FILM_TITLE, h.status,d.category from DVD_COMPANY dc, DVD d, HAS h where dc.code=2 and dc.code = h.code and d.catalog_number =h.catalog_number order by d.title; 4) Find all customers that registered to company=1 and branch=11 and list the result with customer name and register date. select name,registerdate from customer,dvd_comp_branch where customer.code=1 AND customer.branch_id=11 AND customer.code=dvd_comp_branch.code AND customer.branch_id= DVD_COMP_BRANCH.BRANCH_ID; 5) Find all DVDs that are not rented in branch that locates in Fairfax and display result with dvd number, dvd title and dvd category order by dvd number select HAS.DVD_NUMBER, DVD.TITLE, DVD.CATEGORY from DVD,DVD_COMP_BRANCH,HAS where DVD_COMP_BRANCH.CITY='Fairfax' AND DVD_COMP_BRANCH.CODE = has.code AND DVD_COMP_BRANCH.BRANCH_ID = has.branch_id and DVD.CATALOG_NUMBER= has.catalog_number AND HAS.STATUS='A' order by HAS.DVD_NUMBER; 6) Find all customers who rented Titanic. Display customer name, dvd title and rent date. select customer.name,dvd.title,rent.rentdate from DVD,rent,customer where dvd.title='titanic' and dvd.catalog_number=rent.catalog_number and rent.customerid = customer.customer_id;
7) Retrieve ids and names of all customers whose ids are between 1000 and 1005. select customer.customer_id,customer.name from customer where CUSTOMER_ID between 1000 and 1005; 8) Ufuk wants to rent Crazy film in branch where he has registered. Crazy film s first copy is available for renting. So write proper SQL statement that add record to rent table and then update has table. insert into rent values(4,1005,102,1,sysdate); update has set status='na' where BRANCH_ID=10 and CODE=2 and DVD_NUMBER=1; 9) Kenan gives the rented DVD back. So delete Kenan s rental information from rent table. Then update has table. delete from rent where CUSTOMERID=1001; update has set status='a' where CATALOG_NUMBER=101 and DVD_NUMBER=2 and CODE=1 and BRANCH_ID=10; 10) Delete branch whose code is 2 and branch id is 11. delete from DVD_COMP_BRANCH where code=2 and BRANCH_ID=11;