您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 图书管理系统触发器和存储过程
图书馆管理系统数据库---------------创建数据库和数据表createdatabaseLibrarySystemonprimary(name='LibrarySystem',filename='f:\LibrarySystem\LibrarySystem.mdf',size=10240KB,maxsize=unlimited,filegrowth=1024KB)logon(name='LibrarySystem_log',filename='f:\LibrarySystem\LibrarySystem_log.ldf',size=1024KB,maxsize=unlimited,filegrowth=1024KB)go/*创建数据表:图书信息表,读者信息表,用户表,读者借阅表,读者还书表等*/useLibrarySystemgo----------图书信息表CREATETABLEBook(Bidnchar(30)NOTNULL,----分类号Bnamevarchar(50)NOTNULL,----书名Bwriternvarchar(20)NOTNULL,----作者BpubAdrvarchar(30)NOTNULL,----出版社BpubDatesmalldatetime,----出版日期Btypevarchar(20),----类别BtotalNumintNOTNULL,----总量BborrowedNumintdefault0,----借出量默认值为零constraintBook_PKprimarykey(Bid)----创建“分类号”为主键)gouseLibrarySystemgo----------读者信息表CREATETABLEReader(Rnonvarchar(10)NOTNULL,----读者编号Rnamenvarchar(20)NOTNULL,----姓名Rsexchar(2)check(Rsexin('男','女')),----性别只能在“男”和“女”之间选择Rageintnotnull,---读者年龄Rphonenvarchar(15),----电话/手机Readertypenvarchar(20)check(Readertypein('本科生','研究生','教师')),----读者类型只能在“本科生”,“研究生”,“教师”之间选择constraintReader_PKprimarykey(Rno)----创建“读者编号”为主键)go----------读者借阅信息表createtableReaderBook(Rnonvarchar(10)NOTNULL,----读者编号Bidnchar(30)NOTNULL,----分类号RBorrowDatesmalldatetimeNOTNULL,----借阅日期RBackDatesmalldatetimenotnull,----应还书日期RReborrowTimestinyintdefault0,----续借次数默认为次RoverdueDaysint,----图书超期天数constraintReaderBook_PKprimarykey(Rno,Bid),----创建“读者编号,分类号”为主键foreignkey(Rno)referencesReader(Rno),----创建“读者编号”为外部键foreignkey(Bid)referencesBook(Bid)----创建“分类号”为外部键)go------用户表createtableAdmin(AdminIdvarchar(10)notnull,----用户IDAdminPswvarchar(10)notnull,----用户密码AdminNamevarchar(20)notnull,----用户名constraintAdmin_PKprimarykey(AdminId)----创建用户ID为主键)CreatetableRReturnBook(Rnonvarchar(10)NOTNULL,Rnamenvarchar(20)NOTNULL,----姓名Rsexchar(2)check(Rsexin('男','女')),----性别只能在“男”和“女”之间选择Rageintnotnull,---读者年龄Bidnchar(30)NOTNULL,----分类号Bnamevarchar(50)NOTNULL,----书名Bwriternvarchar(20),---作者Btypevarchar(20),---图书类别BpubAdrvarchar(30),---出版社RborrowTimestinyintdefault1,---记录读者借借阅同一本书的次数ReturnDatesmalldatetime,---还书时间constraintRReturnBook_PKprimarykey(Rno,Bid,RborrowTimes),---创建主键constraintRno_Fkforeignkey(Rno)referencesReader(Rno),constraintRBid_FKforeignkey(Bid)referencesBook(Bid)---创建外码)Go图书馆管理系统数据库--------------触发器的创建useLibrarySystemgo------------借书要求(书本没有库存,则无法进行借书操作)-----------createtriggertri_BookonBookforupdateasdeclare@btotalint,@bborrowedintselect@btotal=BTotalNum,@bborrowed=BborrowedNumfrominsertedif(@btotal@bborrowed)beginrollbacktransactionprint'借阅失败!'print'对不起,此书已经没有库存,无法进行本次借书操作!'endgouseLibrarySystemgo------------借书要求(读者最多借阅量)---------------假定教师最多只能借十本,本科生最多只能借五本书,研究生最多可以借八本书----createtriggertri_RBorrowNumonReaderBookforinsertasdeclare@nochar(10),@numint--------获得教师编号select@no=inserted.Rnofrominserted,ReaderwhereReader.Readertype='教师'--------统计教师借书总量并做相应处理beginselect@num=count(*)fromReaderBookwhereRno=@noif(@num10)--------假定教师最多只能借十本书beginrollbacktransactionprint'借阅失败!'print'对不起,你的借阅总量已经达到本,无法进行本次借书操作!请归还部分书籍后,再进行下次借书操作!'endendselect@no=inserted.Rnofrominserted,ReaderwhereReader.Readertype='本科生'beginselect@num=count(*)fromReaderBookwhereRno=@noif(@num5)---------假定本科生最多只能借五本书beginrollbacktransactionPrint'借阅失败!'print'对不起,你的借阅总量已经达到本,无法进行本次借书操作!请归还部分书籍后,再进行下次借书操作!'endendselect@no=inserted.Rnofrominserted,ReaderwhereReader.Readertype='研究生'beginselect@num=count(*)fromReaderBookwhereRno=@noif(@num8)---------假定研究生最多只能借八本书beginrollbacktransactionPrint'借阅失败!'print'对不起,你的借阅总量已经达到本,无法进行本次借书操作!请归还部分书籍后,再进行下次借书操作!'endendgo-------------续借次数要求-----------------假定教师最多允许续借四次,本科生最多允许续借两次,研究生最多可以续借三次------------useLibrarySystemgoCreatetriggertri_RRenewBookonReaderBookforupdateasdeclare@ttinyintselect@t=inserted.RReborrowTimesfrominserted,ReaderwhereReader.Readertype='教师'beginif(@t4)--------教师最多允许续借四次beginrollbacktransactionprint'续借失败!'print'对不起,你的续借次数已经达到了四次,已经无法再续借!'endendselect@t=inserted.RReborrowTimesfrominserted,ReaderwhereReader.Readertype='本科生'beginif(@t2)--------本科生最多允许续借两次beginrollbacktransactionprint'续借失败!'print'对不起,你的续借次数已经达到了两次,已经无法再续借!'endendselect@t=inserted.RReborrowTimesfrominserted,ReaderwhereReader.Readertype='研究生'beginif(@t3)--------研究生最多允许续借三次beginrollbacktransactionprint'续借失败!'print'对不起,你的续借次数已经达到了三次,已经无法再续借!'endendgouseLibrarySystemgo-----------读者还书信息入ReturnBook表-----------Createtriggertri_RReturnBookonReaderBookfordeleteasdeclare@tint,@tnovarchar(10),@Bidvarchar(30)selecttop1@tno=Rnofromdeletedselecttop1@Bid=Bidfromdeleted-----------图书信息更改过程updateBooksetBborrowedNum=BborrowedNum-1whereBid=@Bid--------判断RrturnBook表中该读者是否已借过同样一本书籍select@t=RborrowTimesfromReturnBookwhereRno=@tnoandBid=@Bidif(@t0)----@t0说明该读者过去借过同一本书beginset@t=@t+1endelsebeginset@t=1end---------向ReturnBook表中插入信息-----第一部分(主码先入)insertintoReturnBook(Rno,Bid,RborrowTimes,ReturnDate)values(@tno,@Bid,@t,getdate())-----第二部分(读者、书本信息)updateReturnBooksetRname=(selectRnamefromReaderwhereRno=@tno),Rsex=(selectRsexfromReaderwhereRno=@tno),Rage=(selectRagefromReaderwhereRno=@tno),Bname=(selectBnamefromBookwhereBid=@Bid),Bwriter=(selectBwriterfromBookwhereBid=@Bid),Btype=(selectB
本文标题:图书管理系统触发器和存储过程
链接地址:https://www.777doc.com/doc-6221441 .html