SQL Server网络数据库复习资料计网1、2班

更新时间:2024-05-12 20:25:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1. SQL Server 2000的4个系统数据库和2个样例数据库是什么?

2. 重命名表student 为stu

3. 为表student创建一个非聚集索引,索引字段为s_name,索引名为I_st。

USE teachdb

CREATE INDEX I_st ON student(s_name)

4.查询teachdb 数据库中“student”里的学号、姓名和系别。

Use teachdb

Select s_no,s_name,s_department from student。

5.查询teachdb数据库中“学生成绩表”的数据,并将成绩加20%。

Use teachdb

Select s_no,c_no,score*1.2 from choice

6.使用WHERE子句从teachdb数据库的“student”中检索出所有男生。

Use teachdb

Select * from student where s_sex=’男’

7.查询teachdb数据库中“学生成绩表”的姓名和成绩,并按照成绩从低到高排序。

Use teachdb

Select s_no,score from choice order by score

8.查询pubs数据库中authors表里,作者的姓(au_lname)以“L”开头的作者姓名和电话号码。

Use pubs

Select au_lname,au_fname,phone from authors where au_lname like ‘L%’

9.从student表中分别检索出所有姓李的同学的资料,名字的第二个字是“平”或“萍”的所有同学的资料。

use teachdb

select * from student where s_name like ‘李[平萍]%’

10.显示student表中除计算机系之外的其他所有同学的信息。

use teachdb

SELECT * FROM student WHERE s_department NOT LIKE ’计算机’ 11.使用统计函数求出teachdb数据库中成绩大于80分的同学人数以及平均分。

USE teachdb

select count(*),avg(score) from choice where score>80

12.查询teachdb数据库中,所有机电系学生的人数。

USE teachdb

select count(*) from student where s_department like '机电%'

13.统计各门课程的平均分,使用GROUP BY子句进行分组查询,。

USE teachdb

SELECT c_no,'total'=AVG(score) FROM choice GROUP BY c_no ORDER BY c_no

14.查询1012课程的最高分和最低分。

USE teachdb

select max(score) as '最高分',min(score) as '最低分' from choice where c_no='1012'

15.统计各门课程的平均分,只显示平均分大于80分的课程编号和成绩。

USE teachdb

SELECT c_no,'total'=avg(score) FROM choice GROUP BY c_no having avg(score)>80 ORDER BY c_no

16.返回一张包括学号,课程名,成绩,成绩及格的信息表。分别用FORM子句中定义联接和在WHERE子句中定义(内)联接实现。

USE teachdb

SELECT s_no,c_name,score FROM course join choice ON (course.c_no=choice.c_no) WHERE score>60

SELECT s_no,c_name,score FROM course,choice WHERE course.c_no=choice.c_no AND score>60

17.返回一张包括学号,姓名,课程名,成绩,成绩及格的信息表。

USE teachdb SELECT student.s_no,s_name,c_name,score FROM student,course,choice WHERE student.s_no=choice.s_no AND course.c_no=choice.c_no /*注意第一个student.s_no中的student是不能省略的*/

使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作 18.查询与’李平’在同系学生的信息;

select * from student where s_department in (select s_department from student where s_name='李平')

比较: select * from student where s_department = (select s_department from student where s_name='李平') 的异同

比较: select * from student where s_department = (select s_department from student where s_name='李平') and s_name<>'李平

比较: select S1.* from student S1, student S2 where S1.s_department=S2.s_department and S2.s_name='李平'

19. 查询比’李平’年龄小的所有学生的信息;

select * from student where s_birthday< (select s_birthday from student where s_name='李平')

20. 查询其他系中比计算机系某一学生年龄小的学生姓名和年龄;

select s_name, s_birthday from student where s_birthday '计算机'

21. 查询其他系中比计算机学生年龄都小的学生姓名和年龄;

select s_name, s_birthday from student where s_birthday '计算机'

22. 查询所有选修了1011号课程的学生姓名;

select s_name from student where exists (select * from sc where s_no=student.s_no and c_no='1011')

23. 查询没有选修了1011号课程的学生姓名;

select s_name from student where not exists (select * from sc where s_no=student.s_no and c_no='1')

24.查询成绩(score)大于平均分同学的姓名、性别、选修课程编号和成绩。并将查询结果集组成一个名为newscore的新数据表,表中各字段的名字分别为上面所述的中文名;

USE teachdb

SELECT S.s_name AS 姓名,S.s_sex AS 性别,C.c_no AS 选修课程

号,C.score AS 成绩 INTO newscore FROM student S,choice C WHERE S.s_no=C.s_no AND C.score>(SELECT AVG(score) FROM choice)

25. 查询选修了课程名为’数据结构’ 的学生的学号和姓名。

SQL Server中: select s_no, s_name from student where s_no in (select s_no from sc where c_no in (select c_no from course where cname='数据结构'))

26.将视图stud_ view更名为stud_view4。

sp_rename stud_view1, stud_view4

27.根据程序写结果:

declare @a int set @a=32768

select @a,datalength(@a) go

要求:(1)写出程序运行结果。

(2)把程序类型分别改为smallint、Tinyint、Bigint三种类型,求出程序运行结果。

28.根据程序写结果:

declare @c char set @c='江西工职院'

select @c,datalength(@c) go 要求: (1)写出程序运行结果。

(2)将类型改为CHAR(2)、CHAR(3)、CHAR(5)(3)若将类型改为VARCHAR、VARCHAR(2)多少?

29.声明一个类型为日期时间型的变量,要求一:将今天的日期赋值给该变量,并显示其结果。要求二:将今天的日期接照月、日、年的格式赋值给该变量,并显示其结果。

30.Mary的生日为1987/12/23日,请用日期函数计算DECLARE @a datetime SET @a=’1987-12-23’

SELECT year(getdate())-year(@a) AS

31.求:

declare @x decimal declare @y decimal set @x=-2 if @x>0

set @y=@x-10 else

if @x=0 set @y=@x else

set @y=@x+10 print @y

、CHAR(10)结果为多少? 、VARCHAR(10)结果为Mary现在的年龄。

student

、VARCHAR(3)‘年龄’print @x

32.如果在student表中的男生多,则显示男生人数以及“男生多”信息。

declare @x int declare @y int

set @x=(select count(*) from student where s_sex='男') set @y=(select count(*) from student where s_sex='女') if @x>@y

print '男生多' else

if @x=@y

print '男女生一样多' else print '女生多'

33.执行系统存储过程sp_help查看教学数据库teachdb中student表的信息。

USE teachdb

EXEC sp_help student 34.使用不带参数的存储过程。创建一个查询teachdb数据库中每个同学各门功课成绩的存储过程st1。并要求加密文本条目。

USE teachdb GO

CREATE PROCEDURE student_grade WITH ENCRYPTION AS

SELECT s_name AS 姓名,c_name AS 课程名,score AS 成绩 FROM choice,course,student

WHERE student.s_no=choice.s_no and course.c_no=choice.c_no

35.将st1存储过程名称修改为st。

USE teachdb GO

sp_rename st1,st

36.删除teachdb数据库中的st2存储过程。

USE teachdb GO

DROP PROCEDURE st2

37.将norepeat触发器名称修改为norepeat1 。

USE teachdb GO

sp_rename norepeat, norepeat1

38.删除teachdb数据库中的student表的norepeat1触发器。

USE teachdb GO

DROP trigger norepeat1

本文来源:https://www.bwwdw.com/article/0sug.html

Top