一、选择题(1-25没题1分26-30每题2分共35分)
1)假定有一个用户表,表中包含字段:userid (int)、username (varchar)、 password(varchar)、等,该表需要设置主键,以下说法正确的是()。(选择两项)
a)如果不能有同时重复的username和password,那么username和password可以组合在一起作为主键。
b)此表设计主键时,根据选择主键的最小性原则,最好采用userid作为主键。
c)此表设计主键时,根据选择主键的最小性原则,最好采用username和password作为组合键。
d) 如果采用userid作为主键,那么在userid列输入的数值,允许为空。
2)Microsoft SQL Server 2000 在安装的时候就创建的示例用户数据库包括()。(选择两项)
a)Pubs
b)Master
c)Northwind
d)Msdb
3)以下()语句从表TABLE_NAME中提取前10条记录。(选择一项)
a)select * from TABLE_NAME where rowcount=10
b)select TOP 10 * from TABLE_NAME
c)select TOP of 10 * from TABLE_NAME
d)select * from TABLE_NAME where rowcount<=10
4) 从“产品”表里查询出价格高于产品名称为“一次性纸杯”的产品的记录,此SQL语句为()。(选择一项)
a)SELECT * FROM 产品WHERE 价格>‘一次性纸杯’
b)SELECT * FROM 产品WHERE 价格>(SELECT * FROM 产品WHERE 产品名称>’ 一次性纸杯’
c)SELECT * FROM 产品WHERE EXISTS 产品名称=’ 一次性纸杯’
d)SELECT * FROM 产品WHERE 价格>(SELECT 价格FROM 产品WHERE 产品名称=’ 一次性纸杯’
5)查找 student表中所有电话号码(列名:telephone)的第一位为8或6,第三位为0的电话号码()。(选择一项)
a)SELECT telephone FROM student WHERE telephone LIKE '[8,6]%0*'
b)SELECT telephone FROM student WHERE telephone LIKE '(8,6)*0%'
c)SELECT telephone FROM student WHERE telephone LIKE '[8,6]_0%'
d)SELECT telephone FROM student WHERE telephone LIKE '[8,6]_0*'
6)现有表book,字段:id (int),title (varchar), price (float); 其中id字段设为标识, 使用insert语句向book表中插入数据,以下语句错误的是()。(选择一项)
a)insert into book (id,title,price) values(1,'java',100)
b)insert into book (title,price) values('java',100)
c)insert into book values ('java',100)
d)insert book values('java',100)
7)现有表Employee,字段:id (int),firstname(varchar), lastname(varchar); 以下sql语句错误的是()。(选择一项)
a)select firstname+'.'+lastname as 'name' from employee )
b)select firstname+'.'+lastname='name' from employee
c)select 'name'=firstname+'.'+lastname from employee
d)select firstname,lastname from employee
8)在sql server 2000中,关于数据库说法错误的是()。(选择一项)
a)数据库在磁盘上默认的存储位置是:SQL Server安装路径\Microsoft SQL Server\MSSQL\Data
b)一个数据库至少应包含一个数据库文件(*.mdf)和一个事务日志文件(*.ldf)
c)只有当数据库中不存在数据的情况下,才可以进行数据库的收缩操作。
d)可以通过从A机器拷贝数据库文件和事务日志文件至B机器,然后通过在B机器上执行相应的附加数据库操作,实现数据库从A机器到B机器的复制。
9)若要删除book表中所有数据,以下语句错误的是()。(选择两项)
a)truncate table book
b)delete * from book
c)drop table book
d)delete from book
10)学生成绩表grade中有字段score(float),现在要把所有在55分至60之间的分数提高5分,以下sql语句正确的是()。(选择两项)
a)Update grade set score=score+5
b)Update grade set score=score+5 where score>=55 or score <=60
c)Update grade set score=score+5 where score between 55 and 60
d)Update grade set score=score+5 where score >=55 and score <=60
11)现有书目表book,包含字段:price (float); 现在查询一条书价最高的书目的详细信息,以下语句正确的是()。(选择两项)
a)select top 1 * from book order by price asc
b)select top 1 * from book order by price desc
c)select top 1 * from book where price= (select max (price)from book)
d)select top 1 * from book where price= max(price)
12)现有书目表book,包含字段:价格price (float),类别type(char); 现在查询各个类别的平均价格、类别名称,以下语句正确的是()。(选择一项)
a)select avg(price),type from book group by type
b)select count(price),type from book group by price
c)select avg(price),type from book group by price
d)select count (price),type from book group by type
13)查询student表中的所有非空email信息, 以下语句正确的是()。(选择一项)
a)Select email from student where email !=null
b)Select email from student where email not is null
c)Select email from student where email <> null
d)Select email from student where email is not null
14)成绩表grade中字段score代表分数,以下()语句返回成绩表中的最低分。(选择两项)
a)select max(score) from grade
b)select top 1 score from grade order by score asc
c)Select min(score) from grade
d)select top 1 score from grade order by score desc
15)现有订单表orders,包含用户信息userid, 产品信息 productid, 以下()语句能够返回至少被订购过两回的productid? (选择一项)
a)select productid from orders where count(productid)>1
b)select productid from orders where max(productid)>1
c)select productid from orders where having count(productid)>1 group by productid_
d)select productid from orders group by productid having count(productid)>1
16)关于聚合函数,以下说法错误的是()。(选择一项)
a)Sum返回表达式中所有数的总合,因此只能用于数字类型的列。
b)Avg返回表达式中所有数的平均值,可以用于数字型和日期型的列。
c)Max和Min可以用于字符型的列。
d)Count可以用于字符型的列。
17)使用以下()不可以进行模糊查询。(选择一项)
a)OR
b)Not between
c)Not IN
d)Like
18)关于多表联接查询,以下()描述是错误的。(选择一项)
a)外联接查询返回的结果集行数可能大于所有符合联接条件的结果集行数。
b)多表联接查询必须使用到JOIN关键字
c)内联接查询返回的结果是:所有符合联接条件的数据。
d)在where子句中指定联接条件可以实现内联接查询。
19)Sql语句:select * from students where SNO like ‘010[^0]%[A,B,C]%’,可能会查询出的SNO是()。(选择两项)
a)01053090A #Hm3?
b)01003090A01
c)01053090D09
d)0101A01
20)关于Truncate table, 以下()描述是错误的。(选择两项)
a)Truncate table 可跟Where从句,根据条件进行删除。
b)Truncate table 用来删除表中所有数据。
c)触发器对Truncate table无效。
d)delete 比Truncate table速度快。
21)创建一个名为‘Customers’的新表,同时要求新表中包含表‘clients’的所有记录,sql语句是()。(选择一项)
a)Select * into customers from clients
b)Select into customers from clients
c)Insert into customers select * from clients
d)Insert customers select * from clients
22)关于主键,以下()说法是错误的。(选择两项)
a)主键可以用来确保表中不存在重复的数据行。
b)一个表必须有一个主键。
c)一个表只能有一个主键。
d)只能对整数型列设置主键。
23)假设订单表orders用来存储订单信息,cid代表客户编号,money代表单次订购额,现要查询每个客户的订购次数和每个客户的订购总金额,下面()sql语句可以返回正确结果。(选择一项)
a)select cid,count(distinct(cid)),sum(money) from orders group by cid
b)select cid,count(distinct(cid)),sum(money) from orders order by cid
c)select cid,count(cid),sum(money) from orders order by cid
d)select cid,count(cid),sum(money) from orders group by cid
24)现有客户表customers(主键:客户编号cid),包含10行数据,订单表orders(外键:客户编号cid),包含6条数据。执行sql语句:select * from customers right outer join orders on customers.cid=orders.cid。最多返回()条记录。(选择一项)
a)10
b)6
c)4
d)0
25)以下描述不正确的是
a)存储过程能够实现较快的执行速度。
b)内嵌表值型函数相当于一个带参数的视图。
c)不指定所有者时,调用标量函数会出错。
d)可以通过视图更改任意基表
26)Your database stores telephone numbers. Each telephone number is stored as an integer. You must format the telephone number to print on a report in the following format:
(999) 999-9999
You have selected the phone number into a local variable as follows:
DECLARE @PhoneNumber int Which statement will correctly format the number?
a)SELECT 'Phone Number' = ‘(‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),3,0) + ‘)‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNurnber),3,3)+ ‘-‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),4,6)
b)SELECT 'Phone Number' = ‘(‘ + SUBSTRING(CONVERT(varchar(10),@PhoneNuwber),3,1) + ‘)‘ + SUBSTRING(CONVERT(varcher(10), @PhoneNumber),3,4)+ ‘-‘ + SUBSTRING(CONVERT(varchar(10), @PhoneMumber),4,7)
c)SELECT 'Phone Number' = ‘(‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),0,3) + ‘)‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),3,3)+ ‘-‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNurtiber),6,4)
d)SELECT 'Phone Number' = ‘(‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),1,3) + ‘)‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),4,3)+ '-' + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),7,4)
27)You are a database developer for Wide World Importers. You are creating a database that will store order information. Orders will be entered in a client/server application. Each time a new order is entered, a unique order number must be assigned. Order numbers must be assigned in ascending order. An average of 10,000 orders will be entered each day. You create a new table named Orders and add an OrderNumber column to this table. What should you do next?
a)Set the data type of the column to UniqueIdentifier.
b)Set the data type of the column to int, and set the IDENTITY property for the column.
c)Set the data type of the column to int. Create a user-defined function that selects the maximum order number in the table.
d)Set the data type of the column to int. Create a NextKey table, and add a NextOrder column to the table. Set the data type of the NextOrder column to int. Create a stored procedure to retrieve and update the value held in the NextKey.
28)You are creating a script that will execute this stored procedure. If the stored procedure executes successfully, it should report the year-to-date sales for the book title. If the stored procedure fails to execute, it should report the following message:
“No Sales Found”
How should you create the script?
a)DECLARE @retval int
DECLARE @ytd int
EXEC get_sales_for_title ‘Net Etiquette’, @ytd
IF @retval < 0
PRINT ‘No sales found’
ELSE
PRINT ‘Year to date sales: ’ + STR (@ytd)
GO
b)DECLARE @retval int
DECLARE @ytd int
EXEC get_sales_for_title ‘Net Etiquette’, @ytd OUTPUT
IF @retval < 0
PRINT ‘No sales found’
ELSE
PRINT ‘Year to date sales: ’ + STR (@ytd)
GO
c)DECLARE @retval int
DECLARE @ytd int
EXEC get_sales_for_title ‘Net Etiquette’,@retval OUTPUT
IF @retval < 0
PRINT ‘No sales found’
ELSE
PRINT ‘Year to date sales: ’ + STR (@ytd)
GO
d)DECLARE @retval int
DECLARE @ytd int
EXEC @retval = get_sales_for_title ‘Net Etiquette’, @ytd
OUTPUT
IF @retval < 0
PRINT ‘No sales found’
ELSE
PRINT ‘Year to date sales: ’ + STR (@ytd)
GO
29)You are a database developer for an insurance company. Information about the company's insurance policies is stored in a SQL Server 2000 database. You create a table named policy for this database by using the script shown below:
CREATE TABLE Policy
(
PolicyNumber int NOT NULL DEFAULT (0),
InsuredLastName CHAR (30) NOT NULL,
InsuredFirstName CHAR (20) NOT NULL,
InsuredBirthDate dattime NOT NULL,
PolicyDate datetime NOT NULL,
FaceAmount money NOT NULL,
CONSTRAINT PK_Policy PRIMARY KEY (PolicyNumber)
)
Each time the company sells a new policy, the policy must be assigned a unique policy number. The database must assign a new policy number when a new policy is entered. What should you do?
a)Create an INSTEAD OF INSERT trigger to generate a new policy number, and include the policy number in the data instead into the table.
b)Create an INSTEAD OF UPDATE trigger to generate a new policy number, and include the policy number in the data inserted into the table.
c)Create an AFTER UPDATE trigger to generate a new policy number, and include the policy number in the data inserted into the table.
d)Replace the DEFAULT constraint with a AFTER INSERT trigger that generates a new policy number and includes the policy number in the data inserted into the table.
30)You are a database developer for a marketing firm. You have designed a quarterly sales view. This view joins several tables and calculates aggregate information. You create a view. You want to provide a parameterised query to access the data contained in your view. The output will be used in other SELECT lists. How should you accomplish this goal?
a)Use an ALTER VIEW statement to add the parameter value to the view definition.
b)Create a stored procedure that accepts the parameter as input and returns a rowset with the result set.
c)Create a scalar user-defined function that accepts the parameter as input.
d)Create an inline user-defined function that accepts the parameter as input.
二、填空题(每空一分共25分)
1、关系R是1NF,当且仅当所有的基础域仅包含________。
2、主关键字应该具有________性,__________性,__________性。
3、关系R是3NF,当且仅当R是2NF,并且所有非PK属性都是__________于PK。
4、Transact-SQL 语言包含_________语言,_________语言,_________语言。
5、消除列重复的关键字是_________,使用_________操作符从多个查询中创建单结果集,提交一个事务的语句为________ 。
6、SQLServer的两种授权模式分别为___________和_____________。
7、有一个数据表其一行有5KB,有10000行数据,那么我们至少需要______M的数据库空间。
8、数据完整性分为__________,__________,__________.
9、每个允许有_______个簇索引,___________索引是SQLServer默认选择。
10、可以使用________关键字显示未加密存储过程信息,执行存储过程的关键字是_________,统计列平均值的聚合函数为________,修改对象的关键字为_______,删除对象的关键字为_________。
11、________数据库保存所有的临时表和临时存储过程。
三、简答题(15分)
1、请简述一下第二范式(3分)
2、请简述UPDATE 触发器工作原理(3分)
3、强制引用完整性时,简述SQL Server 禁止用户进行的操作(3分)
4、简述相关子查询的步骤(3分)
5、简述使用索引和不使用索引的理由(3分)
上机题(25分)
一、(12分)问题描述:
已知关系模式:
S (SNO,SNAME) 学生关系。SNO 为学号,SNAME 为姓名
C (CNO,CNAME,CTEACHER) 课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师
SC(SNO,CNO,SCGRADE) 选课关系。SCGRADE 为成绩
1. 找出没有选修过“李明”老师讲授课程的所有学生姓名(4分)
2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩(4分)
3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名(4分)
二、定义一个十进制转换成2进制的标量函数。(4分)
三、活期存款中,“储户”通过“存取款单”和“储蓄所”发生联系。假定储户包括:账号,姓名,电话,地址,存款额;“储蓄所”包括:储蓄所编号,名称,电话,地址(假定一个储户可以在不同得储蓄所存取款)
1、写出设计以上表格的语句(4分)
2、创建一个触发器TR1完成下面内容:
当向“存取款单”表中插入数据时,如果存取标志=1则应该更改储户表让存款额加上存取金额,如果存取标志=0则应该更改储户表让存款额减去存取金额,如果余额不足显示余额不足错误。(5分)
答案:
上
一、填空题
1)a,b 2)a,c 3)b 4)d 5)c 6)a 7)b 8)c 9)b,c 10)c,d 11)b,c 12)a 13)d 14)c,d 15)d 16)b 17)d 18)b 19)a,d 20)a,d 21)a 22)a,c 23)d 24)b 25)d 26)d 27)b 28)d 29)a 30)d
二、填空题
1、原子值
2、稳定性,最简性,熟悉性
3、非传递地依赖
4、数据定义,数据控制,数据操纵
5、distinct,UNION,COMMIT TRANSACTION
6、NT only验证模式,混合模式
7、80
8、域完整性,实体完整性,参考完整性
9、一,非簇
10、sp_help,EXEC,AVG,ALTER,DROP
11、tempdb
下
一、
准备数据
create table s(sno int,sName varchar(100))
create table c(cno int,cName varchar(100),CTEACHER varchar(100))
create table sc(sno int,cno int,scGrade int)
insert into s
values(1,'lfm1')
insert into s
values(2,'lfm2')
insert into s
values(3,'lfm3')
insert into s
values(4,'lfm4')
insert into s
values(5,'lfm5')
insert into s
values(6,'lfm6')
insert into c
values(1,'ch1','李明')
insert into c
values(2,'ch2','王刚')
insert into c
values(3,'ch3','ll')
insert into c
values(4,'ch4','ff')
insert into c
values(5,'ch5','ffd')
insert into c
values(6,'ch6','se')
insert into c
values(7,'ch7','s')
insert into sc
values(1,2,80)
insert into sc
values(2,2,50)
insert into sc
values(1,1,60)
insert into sc
values(1,3,90)
insert into sc
values(3,2,55)
insert into sc
values(1,6,77)
insert into sc
values(3,4,80)
insert into sc
values(4,2,70)
1. 找出没有选修过“李明”老师讲授课程的所有学生姓名
--实现代码:
SELECT SNAME FROM S
WHERE NOT EXISTS(
SELECT * FROM SC,C
WHERE SC.CNO=C.CNO
AND CNAME='李明'
AND SC.SNO=S.SNO)
select sname from s
where sno not in
(select s.sno from s
inner join sc
on sc.sno=s.sno
inner join c
on c.cno=sc.cno
where cteacher='李明')
2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
--实现代码:
SELECT S.SNO,S.SNAME,AVG_SCGRADE=AVG(SC.SCGRADE)
FROM S,SC,(
SELECT SNO
FROM SC
WHERE SCGRADE<60
GROUP BY SNO
HAVING COUNT(DISTINCT CNO)>=2
)A WHERE S.SNO=A.SNO AND SC.SNO=A.SNO
GROUP BY S.SNO,S.SNAME
select s.sno,avg(scGrade) from s,sc
where s.sno in(
select sc.sno from sc
inner join c
on c.cno=sc.cno
where sc.scGrade<60
group by sno
having count(*)>=2)
and s.sno=sc.sno
group by s.sno
3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
--实现代码:
SELECT S.SNO,S.SNAME
FROM S,(
SELECT SC.SNO
FROM SC,C
WHERE SC.CNO=C.CNO
AND C.CNO IN('1','2')
GROUP BY SNO
HAVING COUNT(DISTINCT c.CNO)=2
)SC WHERE S.SNO=SC.SNO
select S.SNO,S.SNAME from sc,s
where cno=1 and sc.sno in(select sno from sc where cno=2) and s.sno=sc.sno
二、
create function convert2( @num int)
returns varchar(100)
as
begin
declare @re varchar(100)
set @re=''
while @num>0
select @re=substring('01',@num%2+1,1)+@re
,@num=@num/2
return @re
end
select dbo.convert2(8)
三、
1、
CREATE TABLE 储户
(
账号 INT PRIMARY KEY ,姓名 CHAR(10),电话 INT ,地址 CHAR(10),存款额MONEY
)
GO
CREATE TABLE 储蓄所
(
储蓄所编号 INT PRIMARY KEY,名称 CHAR(10),电话 INT,地址 CHAR(10)
)
GO
CREATE TABLE 存取款单
(
账号 INT NOT NULL,储蓄所编号 INT NOT NULL,存取日期 DATETIME NOT NULL,
存取标志 INT NOT NULL,存取金额 MONEY
)
GO
2、
CREATE TRIGGER TR1
ON 存取款单
FOR INSERT
AS
DECLARE @BZ INT,@MONEY MONEY,@ZH INT
SELECT @BZ=存取标志,@MONEY=存取金额,@ZH=账号
FROM INSERTED
IF @BZ=0
BEGIN
UPDATE 储户
SET 存款额=存款额-@MONEY
WHERE 账号=@ZH
declare @sy money
select @sy=存款额 from 储户
WHERE 账号=@ZH
if @sy<0
begin
raiserror ('余额不足',16,1)
rollback
end
END
IF @BZ=1
BEGIN
UPDATE 储户
SET 存款额=存款额+@MONEY
WHERE 账号=@ZH
END
1)假定有一个用户表,表中包含字段:userid (int)、username (varchar)、 password(varchar)、等,该表需要设置主键,以下说法正确的是()。(选择两项)
a)如果不能有同时重复的username和password,那么username和password可以组合在一起作为主键。
b)此表设计主键时,根据选择主键的最小性原则,最好采用userid作为主键。
c)此表设计主键时,根据选择主键的最小性原则,最好采用username和password作为组合键。
d) 如果采用userid作为主键,那么在userid列输入的数值,允许为空。
2)Microsoft SQL Server 2000 在安装的时候就创建的示例用户数据库包括()。(选择两项)
a)Pubs
b)Master
c)Northwind
d)Msdb
3)以下()语句从表TABLE_NAME中提取前10条记录。(选择一项)
a)select * from TABLE_NAME where rowcount=10
b)select TOP 10 * from TABLE_NAME
c)select TOP of 10 * from TABLE_NAME
d)select * from TABLE_NAME where rowcount<=10
4) 从“产品”表里查询出价格高于产品名称为“一次性纸杯”的产品的记录,此SQL语句为()。(选择一项)
a)SELECT * FROM 产品WHERE 价格>‘一次性纸杯’
b)SELECT * FROM 产品WHERE 价格>(SELECT * FROM 产品WHERE 产品名称>’ 一次性纸杯’
c)SELECT * FROM 产品WHERE EXISTS 产品名称=’ 一次性纸杯’
d)SELECT * FROM 产品WHERE 价格>(SELECT 价格FROM 产品WHERE 产品名称=’ 一次性纸杯’
5)查找 student表中所有电话号码(列名:telephone)的第一位为8或6,第三位为0的电话号码()。(选择一项)
a)SELECT telephone FROM student WHERE telephone LIKE '[8,6]%0*'
b)SELECT telephone FROM student WHERE telephone LIKE '(8,6)*0%'
c)SELECT telephone FROM student WHERE telephone LIKE '[8,6]_0%'
d)SELECT telephone FROM student WHERE telephone LIKE '[8,6]_0*'
6)现有表book,字段:id (int),title (varchar), price (float); 其中id字段设为标识, 使用insert语句向book表中插入数据,以下语句错误的是()。(选择一项)
a)insert into book (id,title,price) values(1,'java',100)
b)insert into book (title,price) values('java',100)
c)insert into book values ('java',100)
d)insert book values('java',100)
7)现有表Employee,字段:id (int),firstname(varchar), lastname(varchar); 以下sql语句错误的是()。(选择一项)
a)select firstname+'.'+lastname as 'name' from employee )
b)select firstname+'.'+lastname='name' from employee
c)select 'name'=firstname+'.'+lastname from employee
d)select firstname,lastname from employee
8)在sql server 2000中,关于数据库说法错误的是()。(选择一项)
a)数据库在磁盘上默认的存储位置是:SQL Server安装路径\Microsoft SQL Server\MSSQL\Data
b)一个数据库至少应包含一个数据库文件(*.mdf)和一个事务日志文件(*.ldf)
c)只有当数据库中不存在数据的情况下,才可以进行数据库的收缩操作。
d)可以通过从A机器拷贝数据库文件和事务日志文件至B机器,然后通过在B机器上执行相应的附加数据库操作,实现数据库从A机器到B机器的复制。
9)若要删除book表中所有数据,以下语句错误的是()。(选择两项)
a)truncate table book
b)delete * from book
c)drop table book
d)delete from book
10)学生成绩表grade中有字段score(float),现在要把所有在55分至60之间的分数提高5分,以下sql语句正确的是()。(选择两项)
a)Update grade set score=score+5
b)Update grade set score=score+5 where score>=55 or score <=60
c)Update grade set score=score+5 where score between 55 and 60
d)Update grade set score=score+5 where score >=55 and score <=60
11)现有书目表book,包含字段:price (float); 现在查询一条书价最高的书目的详细信息,以下语句正确的是()。(选择两项)
a)select top 1 * from book order by price asc
b)select top 1 * from book order by price desc
c)select top 1 * from book where price= (select max (price)from book)
d)select top 1 * from book where price= max(price)
12)现有书目表book,包含字段:价格price (float),类别type(char); 现在查询各个类别的平均价格、类别名称,以下语句正确的是()。(选择一项)
a)select avg(price),type from book group by type
b)select count(price),type from book group by price
c)select avg(price),type from book group by price
d)select count (price),type from book group by type
13)查询student表中的所有非空email信息, 以下语句正确的是()。(选择一项)
a)Select email from student where email !=null
b)Select email from student where email not is null
c)Select email from student where email <> null
d)Select email from student where email is not null
14)成绩表grade中字段score代表分数,以下()语句返回成绩表中的最低分。(选择两项)
a)select max(score) from grade
b)select top 1 score from grade order by score asc
c)Select min(score) from grade
d)select top 1 score from grade order by score desc
15)现有订单表orders,包含用户信息userid, 产品信息 productid, 以下()语句能够返回至少被订购过两回的productid? (选择一项)
a)select productid from orders where count(productid)>1
b)select productid from orders where max(productid)>1
c)select productid from orders where having count(productid)>1 group by productid_
d)select productid from orders group by productid having count(productid)>1
16)关于聚合函数,以下说法错误的是()。(选择一项)
a)Sum返回表达式中所有数的总合,因此只能用于数字类型的列。
b)Avg返回表达式中所有数的平均值,可以用于数字型和日期型的列。
c)Max和Min可以用于字符型的列。
d)Count可以用于字符型的列。
17)使用以下()不可以进行模糊查询。(选择一项)
a)OR
b)Not between
c)Not IN
d)Like
18)关于多表联接查询,以下()描述是错误的。(选择一项)
a)外联接查询返回的结果集行数可能大于所有符合联接条件的结果集行数。
b)多表联接查询必须使用到JOIN关键字
c)内联接查询返回的结果是:所有符合联接条件的数据。
d)在where子句中指定联接条件可以实现内联接查询。
19)Sql语句:select * from students where SNO like ‘010[^0]%[A,B,C]%’,可能会查询出的SNO是()。(选择两项)
a)01053090A #Hm3?
b)01003090A01
c)01053090D09
d)0101A01
20)关于Truncate table, 以下()描述是错误的。(选择两项)
a)Truncate table 可跟Where从句,根据条件进行删除。
b)Truncate table 用来删除表中所有数据。
c)触发器对Truncate table无效。
d)delete 比Truncate table速度快。
21)创建一个名为‘Customers’的新表,同时要求新表中包含表‘clients’的所有记录,sql语句是()。(选择一项)
a)Select * into customers from clients
b)Select into customers from clients
c)Insert into customers select * from clients
d)Insert customers select * from clients
22)关于主键,以下()说法是错误的。(选择两项)
a)主键可以用来确保表中不存在重复的数据行。
b)一个表必须有一个主键。
c)一个表只能有一个主键。
d)只能对整数型列设置主键。
23)假设订单表orders用来存储订单信息,cid代表客户编号,money代表单次订购额,现要查询每个客户的订购次数和每个客户的订购总金额,下面()sql语句可以返回正确结果。(选择一项)
a)select cid,count(distinct(cid)),sum(money) from orders group by cid
b)select cid,count(distinct(cid)),sum(money) from orders order by cid
c)select cid,count(cid),sum(money) from orders order by cid
d)select cid,count(cid),sum(money) from orders group by cid
24)现有客户表customers(主键:客户编号cid),包含10行数据,订单表orders(外键:客户编号cid),包含6条数据。执行sql语句:select * from customers right outer join orders on customers.cid=orders.cid。最多返回()条记录。(选择一项)
a)10
b)6
c)4
d)0
25)以下描述不正确的是
a)存储过程能够实现较快的执行速度。
b)内嵌表值型函数相当于一个带参数的视图。
c)不指定所有者时,调用标量函数会出错。
d)可以通过视图更改任意基表
26)Your database stores telephone numbers. Each telephone number is stored as an integer. You must format the telephone number to print on a report in the following format:
(999) 999-9999
You have selected the phone number into a local variable as follows:
DECLARE @PhoneNumber int Which statement will correctly format the number?
a)SELECT 'Phone Number' = ‘(‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),3,0) + ‘)‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNurnber),3,3)+ ‘-‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),4,6)
b)SELECT 'Phone Number' = ‘(‘ + SUBSTRING(CONVERT(varchar(10),@PhoneNuwber),3,1) + ‘)‘ + SUBSTRING(CONVERT(varcher(10), @PhoneNumber),3,4)+ ‘-‘ + SUBSTRING(CONVERT(varchar(10), @PhoneMumber),4,7)
c)SELECT 'Phone Number' = ‘(‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),0,3) + ‘)‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),3,3)+ ‘-‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNurtiber),6,4)
d)SELECT 'Phone Number' = ‘(‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),1,3) + ‘)‘ + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),4,3)+ '-' + SUBSTRING(CONVERT(varchar(10), @PhoneNumber),7,4)
27)You are a database developer for Wide World Importers. You are creating a database that will store order information. Orders will be entered in a client/server application. Each time a new order is entered, a unique order number must be assigned. Order numbers must be assigned in ascending order. An average of 10,000 orders will be entered each day. You create a new table named Orders and add an OrderNumber column to this table. What should you do next?
a)Set the data type of the column to UniqueIdentifier.
b)Set the data type of the column to int, and set the IDENTITY property for the column.
c)Set the data type of the column to int. Create a user-defined function that selects the maximum order number in the table.
d)Set the data type of the column to int. Create a NextKey table, and add a NextOrder column to the table. Set the data type of the NextOrder column to int. Create a stored procedure to retrieve and update the value held in the NextKey.
28)You are creating a script that will execute this stored procedure. If the stored procedure executes successfully, it should report the year-to-date sales for the book title. If the stored procedure fails to execute, it should report the following message:
“No Sales Found”
How should you create the script?
a)DECLARE @retval int
DECLARE @ytd int
EXEC get_sales_for_title ‘Net Etiquette’, @ytd
IF @retval < 0
PRINT ‘No sales found’
ELSE
PRINT ‘Year to date sales: ’ + STR (@ytd)
GO
b)DECLARE @retval int
DECLARE @ytd int
EXEC get_sales_for_title ‘Net Etiquette’, @ytd OUTPUT
IF @retval < 0
PRINT ‘No sales found’
ELSE
PRINT ‘Year to date sales: ’ + STR (@ytd)
GO
c)DECLARE @retval int
DECLARE @ytd int
EXEC get_sales_for_title ‘Net Etiquette’,@retval OUTPUT
IF @retval < 0
PRINT ‘No sales found’
ELSE
PRINT ‘Year to date sales: ’ + STR (@ytd)
GO
d)DECLARE @retval int
DECLARE @ytd int
EXEC @retval = get_sales_for_title ‘Net Etiquette’, @ytd
OUTPUT
IF @retval < 0
PRINT ‘No sales found’
ELSE
PRINT ‘Year to date sales: ’ + STR (@ytd)
GO
29)You are a database developer for an insurance company. Information about the company's insurance policies is stored in a SQL Server 2000 database. You create a table named policy for this database by using the script shown below:
CREATE TABLE Policy
(
PolicyNumber int NOT NULL DEFAULT (0),
InsuredLastName CHAR (30) NOT NULL,
InsuredFirstName CHAR (20) NOT NULL,
InsuredBirthDate dattime NOT NULL,
PolicyDate datetime NOT NULL,
FaceAmount money NOT NULL,
CONSTRAINT PK_Policy PRIMARY KEY (PolicyNumber)
)
Each time the company sells a new policy, the policy must be assigned a unique policy number. The database must assign a new policy number when a new policy is entered. What should you do?
a)Create an INSTEAD OF INSERT trigger to generate a new policy number, and include the policy number in the data instead into the table.
b)Create an INSTEAD OF UPDATE trigger to generate a new policy number, and include the policy number in the data inserted into the table.
c)Create an AFTER UPDATE trigger to generate a new policy number, and include the policy number in the data inserted into the table.
d)Replace the DEFAULT constraint with a AFTER INSERT trigger that generates a new policy number and includes the policy number in the data inserted into the table.
30)You are a database developer for a marketing firm. You have designed a quarterly sales view. This view joins several tables and calculates aggregate information. You create a view. You want to provide a parameterised query to access the data contained in your view. The output will be used in other SELECT lists. How should you accomplish this goal?
a)Use an ALTER VIEW statement to add the parameter value to the view definition.
b)Create a stored procedure that accepts the parameter as input and returns a rowset with the result set.
c)Create a scalar user-defined function that accepts the parameter as input.
d)Create an inline user-defined function that accepts the parameter as input.
二、填空题(每空一分共25分)
1、关系R是1NF,当且仅当所有的基础域仅包含________。
2、主关键字应该具有________性,__________性,__________性。
3、关系R是3NF,当且仅当R是2NF,并且所有非PK属性都是__________于PK。
4、Transact-SQL 语言包含_________语言,_________语言,_________语言。
5、消除列重复的关键字是_________,使用_________操作符从多个查询中创建单结果集,提交一个事务的语句为________ 。
6、SQLServer的两种授权模式分别为___________和_____________。
7、有一个数据表其一行有5KB,有10000行数据,那么我们至少需要______M的数据库空间。
8、数据完整性分为__________,__________,__________.
9、每个允许有_______个簇索引,___________索引是SQLServer默认选择。
10、可以使用________关键字显示未加密存储过程信息,执行存储过程的关键字是_________,统计列平均值的聚合函数为________,修改对象的关键字为_______,删除对象的关键字为_________。
11、________数据库保存所有的临时表和临时存储过程。
三、简答题(15分)
1、请简述一下第二范式(3分)
2、请简述UPDATE 触发器工作原理(3分)
3、强制引用完整性时,简述SQL Server 禁止用户进行的操作(3分)
4、简述相关子查询的步骤(3分)
5、简述使用索引和不使用索引的理由(3分)
上机题(25分)
一、(12分)问题描述:
已知关系模式:
S (SNO,SNAME) 学生关系。SNO 为学号,SNAME 为姓名
C (CNO,CNAME,CTEACHER) 课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师
SC(SNO,CNO,SCGRADE) 选课关系。SCGRADE 为成绩
1. 找出没有选修过“李明”老师讲授课程的所有学生姓名(4分)
2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩(4分)
3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名(4分)
二、定义一个十进制转换成2进制的标量函数。(4分)
三、活期存款中,“储户”通过“存取款单”和“储蓄所”发生联系。假定储户包括:账号,姓名,电话,地址,存款额;“储蓄所”包括:储蓄所编号,名称,电话,地址(假定一个储户可以在不同得储蓄所存取款)
1、写出设计以上表格的语句(4分)
2、创建一个触发器TR1完成下面内容:
当向“存取款单”表中插入数据时,如果存取标志=1则应该更改储户表让存款额加上存取金额,如果存取标志=0则应该更改储户表让存款额减去存取金额,如果余额不足显示余额不足错误。(5分)
答案:
上
一、填空题
1)a,b 2)a,c 3)b 4)d 5)c 6)a 7)b 8)c 9)b,c 10)c,d 11)b,c 12)a 13)d 14)c,d 15)d 16)b 17)d 18)b 19)a,d 20)a,d 21)a 22)a,c 23)d 24)b 25)d 26)d 27)b 28)d 29)a 30)d
二、填空题
1、原子值
2、稳定性,最简性,熟悉性
3、非传递地依赖
4、数据定义,数据控制,数据操纵
5、distinct,UNION,COMMIT TRANSACTION
6、NT only验证模式,混合模式
7、80
8、域完整性,实体完整性,参考完整性
9、一,非簇
10、sp_help,EXEC,AVG,ALTER,DROP
11、tempdb
下
一、
准备数据
create table s(sno int,sName varchar(100))
create table c(cno int,cName varchar(100),CTEACHER varchar(100))
create table sc(sno int,cno int,scGrade int)
insert into s
values(1,'lfm1')
insert into s
values(2,'lfm2')
insert into s
values(3,'lfm3')
insert into s
values(4,'lfm4')
insert into s
values(5,'lfm5')
insert into s
values(6,'lfm6')
insert into c
values(1,'ch1','李明')
insert into c
values(2,'ch2','王刚')
insert into c
values(3,'ch3','ll')
insert into c
values(4,'ch4','ff')
insert into c
values(5,'ch5','ffd')
insert into c
values(6,'ch6','se')
insert into c
values(7,'ch7','s')
insert into sc
values(1,2,80)
insert into sc
values(2,2,50)
insert into sc
values(1,1,60)
insert into sc
values(1,3,90)
insert into sc
values(3,2,55)
insert into sc
values(1,6,77)
insert into sc
values(3,4,80)
insert into sc
values(4,2,70)
1. 找出没有选修过“李明”老师讲授课程的所有学生姓名
--实现代码:
SELECT SNAME FROM S
WHERE NOT EXISTS(
SELECT * FROM SC,C
WHERE SC.CNO=C.CNO
AND CNAME='李明'
AND SC.SNO=S.SNO)
select sname from s
where sno not in
(select s.sno from s
inner join sc
on sc.sno=s.sno
inner join c
on c.cno=sc.cno
where cteacher='李明')
2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
--实现代码:
SELECT S.SNO,S.SNAME,AVG_SCGRADE=AVG(SC.SCGRADE)
FROM S,SC,(
SELECT SNO
FROM SC
WHERE SCGRADE<60
GROUP BY SNO
HAVING COUNT(DISTINCT CNO)>=2
)A WHERE S.SNO=A.SNO AND SC.SNO=A.SNO
GROUP BY S.SNO,S.SNAME
select s.sno,avg(scGrade) from s,sc
where s.sno in(
select sc.sno from sc
inner join c
on c.cno=sc.cno
where sc.scGrade<60
group by sno
having count(*)>=2)
and s.sno=sc.sno
group by s.sno
3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
--实现代码:
SELECT S.SNO,S.SNAME
FROM S,(
SELECT SC.SNO
FROM SC,C
WHERE SC.CNO=C.CNO
AND C.CNO IN('1','2')
GROUP BY SNO
HAVING COUNT(DISTINCT c.CNO)=2
)SC WHERE S.SNO=SC.SNO
select S.SNO,S.SNAME from sc,s
where cno=1 and sc.sno in(select sno from sc where cno=2) and s.sno=sc.sno
二、
create function convert2( @num int)
returns varchar(100)
as
begin
declare @re varchar(100)
set @re=''
while @num>0
select @re=substring('01',@num%2+1,1)+@re
,@num=@num/2
return @re
end
select dbo.convert2(8)
三、
1、
CREATE TABLE 储户
(
账号 INT PRIMARY KEY ,姓名 CHAR(10),电话 INT ,地址 CHAR(10),存款额MONEY
)
GO
CREATE TABLE 储蓄所
(
储蓄所编号 INT PRIMARY KEY,名称 CHAR(10),电话 INT,地址 CHAR(10)
)
GO
CREATE TABLE 存取款单
(
账号 INT NOT NULL,储蓄所编号 INT NOT NULL,存取日期 DATETIME NOT NULL,
存取标志 INT NOT NULL,存取金额 MONEY
)
GO
2、
CREATE TRIGGER TR1
ON 存取款单
FOR INSERT
AS
DECLARE @BZ INT,@MONEY MONEY,@ZH INT
SELECT @BZ=存取标志,@MONEY=存取金额,@ZH=账号
FROM INSERTED
IF @BZ=0
BEGIN
UPDATE 储户
SET 存款额=存款额-@MONEY
WHERE 账号=@ZH
declare @sy money
select @sy=存款额 from 储户
WHERE 账号=@ZH
if @sy<0
begin
raiserror ('余额不足',16,1)
rollback
end
END
IF @BZ=1
BEGIN
UPDATE 储户
SET 存款额=存款额+@MONEY
WHERE 账号=@ZH
END