您的位置:首页 > 数据库

课程设计---图书馆数据库

2012-11-22 23:18 357 查看
                       图书馆数据库设计
 
一.实验内容:
为图书馆设计一个数据库,此数据库对每个借阅者保存读者记录,包括:借书证号、姓名、性别、单位、可借数量。对每本书记录:书号、书名、作者、出版社。对每本被借出的书有:读者号、借出日期、应还日期。
可以随时查阅书库中的书籍信息,所有的书籍均由书号唯一标识;可以随时查阅书籍借还情况,系统约定任何人可以借多本图书,任何一种图书可以为多人所借,借书证号具有唯一性。
 
二.实验要求:
1.基于《数据库概论》中所学的知识分析该数据库的基本结构,2.用Transact-sql命令完成如下要求:
(1)创建数据库和数据表,定义每个表的主键和外键,为每个属性选择合适的数据类型,定义每个属性是否允许空值,是否有默认值等;
(2)结合具体情况为数据库表设置合理的约束或规则;
(3)如果需要,为数据表设计合理的触发器;
(4)使用SQL语句,在你设计的每个表中插入至少3条数据,要求记录满足数据约束要求,且尽量真实可信;
(5)自行设计查询要求,给出经常需要用到的查询语句。
   3.写出完整的实验报告。
 

三.实验代码
--创建library数据库
create database library
on primary
(name=library_dat,--创建主数据库文件
 filename='c:\mydata\librarydat.mdf',
 size=5,
 maxsize=100,
 filegrowth=10%)
 log on
 (name='library_log',--创建日志数据库文件
 filename='c:\mydata\librarylog.ldf',
 size=5,
 maxsize=100,
 filegrowth=5)
 go
--借阅者信息表
 use library
 go
 create table reader  
 (rno char(10) primary key,
 rname char(10) not null,
 rsex char(2) check(rsex in ('男','女')),
 rdepart char(30) not null,
 rmaxb smallint not null default(5),
 )

 go
 create table book
  (bno char(10) primary key ,
   bname char(20) not null,
   bauthor char(20) ,
   bconcern char(20) not null,
   bnum int not null )
 go
 create table borrowbook
  (bbno char(20) primary key,
   rno char(10) foreign key(rno) references reader(rno),
   bno char(10) foreign key(bno) references book(bno),
   bbdateo datetime not null default(getdate()),
   bbdaten datetime not null default(dateadd(mm,2,getdate())),
   borrownum int 
not null default(1) )
 go
 --读者借书
create proc pr_borrow @rno char(10),@bno char(10)
as
  if(@rno in (select rno from reader) and @bno in(select bno from book))
    begin
      if((select bnum from book where bno=@bno) like 0 or (select rmaxb from reader where rno=@rno)
like 0)
         begin
         print '该书本已借完或借书已达最大书目'
         return -7
         end
      else

          if(@rno+@bno in (select bbno from borrowbook))
          begin
          update borrowbook
          set borrownum=borrownum+1
          where bbno=@rno+@bno
          update 
reader
          set rmaxb=rmaxb-1 where rno=@rno
          update book

          set bnum=bnum-1 where bno=@bno
          end
          else

            begin
            insert borrowbook(bbno,rno,bno)
            values (@rno+@bno,@rno,@bno)
            update 
reader
            set rmaxb=rmaxb-1 where rno=@rno
            update book

            set bnum=bnum-1 where bno=@bno
            end
    end
  else

    print '您输入的信息有误,请重新输入'
    return -7
 go
--读者还书
create proc pr_return @rno char(10),@bno char(10)
as
      update 
reader
      set rmaxb=rmaxb+1 where rno=@rno
      update book

      set bnum=bnum+1 where bno=@bno
      update borrowbook
      set borrownum=borrownum-1
      where bbno=@rno+@bno
      if ((select borrownum from borrowbook where bbno=@rno+@bno)<=0)
      begin
      delete
 from  borrowbook  where  bbno=@rno+@bno
      end
go
 
 
 
 
 
 
--添加book数据
 
 insert book
 values('s0001','数据库应用技术','刘卫军','清华大学出版社','10')
 insert book
 values('s0002','离散数学','屈婉玲','高等教育出版社','9')
 insert book
 values('s0003','计算机英语','刘艺','机械工程出版社','6')
  insert book
 values('s0004','java程序设计与教程','王行言','清华大学出版社','12')
 go
--添加
reader数据

 
 insert reader
 values('10168001','张三','男','计算机系通信工程','5')
 insert reader
 values('10168002','李四','女','计算机系网络工程','5')
 insert reader
 values('10168003','吴五','男','计算机系网络工程','5')
 insert reader
 values('10168004','王六','女','计算机系通信工程','5')
 go
 
 
 
 
 
 
 
 
 
 
 
 
四:图书馆数据库的实现
 --常用查询测试
 select * from reader
 select * from book  --查阅书库中的书籍信息
 select * from borrowbook --查阅书籍借还情况
 select rno from borrowbook where rno=10168001 --查询借书证为10168001的借书情况

 --测试中恢复数据用到的语句
 drop procedure pr_borrow
 drop procedure pr_return
 drop table borrowbook
 update book
 set bnum=8 where bno='s0001'
update borrowbook
set borrownum=1
update reader
set rmaxb=5
 --执行借书还书
 --1.一本书被多人所借(书借完提示错误)
/*借书说明:
   读者借书使用存储过程pr_borrow,读者借一本书的会记录到

   borrowbook中,并对book表中的书数量bnum减一,读者最大借书数量减一,读者重复借同一本书籍是会修改borrowbook中的借书数量borrownum。若读者最大借书数或图书书目等于0

   则不修改borrowbook表,并提示错误消息。*/
exec pr_borrow '10168001','s0001'
exec pr_borrow '10168003','s0001'   
exec pr_borrow '10168002','s0001'--重复执行这三条中一条消息会显示'该书已借完'
select * from reader
 select * from book 
 select * from borrowbook

 
 
 --2.一人借多本书
 
exec pr_borrow '10168001','s0001'
exec pr_borrow '10168001','s0002'
exec pr_borrow '10168001','s0003'
select * from reader
 select * from book  
 select * from borrowbook

 
 
 
 
 --3.还书 (借书书目还清后删除借书记录)
/*还书说明:
    还书修改borrowbook记录并修改对应读者和书籍的rmaxb和bnum加一。
*/
exec pr_return '10168001','s0001'
exec pr_borrow '10168003','s0001'   
exec pr_borrow '10168002','s0001'
select * from reader
 select * from book  
 select * from borrowbook
go

 






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库