数据库复习题

  • 格式:doc
  • 大小:105.00 KB
  • 文档页数:9

电子商务《数据库技术与应用2》期终样卷(1)1、数据库的基本操作-15分-用Transact-SQL语句创建一个数据库shop02,将包含语句的脚本文件保存为crea_database.sql,存放在考生目录下。

数据库的相关参数如下:数据库由一个主数据文件shop_data1,一个日志文件shop_log组成,数据文件存放在D:\shop\data,日志文件存放在D:\shop\log文件夹中。

shop_data1的初始尺寸为5MB,不限定最大尺寸,增长量为20%,属于主文件组;shop_log的初始尺寸为5MB,最大尺寸为8MB,增长量为10%。

create database shopon primary(name='shop',filename='d:\shop\log\data.mdf',size=3MB,maxsize=10MB,filegrowth=10%)log on(name='shop1',filename='d:\shop\log\shop.ldf',size=1MB,maxsize=8MB,filegrowth=5%)2、表的基本操作-15分-在shop02数据库中,用Transact-SQL语句创建表格,将包含语句的脚本文件保存为crea_table.sql按要求创建如下表格:商品信息表(ProductInfo)use shopgoIF EXISTS(SELECT*FROM sysobjects WHERE NAME='shop')DROP TABLE shopGOcreate table ProductInfo(ProductId int not null,ProductName Nvarchar(50)not null,ProductPrice decimal (9),Intro Nvarchar(11),CategoryID int,ClickCount decimal (9,2))alter table ProductInfo add constraint PK_ProductId primary key(ProductId)alter table ProductInfo add constraint Fk_CategoryID foreign key(CategoryID) references Category(CategoryID)商品分类表(Category)use shopgoIF EXISTS(SELECT*FROM sysobjects WHERE NAME='shop')DROP TABLE shopGOcreate table Category( GukeID int not null,GukeName Nvarchar(20)not nullMemo varchar(50) )alter table Category add constraint PK_CategoryID primary key(CategoryID)3、数据的基本操作-40分-将数据文件shop附加到SQL Server服务器上,创建数据操作所需要的数据环境。

在shop 数据库中用Transact-SQL语句实现如下操作。

将包含语句的脚本与语句执行的结果(可以截图)一起保存到oper_table.doc中-1- 数据查询(-20分-)1)查询所有男性用户的基本信息.2) 查询电子邮件未填或者地址未填的用户3) 按商品的种类统计点击排行,按点击数降序排列4) 查询数码类商品的商品名称、点击数-2- 数据更新(-15分-)1) 给消费额大于1000元的用户,赠送500元券,充入其账户金额中2) 清除地址为空的所有用户记录3) 为商品表添加一条记录,商品编号为’s020101’,商品名为‘蝴蝶酥’,价格为10元的记录-3- 索引/视图(-5分-)1) productinfo表上常用条件查询:select * from productinfo where productname=’自行车’,请为该表设计索引index_pro02,并用T-SQL语句实现。

4、SQL编程初步-30分-在shop数据库中,根据题意完成下面的代码,调试运行,并将包含语句的脚本与执行的结果(可以截图)一起保存到program.doc中。

1)事务(-20分-)【题目】模拟实现ATM取款机的取款和存款业务。

需求说明:(1)实现一种取款或存款中的一种业务便可。

(2)交易步骤如下:●向交易明细表插入交易类型(支取/存入)。

●更新账户余额。

请完成横线上的代码,并调试代码,把①调试成功的代码、②调试数据、③结果截图放在代码文件program.doc中。

--创建账户信息表bank和交易信息表transinfocreate database banksif exists(select * from sysobjects where name='bank') drop table bankif exists(select * from sysobjects where name='transinfo') ①transinfocreate table bank(CustomerName varchar(10) not null,CardID char(10) not null primary key,CurrentMoney money not null default(0))Create table ②transinfo(CardID char(10) not null,TransType char(4) not null,TransMoney money not null,TransDate datetime not null default(getdate()))alter table transinfo add constraint FK_transinro_bank foreign key(CardID) references bank( cardid ③)alter table bank add constraint CK_currentmoney check④(CurrentMoney>=10)alter table transinfo add constraint CK_transtype check(TransType='存入' or TransType='支取')insert into bank(Customername,CardID,CurrentMoney)values('zhangsan','10010001',1000)/*开始事务*/begin transactiondeclare @cardid char(10),@tt char(4),@tm money,@cn varchar(10),@td datetime set @cn='zhangsan'set @tt='支取'set @tm=1000declare @errorsum intset @errorsum=0/*实现取款,张三的卡号支取1000,更新账户金额*/select @cardid=cardid from bank where customername=@cninsert into transinfo( cardid.transtype,transmoney ⑤) values(@cardid,@tt,@tm)set @errorsum=@errorsum+ ⑥if @tt=’存入’⑦beginupdate bank set currentmoney=currentmoney+@tm where customername=@cnset @errorsum=@errorsum+@@errorendelsebeginif @tt='支取'beginupdate bank set currentmoney=currentmoney-@tm where customername=@cnset @errorsum=@errorsum+@@errorendelseprint '交易类型不对!'end/*根据是否有错误,确定事务是提交还是撤销*/if @errorsum <>0⑧beginprint '回滚事务'⑨transactionendelsebeginprint '提交事务,数据永久保存!'commit transaction⑩end/*查询取款后的余额和交易信息*/select top 1 bank.cardid,customername,currentmoney,transtype,transmoney,transdatefrom bank,transinfowhere bank.cardid=transinfo.cardid and customername=@cn order by transdate desc以下(11)(12)占10分调试数据:(11)调试结果:(12)2)存储过程(-10分-)【题目】创建存储过程AddNewProduct添加新的商品,往Product表中添加新的商品信息,输入参数有商品名称Product_Name、商品价格Price、所属分类product_type。

如果存在该类别下的该商品,则提示“该商品已经存在!”,否则,添加该商品记录,如果成功,则提示“添加成功!”,不然提示“未能成功添加!”请完成横线上的代码,并调试代码,把①调试成功的代码、②调试数据、③结果截图放在代码文件program.doc中。

if exists(select * from sys.objects where name='AddNewProduct') drop ①procedure AddNewProductcreate procedure AddNewProduct(@pid varchar(10),@pn varchar(20),@pp money,@pt varchar(10))asdeclare @errorsum intset @errorsum=0if ②exists (select * from product where product_id=@pid)print '该商品已经存在!'elsebegininsert into product ③set @errorsum= ④+@@errorif @errorsum<>0print '记录添加失败!'⑤elseprint '记录添加成功!'end以下(6)(7)占5分调试数据:(6)调试结果:(7)电子商务《数据库技术与应用2》期终试题(2)5、数据库的基本操作-15分-用Transact-SQL语句创建一个数据库shop01,将包含语句的脚本文件保存为crea_database.sql,存放在考生目录下。