叶磊数据库Word下载.docx
《叶磊数据库Word下载.docx》由会员分享,可在线阅读,更多相关《叶磊数据库Word下载.docx(17页珍藏版)》请在冰豆网上搜索。
当修改orderbook表中相应商品数量时,实现商品数量相应变动。
2.创建一触发器tg2,当往orderbook中增加记录时,如果商品库存量少于10,则不允许购买该商品,并给出相应提示。
新建图书订购情况统计表bookstas(包含图书编号、图书名称、图书类别、图书价格和订购册数,数据类型自定),并根据数据库的订单情况将社科类图书的订购情况插入表中。
创建一触发器tg3,当往orderbook表中增加记录时,自动更新bookstas表相应图书的统计信息
4源码,文档化
---新建一存储过程proc1,显示指定用户ID的订单信息列表
---(订单号、成交时间、订单总金额、订单状态及所有订单的平均金额),并成交时间降序排列。
createprocedureproc1
@user_idvarchar(20)
as
selectoid,ordertime,payment,orderstate,avgpayment,sumpayment
from"
order"
orderstate,
(selectavg(payment)avgpayment
from"
where"
user"
=@user_id)a,
(selectsum(payment)sumpayment
=@user_id)b
where"
.state=orderstate.osid
and"
=@user_id
orderbyordertimedesc
go
execproc1102
---新建一存储过程proc2,通过输入订单号、书目ID及数量,
---实现给该订单增加商品、更新订单总金额,并返回当前订单包含的商品总数(商品类别数量)。
createprocedureproc2(
@order_idvarchar(20),
@book_dvarchar(20),
@book_svarchar(20)
)
update"
setpayment=payment+price
frombook,"
whereoidin(
selectorderid
fromorderbook
wherebookid=@book_d)
andbid=@book_d
updateorderbook
setquantity=quantity+@book_s
whereorderid=@order_id
selectbookid
fromorderbook
execproc22014001,1001,4
---新建一存储过程proc3,通过修改指定订单、指定商品的价格,并返回该订单的新总金额及优惠金额。
createprocedureproc3
@book_idvarchar(20),
@s_pricevarchar(20)
setpayment=payment+((@s_price-price)*quantity)
frombook,orderbook
wherebookid=@book_idand(bid=@book_idandorderid=@order_id)
select*
whereoid=@order_id
execproc32014001,1001,20
---新建一存储过程proc4,统计各订单总金额的分布情况。
---0-50元、-100元、-150元、-200元和元以上。
createprocedureproc4
begin
declare@zerotofiftyint,
@fiftytofundredint,
@hundredtohunfint,
@hunftoTwohint,
@moretwohint;
select@zerotofifty=COUNT(case
whenpayment>
=0ANDpayment<
=50then'
1'
end),
@fiftytofundred=COUNT(case
whenpayment>
=51ANDpayment<
=100then'
2'
@hundredtohunf=COUNT(case
=101ANDpayment<
=150then'
3'
@hunftoTwoh=COUNT(case
=151ANDpayment<
=200then'
4'
@moretwoh=COUNT(case
=201then'
5'
end)
from"
print@zerotofifty;
print@fiftytofundred;
print@hundredtohunf;
print@hunftoTwoh;
print@moretwoh
end
execproc4
---新建一存储过程proc5,实现查询购买过指定书目ID的用户还同时
---购买过的购买次数最多的前名书目名称及购买次数,并按照购买次数排序。
createprocedureproc5
@book_idvarchar(20)
declare@user_idint;
select@user_id=uid
fromorderbook,"
"
wherebookid=@book_id
andorderid=oid
and"
."
=uid
selectuid,name,oid,quantity,a.bookid,a.title
orderbook,(selecttop3sum(quantity)sumquantity,bookid,title
fromorderbook,book
wherebookid=bid
groupbybookid,title
orderbysumquantitydesc)a
whereuid=@user_id
anduid="
andoid=orderid
andorderbook.bookid=a.bookid
orderbya.sumquantitydesc
execproc51003
---新建一存储过程proc6,实现查询与指定用户ID购买过
---相同商品的用户及购买过相同商品的数量,并按照相同商品数量降序排列。
createprocedureproc6
selecta.uid,a.name,a.bookid,a.quantity
(selectuid,name,bookid,quantity
fromorderbook,"
whereorderid=oid
and"
=uid)a
where"
.uid=@user_id
="
.uid
.oid=orderbook.orderid
anda.uid!
="
.uid
anda.bookid=orderbook.bookid
groupbya.uid,a.name,a.bookid,a.quantity
orderbyquantitydesc
execproc6102
---创建一触发器tg1,当往orderbook表中增加记录时,
---实现商品库存相应减少;
createtriggertg1
onorderbook
forinsert,update
declare@order_idint,
@book_idint,
@quantityint;
select@book_id=bookid,@quantity=quantity
frominserted;
updatebook
setbook.stock=book.stock-@quantity
where@book_id=book.bid;
print'
insertsucceed'
;
declare@book_idfint,
@quantityfint,
@bookidoint,
@quantityoint,
@lstint;
select@bookido=bookid,@quantityo=quantity
fromdeleted;
select@book_idf=bookid,@quantityf=quantity
setbook.stock=book.stock-@quantityf+@quantityo
where