GDOU-B-11-112
大学学生实验报告书
实验名称 实验六:使用索引、视图和批量 课程名称 数据库原理与设计 学院(系) 学生姓名
成绩
软件学院
学号
专业
计算机软件工程 实验地点
班级 实验日期
实验目的: 1. 掌握索引的创建语法 2. 掌握视图的创建语法 3. 使用视图更新数据 4. 编写各种批量 实验内容 针对GlobalToyz和Recruitement,Student数据库,按要求完成下列题目。 1. 对于Toys表,在玩具名称上建立一个唯一索引,写出相应的T-SQL语句。 CREATE INDEX idxname ON Toys(cToyId DESC) 2. 对于Shipment表,我们会经常查看玩具的实际发送日期(dActualDeliveryDate),请问,我们应该建立什么索引加快每次查询该列的速度,并写出相应的T-SQL语句。 CREATE NONCLUSTERED INDEX idxname ON Shipment(dActualDeliveryDate DESC) 3. 创建一个视图,由Toys表的vToyDescription和mToyRate列构成,要求限制用户查看该视图的生成脚本语句。(视图文本加密使用 with encryption 语句) CREATE VIEW IS_Toys with encryption(固定语法) AS SELECT vToyDescription,mToyRate FROM toys 4. 对于Recruitment数据库,创建一个视图名为vwCandidateContractRecruiter,其中包含了外部候选人的代码,姓名,测试成绩,以及其对应的合同招聘人员的代码和名称。 CREATE VIEW vwCandidateContractRecruiter AS SELECT cCandidateCode,vFirstName, vLastName,siTestScore,cInterviewer, cInterviewername=(select cEmployeeCode from Employee) FROM ExternalCandidate 5. 对于第4题中创建的视图vwCandidateContractRecruiter进行更新,修改外部候选人代码为‘000049’的候选人,更改其测验成绩为87分,并更改相对应的合同招聘人员的名称为‘Roger Federal’。写出完成该操作的所有语句。(连接视图的更新,一次只允许更新一个基本表 ) UPDATE vwCandidateContractRecruiter SET sitestscore=87 WHERE cCandidateCode='000049' UPDATE vwCandidateContractRecruiter SET cInterviewername='Roger Federal' WHERE cCandidateCode='000049' 6. 编写一个批量,计算1到100的偶数和。(使用while) declare @x int,@sum int set @x=0 set @sum=0 while @x<=100 begin set @sum=@sum+@x set @x=@x+2 end print @sum 7. 编写一个批量,针对Toys表,如果价格大于25元的玩具数量在10种之上,则打印下列消息‘Maybe the price of toys on globaltoyz is not so cheap ’,否则,打印消息‘You can search something on globaltoyz’。(使用IF?ELSE语句) declare @x float,@y int SELECT @x=mtoyrate from toys SELECT @y=sitoyqoh FROM toys if @x>25 and @y>10 print'Maybe the price of toys on globaltoyz is not so cheap ' else print 'You can search something on globaltoyz' 8. 对于Position表,其中可以看到每个职位的人员配备情况,根据Position表中的每个职位的人员缺少数,我们分别打印不同的消息,如果缺少数在20人以上,打印‘奇缺’,在5~20之间,打印‘中度’,如果在5人以下,则打印‘或缺’。(使用搜索的CASE语句)提示:人员缺少数应该根据预算人数(iBudgetedStrength)和当前人数(iCurrentStrength)之差求出来。 declare @x int,@y int SELECT @x=iBudgetedStrength,@y=iCurrentStrength from Position if (@x-@y)>20 print '奇缺' else if (@x-@y)<5 print '或缺' else print '中度' 9. 针对外部候选人表,计算所有人的平均成绩,如果平均分大于80,则将所有人的成绩降低5%,否则将所有人的成绩降低3%。 SELECT AVG(sitestscore) FROM ExternalCandidate)>80 BEGIN UPDATE ExternalCandidate SET sitestscore=sitestscore*0.95 END else BEGIN UPDATE ExternalCandidate SET sitestscore=sitestscore*0.97 END 10. Toys表包含由零售机构出售的所有玩具的材料。Toys表的siToyQOH列存储各玩具的零件数量,cToyId列存储各玩具的Toy Id。在cToyId列上定义Primary Key的约束。当产品代号为000029的玩具的siToyQOH列的值大于0时,使用适当的编程构造显示短语“Product available”。如果存储在siToyQOHd的值为0时,那么显示短语“Product not available”。 ALTER table toys ADD PRIMARY KEY (cToyId) declare @x int SELECT @x=siToyQOH from toys where cToyId='000029' if @x>0 print 'Product available' else print 'Product not available' 11. 查询是否所有的玩具都曾经被人订购,如果是的话,那么打印消息‘All Toys have ever been ordered’。否则,打印消息‘Some kind of toys are not so popular ’,并列出没有被人订购过的玩具的详细信息。 if exists (select * from toys where cToyId not in (select cToyId from OrderDetail ) ) begin print 'All Toys have ever been ordered' end else begin print 'Some kind of toys are not so popular' end select* from Toys where cToyId not in (select cToyId from OrderDetail) 12. 定义一个变量,用于存储Toys表中价格最高的玩具名称。如果最高价格<50元,则打印消息‘price is less than 50’并打印该变量的值,否则打印消息‘The price is more than 50’并打印该变量的值。 declare @highrate float select @highrate=(select top 1 mToyRate from Toys ORDER BY mToyRate DESC ) from Toys if @highrate<50 begin print 'price is less than 50' print @highrate end else begin print 'The price is more than 50' print @highrate end
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库SQL实验6:使用索引,视图和批量在线全文阅读。
相关推荐: