1.member表:
create table member(
id number(10),
last_name varchar2(25) constraint meb_lastname_nn not null,
first_name varchar2(25),
address varchar2(50),
city varchar2(20),
phone varchar2(20),
join_date date constraint meb_join_nn not null,
constraint mem_id_pk primary key(id)
);
create table title (
id number(10),
title varchar2(200) constraint tit_title_nn not null,
description varchar2(1000) constraint tit_des_nn not null,
rating number(2),
category varchar2(10),
release_date date,
price number(10,2),
constraint tit_id_pk primary key(id)
);
3.reservation表:
create table reservation (
res_date date,
tit_id number(10) constraint res_tit_id_fk references title(id),
mem_id number(10) constraint res_mem_id_fk references member(id),
constraint reservation_res_date_id_pk primary key(res_date,tit_id,mem_id)
);
4.title_copy表:
create table title_copy (
id number(10),
statue varchar2(20) constraint title_copy_statue_nn not null,
title_id number(10) constraint tit_cop_tit_id_fk references title(id),
constraint tit_copy_id_tit_id_pk primary key(id,title_id)
);
5.rental表:
create table rental (
book_date date,
act_ret_date date,
exp_ret_date date,
mem_id number(10) constraint ren_mem_id_nn not null constraint ren_mem_id_fk
references member(id),
tit_id number(10),
tit_copy_id number(10),
unique(tit_id,tit_copy_id),
constraint rental_fk foreign key(tit_id,tit_copy_id) references title_copy(title_id,id),
constraint rental_pk primary key(book_date,tit_id,tit_copy_id)
);
6.member表插入:
insert into member(id,last_name,join_date) values(1,'张毅鹏','17-11月-19');
insert into member(id,last_name,join_date) values(2,'张慧乾','17-10月-19');
insert into member(id,last_name,join_date) values(3,'杨业雷','17-9月-19');
insert into member(id,last_name,join_date) values(4,'原紫薇','17-8月-19');
insert into member(id,last_name,join_date) values(5,'刘卓一','17-7月-19');
insert into member(id,last_name,join_date) values(6,'张婧暄','17-11月-19');
insert into member(id,last_name,join_date) values(7,'张北锴','17-3月-19');
insert into member(id,last_name,join_date) values(8,'杨峻霖','17-4月-08');
insert into member(id,last_name,join_date) values(9,'朱伟','17-11月-19');
insert into member(id,last_name,join_date) values(10,'姜毛','17-2月-19');
7.title表插入:
insert into title(id,title,description,price) values(1,'春娇与志明','爱情',20);
insert into title(id,title,description,price) values(2,'羞羞的铁拳','喜剧',20);
insert into title(id,title,description,price) values(3,'钢铁侠','动作',40);
insert into title(id,title,description,price) values(4,'唐伯虎点秋香','喜剧',40);
insert into title(id,title,description,price) values(5,'英雄本色','动作 剧情',40);
insert into title(id,title,description,price) values(6,'赌神','剧情 喜剧',30);
insert into title(id,title,description,price) values(7,'春娇救志明','喜剧',30);
insert into title(id,title,description,price) values(8,'志明与春娇','爱情',30);
insert into title(id,title,description,price) values(9,'夏洛特烦恼','喜剧 剧情',30);
insert into title(id,title,description,price) values(10,'阿甘正传','剧情',50);
8.reservation表插入:
insert into reservation(tit_id,mem_id,res_date) values(1,1,sysdate);
insert into reservation(tit_id,mem_id,res_date) values(2,2,sysdate-10);
insert into reservation(tit_id,mem_id,res_date) values(3,3,sysdate-23);
insert into reservation(tit_id,mem_id,res_date) values(4,2,sysdate-32);
insert into reservation(tit_id,mem_id,res_date) values(3,5,sysdate-4);
insert into reservation(tit_id,mem_id,res_date) values(6,6,sysdate-1);
insert into reservation(tit_id,mem_id,res_date) values(1,7,sysdate-3);
insert into reservation(tit_id,mem_id,res_date) values(8,9,sysdate-45);
insert into reservation(tit_id,mem_id,res_date) values(10,9,sysdate-6);
insert into reservation(tit_id,mem_id,res_date) values(10,10,sysdate-44);
9.title_copy表插入:
insert into title_copy(id,statue,title_id) values(1,'rent',1);
insert into title_copy(id,statue,title_id) values(2,'rent',1);
insert into title_copy(id,statue,title_id) values(3,'free',1);
insert into title_copy(id,statue,title_id) values(1,'rent',2);
insert into title_copy(id,statue,title_id) values(1,'rent',3);
insert into title_copy(id,statue,title_id) values(2,'free',3);
insert into title_copy(id,statue,title_id) values(1,'rent',4);
insert into title_copy(id,statue,title_id) values(1,'rent',5);
insert into title_copy(id,statue,title_id) values(1,'rent',8);
insert into title_copy(id,statue,title_id) values(1,'rent',10);
10.rental表插入:
insert into rental(book_date,act_ret_date,exp_ret_date,tit_id,tit_copy_id,mem_id)
values(sysdate-10,sysdate-5,sysdate-3,1,1,1);
insert into rental(book_date,act_ret_date,exp_ret_date,tit_id,tit_copy_id,mem_id)
values(sysdate-20,sysdate-15,sysdate-10,1,2,2);
insert into rental(book_date,act_ret_date,exp_ret_date,tit_id,tit_copy_id,mem_id)
values(sysdate-10,sysdate-5,sysdate-3,1,3,4);
insert into rental(book_date,act_ret_date,exp_ret_date,tit_id,tit_copy_id,mem_id)
values(sysdate-15,sysdate-10,sysdate-5,2,1,3);
insert into rental(book_date,act_ret_date,exp_ret_date,tit_id,tit_copy_id,mem_id)
values(sysdate-25,sysdate-20,sysdate-15,3,1,8);
insert into rental(book_date,act_ret_date,exp_ret_date,tit_id,tit_copy_id,mem_id)
values(sysdate-30,sysdate-25,sysdate-20,3,2,9);
insert into rental(book_date,act_ret_date,exp_ret_date,tit_id,tit_copy_id,mem_id)
values(sysdate-20,sysdate-15,sysdate-10,4,1,10);
insert into rental(book_date,act_ret_date,exp_ret_date,tit_id,tit_copy_id,mem_id)
values(sysdate-10,sysdate-5,sysdate-3,5,1,6);
insert into rental(book_date,act_ret_date,exp_ret_date,tit_id,tit_copy_id,mem_id)
values(sysdate-12,sysdate-7,sysdate-4,8,1,7);
insert into rental(book_date,act_ret_date,exp_ret_date,tit_id,tit_copy_id,mem_id)
values(sysdate-11,sysdate-6,sysdate-1,10,1,5);
实现业务逻辑:
1.查询出所有用户以及用户所借阅的影像资料名字和借阅的日期:
select m.last_name,t.title,r.book_date
from member m,rental r,title_copy tc,title t
where m.id=r.mem_id
and r.tit_id=tc.title_id
and r.tit_copy_id=tc.id
and tc.title_id=t.id;
2.查询出最近一周订阅影像资料的用户和相应的影像资料名字及借阅日期:
select m.last_name,t.title,r.book_date
from member m,rental r,title_copy tc,title t
where m.id=r.mem_id
and r.tit_id=tc.title_id
and r.tit_copy_id=tc.id
and tc.title_id=t.id
and r.book_date between sysdate-7
and sysdate;
select m.last_name,m.address,r.book_date
from member m,rental r,title_copy tc,title t
where m.id=r.mem_id
and r.tit_id=tc.title_id
and r.tit_copy_id=tc.id
and tc.title_id=t.id
and r.exp_ret_date=next_day(sysdate,'星期日');
4.查询出已经超期还未归还的影像资料和借阅者的姓名,地址:
select m.last_name,m.address
from member m,rental r,title_copy tc,title t
where m.id=r.mem_id
and r.tit_id=tc.title_id
and r.tit_copy_id=tc.id
and tc.title_id=t.id
and r.act_ret_date is null;
select title
from title
where id=(select tit_id
from(select tit_id,count(*) c
from rental
where book_date between add_months(sysdate,-1) and sysdate
group by tit_id
order by c desc)
where rownum<=1);
6.查询出已经登记但是还没有拷贝的影像资料:
select title
from title
where id not in(
select title_id
from title_copy);
7.查询出本周预定最多的影像资料:
select title
from title
where id=(select tit_id
from (select tit_id,count(*) c
from reservation
where res_date between sysdate-7 and sysdate
group by tit_id
order by c desc)
where rownum<=1);