SQL SERVER 2000实用教程习题及实验参考答案(3)

2019-03-22 20:29

CONSTRAINT PK_ C_Number PRIMARY KEY CLUSTERED,

C_Name Char(16) NOT NULL , Teacher Char(10) NULL, Hours Int NOT NULL, Credit Int NULL ) GO

--创建课程表T_Score CREATE TABLE T_Score

(S_Number Char(8) NOT NULL

CONSTRAINT FK_S_Number FOREIGN KEY (S_Number) REFERENCES T_Student (S_Number),

C_Number Char(4) NOT NULL

CONSTRAINT PK_SC_Number PRIMARY KEY (S_Number, C_Number)

CONSTRAINT FK_C_Number FOREIGN KEY (C_Number) REFERENCES T_Course (C_Number),

Score Real NULL )

3.在企业管理器中打开表,按照表5-4、5-5、5-6的内容直接输入数据即可。 4.显示错误信息,不能保存该条记录。因为T_Score表的S_Number列与表T_Student的S_Number列存在着外键约束关系,而表T_Student不存在S_Number为20030103的记录。

5. 显示错误信息,不能保存该条记录。因为T_Score表的C_Number列与表T_Course的C_Number列存在着外键约束关系,而表T_Course不存在C_Number为3002的记录。

6. 显示错误信息,不能保存该条记录。因为T_Score表的S_Number列与表T_Student的S_Number列存在着外键约束关系,而表T_Student不存在S_Number为20030105的记录。

7. 显示错误信息,不能删除该条记录。因为T_Score表的C_Number列与表

T_Course的C_Number列存在着外键约束关系,为保证表T_Score数据的完整性,不能删除表T_Course中C_Number为3001的记录。

8.首先删除T_Score表中C_Number=’3001’的所有记录,然后再删除表T_Course中C_Number为3001的记录。

9.

在查询分析器输入如下命令:

EXEC SP_DETACH_DB 'StuInfo', 'true'

单击工具栏中的“运行”按钮,返回“命令已成功完成”的信息,说明已成功分离数据库。

在附加数据库前,将上面分离出来的数据库文件'StuInfo _Data.Mdf、

11

'StuInfo_Log.Ldf复制到另一台计算机的C:\\。然后在查询分析器中运行如下命令: CREATE DATABASE StuInfo ON

PRIMARY (NAME= 'StuInfo_Data', FILENAME = 'C:\\ StuInfo_Data.Mdf' )

LOG ON

(NAME='StuInfo_Log',

FILENAME = 'C:\\ StuInfo_Log.Ldf') FOR ATTACH GO

实验三

1. 查询所有男学生的姓名、出生日期、年龄。

use stuinfo go

Select s_name,birthday, year(getdate())-year(birthday) 年龄 from t_student Where sex='男'

2. 查询所有女学生详细信息和女学生的总人数。

use stuinfo go

select *,count(sex) 人数 from t_student

group by s_number,birthday,sex,s_name having sex='女' compute sum(count(sex))

3. 查询SQL Server课程的总成绩、平均成绩、及格学生人数和不及格学生人数。

use stuinfo go

select t_course.c_name 课程名称,t_score.score 成绩 from t_course,t_score

where t_course.c_number=t_score.c_number and c_name='SQL Server' compute avg(score) compute sum(score) go

select count(*) 'sql server及格学生人数' from t_score where score>=60 and c_number='1'

select count(*) 'sql server不及格学生人数' from t_score where score<60 and c_number='1'

12

4. 分别查询sql server课程所有男生成绩,总分、平均分和所有女生的成绩、总分、平均分。

use stuinfo go

select t_student.s_name 姓名,t_student.sex 性别, t_course.c_name 课程名称,t_score.score 成绩 from t_student,t_course,t_score

where t_course.c_number=t_score.c_number and t_student.s_number=t_score.s_number and c_name='sql server' and t_student.sex='男' compute avg(score) compute sum(score) go

select t_student.s_name 姓名,t_student.sex 性别, t_course.c_name 课程名称,t_score.score 成绩 from t_student,t_course,t_score

where t_course.c_number=t_score.c_number and t_student.s_number=t_score.s_number and c_name='sql server' and t_student.sex='女' compute avg(score) compute sum(score)

5. 查询所有姓李的男学生的选修课程和成绩。

Use stuinfo Go

Select t_student.s_name 姓名,t_student.sex 性别, t_course.c_name 课程名称,t_score.score 成绩 From t_student, t_course, t_score

Where t_student.s_number=t_score.s_number and

t_score.c_number=t_course.c_number and s_name like '李%' and sex='男'

6. 查询所有不及格学生的姓名、不及格课程和不及格课程的成绩。

Use stuinfo Go

Select t_student.s_name,t_course.c_name,t_score.score From t_student,t_course,t_score

Where t_student.s_number=t_score.s_number and t_score.c_number=t_course.c_number and score<60

7. 按男同学进行分组。

Use stuinfo Go

13

Select * from t_student

Group by s_number, s_name, birthday,sex having sex='男'

8. 将t_student表中的前40%条记录在s_number,s_name,polity字段上的值插入到新表t_student1中。

Use stuinfo Go

Select top 40 percent s_number,s_name into t_student1 from t_student

Select * from t_student2

实验四

1. 分别利用企业管理器和创建索引向导为t_score表的score字段建立非聚集索引,索引名为i_score,并按降序排序。(略)

2. 利用transact sql语句新建一个数据表t_user,要求使用CREATE INDEX语句为U_id字段创建一个唯一性聚集索引,索引名为i_id。

第二题代码:

use stuinfo go

create table t_user ( U_id char(10) not null, U_name char(20),

U_password char(10) dafault ‘000000’, U_popedom char(20) )

create unique clustered index i_id on t_user(U_id)

3. 使用SQL语句为t_score表的s_number、c_number字段创建一个复合索引,索引名为i_t_score。设置填充因子为40,指定过期的索引统计不自动重新计算。

第三题程序为:

use stuinfo go

create index i_t_score on t_user(s_number、c_number) WITH PAD_INDEX, FILLFACTOR=50, STATISTICS_NORECOMPUTE

4. 使用存储过程将上题中建立的索引i_t_score重命名为index_s_c。 第四题源代码:sp_rename i_t_score,index_s_c

5. 分别使用企业管理器和SQL语句两种方法删除索引index_s_c。

14

第五题程序为:drop index index_s_c

实验五

1、如何为“姓名”字段添加一个唯一性约束?代码如下:

use student2 go

ALTER TABLE 基本表

ADD CONSTRAINT IX_E UNIQUE (姓名) GO

2、利用T-SQL语句创建一个名为“课程信息表” 的表,代码如下:

use student2 go

create table课程信息表 (

课程编号 char(10) not null primary key, 课程名 char(30), 学时 int,

学分 real default '4' )

go

3. 利用T-SQL语句创建一个名为“学生成绩表”的表,代码如下:

use student2 go

create table 学生成绩表( 学号 char(10) not null, 课程编号 char(10) not null, 分数 real,

constraint pk_s primary key (学号, 课程编号), constraint chk_score check (分数>0 and 分数<100), constraint con_num references 课程信息表(课程编号)

) go

4. 如何使用“alter table……add”命令为“课程名”字段增加唯一性约束。

use student2 go

alter table 课程信息表

add constraint uk_c unique(课程名) go

5. 如何使用命令删除学生成绩表中的一个外键约束,代码如下:

15


SQL SERVER 2000实用教程习题及实验参考答案(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:辩论材料真才实学比人际关系更重要

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: