create database Library
on(
name='library',
filename='e:\yangbo\library.mdf',
size=5,
filegrowth=15%
)
log on(
name='library_log',
filename='e:\yangbo\library_log.ldf',
size=1MB,
filegrowth=15%
)
use library
go
--创建图书信息表
create table Book(
BID VARCHAR(50) NOT NULL PRIMARY KEY(BID) CHECK(BID LIKE 'ISBN%'),--图书编号,主键,该栏必填,必须以'isbn'开头
BName varchar(50) not null, --图书书名,该栏必填
Author varchar(50), --作者姓名
PubComp varchar(50), --出版社
PubDate datetime check(pubdate<getdate()), --出版日期,必须小于当前日期
BCount int check(bcount>1 or bcount=1), --现存数量,必须大于等于1
price money check(price>0) --单价,必须大于0
)
--创建读者信息表
create table Reader(
RID varchar(50) not null primary key(rid), --读者编号,主键,该栏必填
RName varchar(50) not null, --读者姓名,该栏必填
LendNum int check(lendnum>0 or lendnum=0), --已借书数量,必须大于等于0
RAddress varchar(50) --联系地址
)
--创建图书借阅表
create table Borrow(
RID varchar(50) not null, --读者编号,复合主键,读者信息表的外键,该栏必填
BID varchar(50) not null, --图书编号,复合主键,图书信息表的外键,该栏必填
LendDate datetime not null default(getdate()), --借阅日期,复合主键,默认值为当前日期,该栏必填
WillDate datetime , --应归还日期,必须大于等于借阅日期,默认值为借阅日期+1个月
ReturnDate datetime --实际归还日期,默认值为空
)
alter table borrow
add constraint borrow_pk primary key(rid,bid,lenddate) --设置复合主键
alter table borrow
add constraint rid_fk foreign key(rid) references reader(rid) --设置RID为外键
alter table borrow
add constraint bid_fk foreign key(bid) references book(bid) --设置bid为外键
alter table borrow
add constraint default_willdate default(dateadd(mm,1,getdate())) for willdate --应归还日期的默认值
alter table borrow
add constraint willdate_check check(willdate>lenddate) --应归还日期的约束
--创建惩罚记录表
create table Penalty(
RID varchar(50) not null, --读者编号,复合主键,读者信息表的外键,该栏必填
BID varchar(50) not null, --图书编号,复合主键,图书信息表的外键,该栏必填
PDate datetime not null default(getdate()), --罚款日期,复合主键,默认值为当前日期,该栏必填
PType int, --罚款类型,1——延期 2——损坏 3——丢失
Amount money check(amount>0) --罚金金额,必须大于0
)
alter table penalty
add constraint penalty_pk primary key(rid,bid,pdate) --设置复合主键
alter table penalty
add constraint prid_fk foreign key(rid) references reader(rid) --设置外键
alter table penalty
add constraint pbid_fk foreign key(bid) references book(bid) --设置外键
--在图书信息表中增加'btotal'列,数据类型是INT,用于保存每种图书的馆藏总量
alter table book
add BTotal int
use Library
go
select *from Book
select *from Borrow
select *from Penalty
select *from Reader
--查询并输出罚款记录表,将罚款类型列的值用相应的文字说明(1-延期 2-损坏 3-丢失)
select Book.BName as '书名',Reader.RName as'借阅者',Penalty.PDate as'罚款日期','返款类型'=case
when Penalty.PType=1 then '损坏'
when Penalty.PType=2 then '延期'
when Penalty.PType=3 then '丢失'
end
,Penalty.Amount as '返款金额'
from Penalty
inner join Book on Penalty.BID=Book.BID
inner join Reader on Reader.RID=Penalty.RID
--查找图书借阅表中明天应归坏的所有借书记录,如果应归还图书记录等于0,显示提示星系“明天没有应归还的图书”,如果应归还图书记录小于10
--则将这些借阅记录的“应归还日期”列值加2,否则,输出明天应该换图书的清单,包括图书名,读者姓名,和借阅日期,并在清单最后给出应
--归还图书的总数量
declare @borrowSum int
select @borrowSum=COUNT(*)from Borrow where Borrow.WillDate=DATEADD(DD,1,GETDATE())
if(@borrowSum=0)
begin
print '明天没有应归还的图书'
end
else if(@borrowSum<10)
begin
update Borrow
set WillDate=DATEADD(DD,2,WillDate)
where Borrow.WillDate= DATEADD(DD,1,GETDATE())
end
else
begin
select Book.BName as '图书名称', Reader.RName as'读者姓名',Borrow.LendDate as'借阅日期'
from Borrow
inner join Book on Borrow.BID=Book.BID
inner join Reader on Reader.RID=Borrow.BID
where Borrow.WillDate= DATEADD(DD,1,GETDATE())
union
select '总计',' ',(select COUNT (*)from Borrow where Borrow.WillDate= DATEADD(DD,1,GETDATE()))
from Borrow
where Borrow.WillDate= DATEADD(DD,1,GETDATE())
end
--统计并输出图书馆当前现有各种图书的册数和总金额,入股图书现有册数不到一万本,就显示信息“现有图书不足一万本,还需要继续购置书籍”
--否则,显示信息“现有图书在一万本以上,需要管理员加强图书管理”
select '书籍总量'=(select COUNT(*)from Book), '总金额'=(select sum(book.price)from book)
if((select COUNT(*)from Book)<10000)
begin
print '现有图书不足一万本,还需要继续购置书籍'
end
else
begin
print '现有图书在一万本以上,需要管理员加强图书管理'
end
--使用子查询,获得当前没有被读者借阅的图书信息。
--要求:输出图书名称,图书编号,作者姓名,出版社,和单价
select book.BName as '图书名称',Book.BID as '图书编号', Book.Author as'作者姓名',book.PubComp as'出版社',Book.price as'单价'
from Book
where Book.BID not in (select Borrow.BID from Borrow)
--使用子查询获得今年的所有缴纳罚款的读者清单,要求输出的数据包括读者姓名,图书名称,罚款日期,罚款类型和缴纳罚金等
--联合查询
select Reader.RName as'读者姓名', book.BName as'图书名称',Penalty.PDate as '罚款日期','罚款类型'=case
when Penalty.PType=1 then '延期'
when Penalty.PType=2 then '损坏'
when penalty.PType=3 then '丢失'
end,
Penalty.Amount as '罚款金额'
from Penalty
inner join Book on Penalty.BID=Book.BID
inner join Reader on Penalty.RID=Reader.RID
where DATEPART(YY,Penalty.PDate)=DATEPART(YY,GETDATE())
--子查询
--select (select Reader.RName as name from Reader where Reader.RID
-- in(select penalty.RID from Penalty where DATEPART(YY,Penalty.PDate)=DATEPART(YY,GETDATE()))),
-- (select book.BName from Book where Book.BID
-- in(select Penalty.BID from Penalty where DATEPART(YY,Penalty.PDate)=DATEPART(YY,GETDATE()))),
-- Penalty.PDate as '罚款日期','罚款类型'=case
-- when Penalty.PType=1 then '延期'
-- when Penalty.PType=2 then '损坏'
-- when penalty.PType=3 then '丢失'
-- end,Penalty.Amount as '罚款金额'
-- from Penalty
-- where DATEPART(YY,Penalty.PDate)=DATEPART(YY,GETDATE())
--使用子查询获得地址为空的所有读者尚未归还的图书清单
--要求:按读者编号从高到低,借书日期由近到远的顺序输出读者编号,读者姓名,图书名称,借书日期,和应归还日期
use Library
go
select Borrow.RID as '读者编号', Reader.RName as'读者姓名',Book .BName as'图书名称',Borrow.LendDate as '借书日期',Borrow.WillDate as '应归还日期'
from Borrow
inner join Book on book.BID=Borrow.BID
inner join Reader on Reader.RID=Borrow.RID
where Reader.RAddress='null'
--为张三办理借阅'java语言逻辑'的手续,要求编码实现
--提示:在图书借与诶包中增加一条图书借阅记录的同时,将图书信息表中这本书的当前数量-1,在图书信息表中未‘张无忌’记录已借书数量加1
begin transaction
declare @error int
set @error=0
insert into Borrow(RID,BID)
values ((select Reader.RID from Reader where Reader.RName='张三' ),
(select Book.BID from Book where Book.BName='java语言逻辑'))
set @error=@error+@@ERROR
update Book
set book.BCount=Book.BCount-1
where Book.BName='java语言逻辑'
set @error=@error+@@ERROR
update Reader
set reader.LendNum=Reader.LendNum+1
where Reader.RName='张三'
set @error=@error+@@ERROR
if(@error=0)
begin
print '借书成功'
commit transaction
end
else
begin
print '输入错误,请从新办理'
rollback transaction
end
--编写读者‘张三’缴纳罚金归还图书的手续,要求一次完成以下功能
--1:在罚款记录表中增加一条记录,记录‘张三’因延期还‘java语言逻辑’艺术而缴纳滞纳金5.6元
--2:在图书借阅表中修改归还日期为当前日期
--3:将读者信息表中已借书数量减1
--4: 将图书表中库存数量加1
begin transaction
declare @error int
set @error=0
insert into Penalty
values ((select Reader.RID from Reader where Reader.RName='张三'),
(select Book.BID from Book where Book.BName='java语言逻辑'),
GETDATE(),1,5.6)
set @error=@error+@@ERROR
update Book
set book.BCount= book.BCount+1
where Book.BName='java语言逻辑'
set @error=@error+@@ERROR
update Reader
set reader.LendNum=Reader.LendNum-1
where Reader.RName='张三'
set @error=@error+@@ERROR
update Borrow
set borrow.ReturnDate=GETDATE()
where Borrow.BID=(select Book.BID from Book where Book.BName='java语言逻辑')
and Borrow.RID=(select Reader.RID from Reader where Reader.RName='张三')
set @error=@error+@@ERROR
if(@error=0)
begin
print '归还成功'
commit transaction
end
else
begin
print '输入错误,请从新输入'
rollback transaction
end
--在图书馆日常工作中,图书馆管理员希望及时得到最新的到期图书清单,包括图书名称,到期日期和读者姓名等信息
--而读者则关心各种图书信息,如图书名称,馆存量和可借阅数量等,请编写代码按上面的需求在图书名称字段创建索引
--为图书管理员和读者分别创建不同的查询视图,并利用所创建的索引和视图获得相关查询数据
create view vw_daoqibook
as
select Reader.RName as'姓名', Book.BName as '图书名称',Borrow.WillDate as '到期日期'
from Book
inner join Borrow on Book.BID=Borrow.BID
inner join Reader on Borrow.RID=Reader.RID
where Borrow.WillDate>DATEADD(DD,-1,GETDATE()) and Borrow.WillDate<GETDATE()
select *from vw_daoqibook
create view vw_readerBook
as
select Book.BName as '图书名称',Book.BCount as'可借阅数量', Book.BTotal as '馆存量'
from Book
select *from vw_readerBook
create index ix_book_bName
on book(bname)
select *from Book
with (index=ix_book_bName)
--使用存储过程统计显示以‘清华’冠名的出版社出版的图书信息,要求:出版社名称作为参数传递给存储过程
create proc usp_bookInformation
@bookSum int output,
@bookName varchar(50)
as
select *from Book
where Book.PubComp=@bookName
select @bookSum=COUNT(*)from Book
where Book.PubComp=@bookName
go
declare @bookSum int
exec usp_bookInformation @bookSum output,'清华大学出版社'
print '清华大学出版社的图书共有'+convert(varchar(20),@bookSum)+'本'
--使用存储过程统计某一时间段内各种图书借阅人次,要求:如果没有指定起始日期,就以前一个月当日作为起始日期
--如果没有指定截止日期,就以当日作为截止日期,统计的总结月人次作为存储过程的返回值返回
create proc usp_sumReader
@SumReader int output,
@beginDate datetime,
@endDate datetime
as
select @SumReader=COUNT(*)from Borrow
where Borrow.LendDate between @begindate and @endDate
GO
declare @SumReader int
declare @beginDate datetime
declare @endDate datetime
select @beginDate=DATEADD(MM,1,GETDATE())
select @endDate=GETDATE()
exec usp_sumReader @sumReader output,@begindate,@endDate
print @sumreader
--存储过程实现插入借阅记录,输入参数为,借书人ID,借书人姓名,借阅的图书书名,要求同时完成如下操作
--1:图书信息表BOOK对应的图书数量-1
--2:读者信息表reader对应的读者已借书数量+1,如果没有改借阅者的信息,则新加一条读者信息记录
--3:向图书借阅表BORROW添加一条借阅记录,借阅日期,应归还日期和实际归还日期都采用默认值
if exists(select *from sysobjects where name='usp_insertBorrow')
drop proc usp_insertBorrow
create proc usp_insertBorrow
@readID varchar(20),
@readName varchar(20),
@bookName varchar(20)
as
if exists (select *from Reader where Reader.RID=@readID)
begin
begin transaction
declare @error int
set @error=0
insert Borrow(rid,bid)
values(@readID,(select book.BID from Book where Book.BName=@bookName))
set @error=@error+@@ERROR
update Book
set book.BCount=Book.BCount-1
where Book.BName=@bookName
set @error=@error+@@ERROR
update Reader
set reader.LendNum=Reader.LendNum-1
where Reader.RID=@readID
set @error=@error+@@ERROR
if(@error =0)
begin
print '插入借阅记录成功'
commit transaction
end
else
begin
print '插入记录失败'
rollback transaction
end
end
else
begin
begin transaction
insert Reader(rid,rname)
values (@readID,@readName)
declare @error1 int
set @error1=0
insert Borrow(rid,bid)
values(@readID,(select book.BID from Book where Book.BName=@bookName))
set @error1=@error1+@@ERROR
update Book
set book.BCount=Book.BCount-1
where Book.BName=@bookName
set @error1=@error1+@@ERROR
update Reader
set reader.LendNum=Reader.LendNum+1
where Reader.RID=@readID
set @error1=@error1+@@ERROR
if(@error1 =0)
begin
print '插入借阅记录成功'
commit transaction
end
else
begin
print '插入记录失败'
rollback transaction
end
end
go
exec usp_insertBorrow '10','小明','数据库语言和设计'
alter table reader
add constraint default_lendNum default(0) for lendNum
select *from Reader
select *from Book
select *from Borrow