浙江理工大学2013—2013学年第2学期 《数据库原理与应用》期末试卷(A)卷
本人郑重承诺:本人已阅读并且透彻地理解《浙江理工大学考场规则》,愿意在考试中自觉遵守这些规定,保证按规定的程序和要求参加考试,如有违反,自愿按《浙江理工大学学生违纪处分规定》有关条款接受处理。
承诺人签名: 学号: 班级:
一、程序阅读题40% 1 2 3 4 5 6 7 1 2 二、程序设计题60% 附加题 总分 3 4 5 6 7 8
一、程序阅读题(本题共40分)
已知数据表Test1和Test2的定义如下。其数据分别如表1和表2所示(表中未列出的数据需要自己计算得出),写出下列各个小题中SELECT语句的输出结果。
CREATE TABLE test1 (
ProductID char(2) Primary key, Productname varchar(20), Category char(1), Price money,
RowID int identity(20,2) ) GO
CREATE TABLE test2 ( OrderID char(4), OrderDate datetime, Qty int default(0), ProductID char(2),
Primary Key(OrderID, ProductID DESC) ) GO
CREATE VIEW view1 AS
SELECT a.ProductID,Productname,OrderID,OrderDate,Convert(varchar(10),OrderDate,120) as 'OrderDay',Qty*Price as 'Amt' FROM test2 a JOIN test1 b on a.ProductID=b.ProductID GO
1 2012-2013-05075386356-
--第1小题(6分)
CREATE PROCEDURE pro1 @str nvarchar(20) AS
SELECT ProductID,ProductName,OrderID,OrderDay,Amt FROM view1 WHERE Productname like @str and
DATEDIFF(month,OrderDate,GETDATE())<=6 GO
Execute Pro1 '%[^ns]o%' GO --第2小题(6分) ; WITH tmp AS
(SELECT Category,COUNT(*) as Number FROM test1 GROUP BY Category WITH Rollup) SELECT CASE Category WHEN 'R' THEN 'Land' WHEN 'T' THEN 'Fruit' WHEN 'M' THEN 'Wave'
ELSE '*S*' END as 'Category',Number FROM tmp GO 2 2012-2013-05075386356-
--第3小题(6分)
DECLARE @t TABLE (Pid char(2),Qty int)
INSERT INTO @t (Pid,Qty) SELECT ProductID,SUM(qty) FROM test2 GROUP BY ProductID HAVING SUM(qty)>15
SELECT a.Pid,b.Productname as 'Pname',Qty FROM @t a,test1 b WHERE b.ProductID=a.Pid GO --第4小题(6分)
ALTER TABLE test1 Add newCol int GO
UPDATE test1 set newCol=(SELECT SUM(qty) from test2 WHERE ProductID=test1.ProductID ) SELECT * FROM test1 WHERE newCol<(SELECT AVG(newCol) FROM test1) GO 3 2012-2013-05075386356-