zoukankan      html  css  js  c++  java
  • Sql Server 学习2

    很多东西只有自己动手去做了才能理解  其中的含义   呵呵  先把练手的代码贴出来以后有空了再做分析吧

    本题用到下面三个关系表:
    CARD     借书卡:   (CNO 卡号,NAME  姓名,CLASS 班级)
    BOOKS    图书:     (BNO 书号,BNAME 书名,AUTHOR 作者,PRICE 单价,QUANTITY 库存册数 )
    BORROW   借书记录: (CNO 借书卡号,BNO 书号,RDATE 还书日期
    
    
    --1.    写出自定义函数,要求输入借书卡号能得到该卡号所借书金额的总和
    
    CREATE FUNCTION GetSUMP 
    (
    @CNO int
    )
    RETURNS int
    AS
    BEGIN
        declare @sum int
    select @sum=sum(price) from BOOKS where bno in (select bno from BORROW where cno=@CNO)
    
    return @sum
    END
    GO
    -- 2. 找出借书超过5本的读者,输出借书卡号及所借图书册数。
    select cno,COUNT(bno) t from borrow group by cno having count(bno)>=2
    -- 6. 查询现有图书中价格最高的图书,输出书名及作者。
    
     select author,t_Books.bname from t_Books where price in  (select  max(price) from t_Books)
     --  7. 查询当前借了"计算方法"但没有借"计算方法习题集"的读者,输出其借书卡号,并按卡号降序排序输出。
    select * from borrow bb where bno= (select bno bt from t_Books where bname='计算机方法')and bb.cno
     not in
    (select cno c from borrow where bno=(select bno b from t_Books where bname='计算机方法习题集') ) order by cno desc;
    
     
    
    select cc.cno from borrow cc,t_Books dd
        where cc.bno=dd.bno
        and dd.bname='计算机方法'
        and  not exists(
        SELECT * FROM BORROW aa,t_Books bb
            WHERE aa.BNO=bb.BNO
                AND bb.BNAME='计算机方法习题集'
             and cc.cno=aa.cno)
    
    --将"C01"班同学所借图书的还期都延长一周。
      update  borrow set  borrow.rdate=dateadd(day,7,rdate) where cno in (select bno from borrow where cno=1)
      --  9. 从BOOKS表中删除当前无人借阅的图书记录。
    delete t from t_books t
     where 
      not exists( select * from borrow where t.bno=bno)
    
     --  10.如果经常按书名查询图书信息,请建立合适的索引。
     create clustered index IBname on t_Books(bname)
     --  11.在BORROW表上建立一个触发器,完成如下功能:如果读者借阅的书名是"数据库技术及应用",
     --就将该读者的借阅记录保存在BORROW_SAVE表中(注ORROW_SAVE表结构同BORROW表)。
    alter trigger trg_insert on borrow
    for insert,update
    as
    IF @@ROWCOUNT>0
    insert  borrow_save 
    SELECT i.*
    FROM INSERTED i,t_books b
    WHERE i.BNO=b.BNO and b.bname='数据库技术及应用'
    
    
    insert into t_Books values('数据库技术及应用','张啊红',90.00,90)
    
    insert into borrow values(6,7,GETDATE())
    --吧查询出来的数据存入指定表中
    insert t_Books select bname,author,price,quantity from t_Books where bname='诛仙3'
    -- 12.建立一个视图,显示"01"班学生的借书信息(只要求显示姓名和书名)。
    alter view v_v05
    as
    select cc.name,tb.bname from borrow b,t_Card cc,t_Books tb where b.cno=cc.cno and tb.bno=b.bno and b.cno in(select cno c from t_Card where class='09软工5版')
    
    ----
    CREATE VIEW V_VIEW
    AS
    select name,bname 
    from  t_Books,t_Card,borrow 
    where borrow.cno=t_Card.cno and borrow.bno=t_Books.bno and class='09软工5版'
    
    
    select * from v_v05
    select * from V_VIEW
    
    ---  13.查询当前同时借有"计算方法"和"组合数学"两本书的读者,输出其借书卡号,并按卡号升序排序输出。
    
    
     select cno,COUNT(bno) 
     from borrow bb 
     where bb.bno in(select bno bb from t_Books where bname in ('计算机方法','组合数学'))
     group by cno having COUNT(bno)=2 
     order by cno desc
    
     --select cno,count(bno)  from borrow group by cno
    
     select * from borrow where bno 
     in (select bno from t_Books where bname='计算机方法') and cno in
     (select cno from borrow where  bno in (select bno from t_Books where bname='组合数学'))
     order by cno desc
     ---  14、用事务实现如下功能:一个借书卡号借走某书号的书,则该书的库存量减少1,
     ---当某书的库存量不够1本的时候,该卡号不能借该书
     alter procedure proBorrowBook
     (
        @cno int ,
        @bno int,
        @rdate date
     )
     as
     begin
        begin transaction
        declare @quantity int 
        select @quantity=quantity from t_Books where bno=@bno;
        insert into borrow values(@cno,@bno, dateadd(day,30, @rdate));
        update t_Books set quantity=@quantity-1 where bno=@bno;
        if(@quantity>0 and @@error<=0)
        begin
            commit transaction
            print '借书成功'
        end
        else
        begin
            rollback transaction
            print '无库存'
        end
     end
     
    
     select GETDATE()
     exec proBorrowBook 2,1,'2013-01-08 17:46:01.160'
    
    
     --15、用游标实现将书号为‘80’的书本的价格提高10元
    
     declare @bno int
     declare @bname varchar(30)
     declare @author varchar(30)
     declare @price numeric(10,2)
     declare @quantity int 
    
     declare myCursor cursor for select * from t_Books
    
     open myCursor
    
     fetch next from myCursor into @bno,@bname,@author,@price,@quantity
    
     while(@@FETCH_STATUS=0)
     begin
        if(@bno=2)
        begin
            update t_Books set price=@price+100 where current of myCursor
        end
        fetch next from myCursor into @bno,@bname,@author,@price,@quantity
     end
     close myCursor
    
     deallocate myCursor

  • 相关阅读:
    15天玩转redis —— 第四篇 哈希对象类型
    15天玩转redis —— 第三篇 无敌的列表类型
    15天玩转redis —— 第二篇 基础的字符串类型
    15天玩转redis —— 第一篇 开始入手
    双十一来了,别让你的mongodb宕机了
    AutoIncrement无法设置的问题
    Frame animation
    Tween animation
    在project窗口中快速定位文件
    Activty左出右进动画
  • 原文地址:https://www.cnblogs.com/mxxblog/p/2853535.html
Copyright © 2011-2022 走看看