当前位置:文档之家› sqlServer2005习题与答案

sqlServer2005习题与答案

期末复习题

一选择题

1 SQL语言允许使用通配符进行字符串匹配的*作,其中‘%’可以表示(C )

A 零个字符

B 1个字符串

C 多个字符串

D 以上都是

2 如何在已经创建好的表上添加一个外键(A )

A Alter table 表名add foreign key(键名)reference 关联表(关联键名)

B Alter table 表名add Primary key(键名)reference 关联表(关联键名)

C Alter 表名add foreign key(键名)reference 关联键名)

D Alter 表名add Primary key(键名)reference 关联表(关联键名)

3 要修改表名为Table1的字段Field1长度,原为Char(10)要求用SQL增加长度为char (20),以下语句那个正确( C )

A Alter table Table1 Alter Field1 char(20)

B Alter Table1 Alter column Field1 char(20)

C Alter table Table1 Alter column Field1 char(20)

D Alter column Field1 char(20)

4 下面聚集函数中哪个只能用于计算数值类型的数据(D )

A COUNT( )

B MIN( )

C MAX( )

D SUM( )

5 以下哪个关键字用来定义记录在某属性上的约束条件(D )

A DEFAULT

B DISTINCT

C UNIQUE

D CHECK

6 欲往表中增加一条记录,应该用哪一条SQL语句(B )

A ALTER TABLE

B INSERT INTO TABLE

C CREATE TABLE

D DROP TABLE

7 以下哪一个不是逻辑运算符(D )

A NOT

B AND

C OR

D IN

8 在创建表的过程中,哪个关键字用来定义默认值(D )

A DISTINCT

B UNIQUE

C CHECK

D DEFAULT

9 以下哪些描述符合数据库阶段的特点(C )

1) 文件的组织形式可以是顺序的,也可以是随机的

2)数据结构化

3)数据可以共享,但冗余大

4)程序独立于数据

5)统一的数据管理功能

6)数据面向应用

7)数据不保存

A 1) 2) 7)

B 2) 3) 4) 5)

C 2) 4) 5)

D 2) 3) 5) 6)

二填空题

1 数据库技术经历了人工管理阶段,_________和_______三个发展阶段。

2 数据库系统中最常用的三种数据模型有层次模型,_________和_______。

3 关系模式的三类完整性约束条件分别是_____,_____,和______。

4 E-R图的主要元素是实体型,______,______。

5 在E-R模型的主要术语中,属性的取值范围称为_______。能够区别不同的实体且能唯一标识实体的最小属性集,称为______。

填空题答案:

1文件系统阶段,数据系统阶段

2网状模型,关系模型

3实体完整性,引用完整性约束,用户自定义完整性

4属性,联系

5域,实体集的实体键

三判断题

1 默认值绑定到列上后,该列上的值将固定不能改变(X )

2 一张表允许有多个主键,但只能有一个外键(X )

3 可以通过SQL语句修改数据库名称及表的名称(√)

4 聚集函数SUM( )与A VG( )可以用来操作任何类型的数据(X )

5 要删除一条记录使用DROP指令(X )

6 在空串处理语句中,IS NULL用来判断属性值是否为空,在此IS是关键字,不是标准的逻辑运算符(√)

7 在模糊查询中可以使用LIKE结合通配符一块儿实现,其中%代表单个字符(X )

8 在查询语句中,若要把记录进行降序排列应使用ORDER BY 字段名DESC (√)

9 在创建表的过程中,如果一属性是DATETIME类型需要指定长度(X )

10 在关系数据库的设计过程中,允许在设计后对表结构进行修改(√)

四基本操作

现在要为学校建立一个数据库,设学校的环境如下:一个系有若干个专业,每个专业一年只招一个班,每个班有若干个学生,现要建立关于系、学生、班级的数据库,关系模式为:

学生表STUDENT(学号,姓名,性别,生源地,出生日期)

班级表CLASS(班号,专业名,系号,入学年份)

系DEPARTMENT(系号,系名)

使用SQL语句完成一下功能:

1 建立数据库名为SCHOOL并激活当前新建的数据库

CREATE DATABASE SCHOOL

ON PRIMARY

(NAME= SCHOOL,

FILENAME='D:\SCHOOL.mdf',

SIZE= 5MB,

MAXSIZE= 20MB,

FILEGROWTH= 1MB)

LOG ON

(NAME= SCHOOL_log,

FILENAME='D:\SCHOOL_log.ldf',

SIZE= 2MB,

MAXSIZE= 20MB,

FILEGROWTH= 10%)

GO

use SCHOOL

2 建表:在定义中声明

1)每个表要有主键

2)学生的性别只能取‘男’或者‘女’

3)学生姓名不能为空

4)表CLASS中建立外键系名,关联表DEPARTMENT中的系名

5)系代号不能重复且不能为空

(1)学生表STUDENT(学号,姓名,性别,生源地,出生日期)create table student -- 学生表定义

(

学号char(10)primary key,-- 主键为学号

姓名varchar(20)not null,-- 姓名不能为空值

性别char(4)default'男',-- 性别默认为男性

生源地varchar(12),--生源地

出生日期datetime,

check((性别='男')or(性别='女'))-- 性别必须为男性或女性

)

(2)班级表CLASS(班号,专业名,系名,入学年份)

--班级表CLASS(班号,专业名,系名,入学年份)

create table class -- 班级表定义

(

班号char(4)primary key not null,-- 定义班号不能为空值

专业名char(20),-- 定义专业名

系号char(4),-- 定义学分

入学年份datetime,-- 入学年份

foreign key(系号)references department(系号),

)

(3)系DEPARTMENT(系号,系名)

create table department -- 系表定义

(

系号char(4)primary key not null,--系代号不能为空

系名char(8),--系名

unique(系号),-- 系号是唯一的,即系号不能重复

)

3 插入如下数据

CALSS(101,软件,02,2003;

102,微电子,02,2004)

STUDENT(8101,张三,男,101,山东,1984;

8102,钱四,女,121,济南,1984;

8103,王玲,女,131,福建,1985;

8105,李飞,男,102,河北,1985)

DEPARTMENT(02,计算机;

03,数学;

04,化学)

insert into class values('101','软件','02','2003-09-01')

insert into class values('102','微电子','02','2004-09-01')

insert into student values('8101','张三','男','山东','1984-01-01') insert into student values('8102','钱四','女','济南','1984-01-01') insert into student values('8103','王玲','女','福建','1985-01-01') insert into student values('8105','李飞','男','河北','1985-01-01')

insert into department values('02','计算机')

insert into department values('03','数学')

insert into department values('04','化学')

4 完成以下的查询功能

(1)通过对学生信息表的查询,查询出所有学生的学号,姓名和性别select学号,姓名,性别

from student

(2)通过对学生信息表的查询,查询来自福建的学生的记录

select*

from student

where生源地='福建'

(3)在查询语句中,查询出学生姓名并计算出学生的年龄大小

select姓名,datediff(yy,出生日期,getdate())

from student

(4)用模糊查询出姓钱的女生记录

select*

from student

where姓名like'钱%'

(5)在查询语句中使用聚合函数计算出所有学生总数起别名为学生人数select count(*)as'学生人数'

from student

(6)查询所有班级的信息,并用班级号进行降序排序

select*

from class

order by班号desc

(7)更新学号为8103的这位学生的姓名为张玲

update student

set姓名='张玲'

where学号='8103'

(8)查找出学号在8101到8103的学生信息

select*

from student

where学号between'8101'and'8103'

(9)删除姓名为李飞的学生记录

delete from student

where姓名='李飞'

(10)删除数据表STUDENT

drop table student

T-SQL语言练习题

从学生表Student(Sno,Sname,Ssex,Sage,Sdept)中查询出全体学生的学号与姓名

1. 查询全体学生的详细记录

2. 显示前5条纪录

3. 显示前50%条纪录

4. 查询所有年龄在17岁以下的学生姓名及其年龄。

5. 某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的学号和相应的课程号。(成绩为null)

6. 查所有有成绩的学生学号和课程号

7. 查询学生的所有信息,按学号的降序排列

8 查询选修了课程的学生学号

9. 查全体学生的姓名及其出生年份,显示两列:姓名、出生年份

10. 查询年龄在15~17岁(包括15岁和17岁)之间的学生的姓名、年龄。

11. 查询年龄不在15~17岁之间的学生姓名、系别和年龄。

12. 查询年龄不在15~17岁之间的男生姓名、系别和年龄。

13. 将上题查询的结果插入一个新表中。

14. 查询学生总人数。

15. 查询选修了课程的学生人数。

16. 计算1001号课程的学生平均成绩。

17. 查询选修1号课程的学生最高分数。

18. 求各个课程号及相应的选课人数。(group by)

19. 查询选修了1门以上课程的学生学号。(having)

20. 请说明union的作用。

21. 请举例说明With cube和With rollup的作用。

22. 使用compute 汇总所有学生的成绩平均分。

23. 使用compute by汇总每个人的选修课程数。

1.select * from student

2.select top 5 * from student

3.select top 50 percent * from student

4.select sname,sage from student where sage<17

5.select sno,cno from sc where score is NULL

6.select sno,cno from sc where score is not NULL

7.select * from student order by sno desc

8.select distinct sno from sc

9.select sname,2010-sage as 出生年份from student

10.select sname,sage from student where sage between 15 and 17

11.select sname,sdept,sage from student where sage is not between 15 and 17

12.select sname,sdept,sage from student where ssex='男' and sage is not between 15 and 17

13.select sname,sdept,sage into newtable from student where ssex='男' and sage is not between 15 and 17

14.select count(*) from student

15.select count(distinct sno) as 人数from sc

16.select avg(score) as 平均成绩from sc where cno =1001

17.select max(score) as 最高分数from sc where cno =1

18.select cno,count(*) as 选课人数from sc group by cno

19.select cno, count(*) as 选课人数from sc group by cno having count(*)>1

20.在列数和列的顺序相同且数据类型相同的前提下,将多个select语句返回的结果组合到同一个结果当中。

21.select cno,cname,count(cno)as 人数from course group by cno,cname with cube说明每一个分组统计的总数

select cno,cname,count(cno)as 人数from course group by cno,cname with rollup说明每一个小分组的统计总数

22.select sno,cno,score from sc compute avg(score) 统计所有内容,求出平均成绩

23.select * from sc order by sno,cno compute count(cno) by sno按SNO,CNO分组进行统计

使用ANSI连接和sql server 连接两种方式完成:

1. 查询每个学生的学号、姓名及其选修课程的课程号、成绩。

使用ANSI:select student.sno,sname,sc.sno,cno from student inner join sc on student.sno=sc.sno

使用sql server:select student.sno,sname,sc.sno,cno from student,sc where student.sno=sc.sno

2. 查询出'101'号学生选修的课程的课程名称和学分

使用ANSI:select cname,ccredit from course inner join sc on

https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o

使用sql server:select cname,ccredit from course,sc where https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o and sc.sno='101'

3 查询出选修‘1002’号课程的学生的学号、姓名。

使用ANSI select student.sno,sname from student inner join sc on student.sno=sc.sno and https://www.doczj.com/doc/155176231.html,o='1002'

使用sql server:select student.sno,sname from student,sc where student.sno=sc.sno and https://www.doczj.com/doc/155176231.html,o='1002'

4 --查询与“name2”在同一个系学习的学生信息。

select *from student where sdept in (select sdept from student where sname='name2')and sname!='name2'

查男女各有多少人select ssex ,count(*)as 人数from student group by ssex

按降序排列:group by是分组order by 是排序

select ssex ,count(*)as 人数from student group by ssex order by ssex desc

选课多余2的人数select cno,count(*)from sc group by cno having count(*)>'2'

查询出‘101’号学生选修的课程的课程名称和学分。

使用sql server:select cname,ccredit from sc,course where sno='101' and https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o

使用ANSI:select cname,ccredit from course inner join sc on https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o and sno='101' 嵌套查询:select cname,ccredit from course where cno in (select cno from sc where sno='101') exists查询:select cname,ccredit from course where exists(select * from sc where cno=https://www.doczj.com/doc/155176231.html,o

and sno='101')

--查询选修课程号为“1001”的所有男生的姓名和该科成绩。

sql server:select sname, score from student,sc where student.sno=sc.sno and https://www.doczj.com/doc/155176231.html,o='1001'and student.ssex='男'

ANSI:select sname,score from student inner join sc on student.sno=sc.sno and student.ssex='男'and https://www.doczj.com/doc/155176231.html,o='1001'

--查询出‘101’号学生选修的课程的学分总和。

--使用sql server:select sum(ccredit) as 总学分from sc,course where sc.sno='101'and https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o

--使用ANSI:select sum(ccredit) as 总学分from course inner join sc on sc.sno='101'and https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o

--嵌套查询:

select sum(ccredit) as 总学分from course where cno in(select cno from sc where sno='101')

--exists:select sum(ccredit) from coursewhere exists(select cno from sc where sno='101' and https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o)

--查询出每个学生已经修过的总学分。

sql server:select sno,sum(ccredit) as 总学分from sc,course where https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o group by sc.sno order by sum(ccredit) desc

--使用ANSI:select sno,sum(ccredit) as 总学分from scinner join course on https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o group by sc.sno

order by sum(ccredit) desc

--查询出选修‘c语言’的学生的学号、姓名和成绩

--使用sql server:

select student.sno,sname,score

from student,sc,course

where https://www.doczj.com/doc/155176231.html,ame='c'

and student.sno=sc.sno

and https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o

--查询出选修了学分是4的课程的学号

--使用sql server:

select https://www.doczj.com/doc/155176231.html,o,sno

from sc,course

where https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o

and https://www.doczj.com/doc/155176231.html,redit='4'

--查询出选修了学分是4的课程的姓名

--使用sql server:

select cname,sno

from sc,course

where https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o

and https://www.doczj.com/doc/155176231.html,redit='4'

--查询出没有选修学分是4的课程的学号

--使用sql server:

select sno ,https://www.doczj.com/doc/155176231.html,o

from sc,course

where https://www.doczj.com/doc/155176231.html,o=https://www.doczj.com/doc/155176231.html,o

and https://www.doczj.com/doc/155176231.html,redit<>'4'

任务1(insert)

1.将一个新生记录(学号:111;姓名:陈冬;性别:男;所在系:IS;年龄:18岁)插入到Student表中。

insert into

student (sno,sname,ssex,sage,sdept)

values ('111','陈冬','男','18','IS')

2.插入一条选课记录(sno:'111',cno:'1111 '),新插入的记录在score列上将会取空值。能插入吗?

存在外键则不能

3.student数据库中,有一个表Deptage(Sdept,Avgage)用来存放每个系的学生平均成绩,但还没有数据。请你对每一个系求学生的平均年龄,并把结果存入表Deptage。

select sdept,avg(sage)as avgage

into avger

from student

group by sdept

任务2(update):

1.将学生'101'的年龄改为19岁。

update student

set sage='19'

where sno='101'

2.将所有学生的年龄增加1岁。

update student

set sage=sage+1

3.将信息系全体学生的成绩置零。

update sc

set score=0

where sno in (

select sno from student

where sdept='信息')

任务3(delete):

1.删除学号为'102'的学生选课记录。

delete sc

where sno='102'

2.删除所有的学生选课记录。

delete sc

3.删除信息系所有学生的选课记录。

delete sc

where sno in(

select sno

from student

where sdept='信息')

4.你能删除student表中学号为'101'的学生记录吗?问什么?

不能删除

DELETE 语句与REFERENCE 约束"FK_sc_student1"冲突。该冲突发生于数据库"students",表"dbo.sc", column 'sno'。

任务4(视图)

创建一个视图view1,查询选修课程号为"1001"的所有女生的姓名和该科成绩。

create view view1

as

select sname,score from student inner join sc on student.sno=sc.sno

where ssex='女' and cno='1001'

完成以下操作:创建一个视图v1,在视图中包含sc表中及格的选课信息。

create view v1

as select *from sc

where score>=60

1. 插入数据记录:将这两条记录插入视图v1 中('105','1001',69)('105','1002',50)。观察视图和表的记录变化。

insert into v1

values ('105','1001',69)

insert into v1

values ('105','1002',50)

2. 修改数据记录:修改视图v1,将('105','1001',69)的成绩改为99

UPDATE v1

set score='99'

where sno='105' and cno='1001'。

3. 删除数据记录:修改视图v1,删除105号学生的选课记录。

delete from v1

where sno='105'

相关主题
文本预览
相关文档 最新文档