实验五 为表格建立约束,修改约束和查询约束
2012-06-16 17:00阅读:
1. 目的要求:
使用ALTER语句和CREATE语句建立、修改、删除和查询约束
2. 实验内容
执行以下SQL语句,完成随后的操作,若有错误,分析错误原因并改正错误:
Create table
student(sno
char(9)
not
null
, sname
char(10),ssex
char(2),sage
tinyint,sdept
varchar(40));
Create table
course(cno
char(4)
not
null, cname
varchar(30),cpno
char(4),credit
tinyint);
Create table
sc(sno
char(9),char(5),grade
numeric(3,1));
第三句char 没有对象
应该修改为Create table
sc(sno char(9),cno char(5),grade
numeric(3,1));
1)在student表中,使sdept只能取值“计算机科学学院”,“数软学院”,“电子工程学院”,“化学与材料科学学院”
Alter table
student add
check
(sdept
in
('计算机科学学院','数软学院','电子工程学院','化学与材料科学学院'));
2)在student表中sage有默认值
alter table
student add
constraint
sage_student default
18 for
sage;
3)为student表建立主键
alter table
student add
primary
key
(sno);
4)为course表建立主键和外键,其中外键约束名为C_FK_CPNO
alter table
course add
constraint PK_course
primary
key(cno);
alter table
course add
constraint Fk_course
foreign
key(cpno)
references
course(cno);
5)为course表建立检查约束,限定credit的取值只能取,,,;
alter table
course add
check
(credit
in
('3','2','4','5'));
6)为course表建立唯一约束,确保每们课程名字唯一
alter table
course add
unique(cname);
7)为sc表建立主键和外键,并给出相应的约束名
需要修改sc表中cno的取值
Create table
sc(sno
char(9),cno
char(4),grade
numeric(3,1));
alter table
sc add
constraint PK_sc
primary
key(sno,cno);
alter table
sc add
constraint Fk_sc
foreign
key(sno)
references
student(sno);
alter table
sc add
constraint Fk_sc
foreign
key(cno)
references
course(cno)
;
8)在course表中插入元组(1,数据库,,)和(2,数学,null,2),若不能正确插入,分析原因,给出解决办法
不能插入,因为先行课参照了课程号,所以必须先插入课程号,才能顺利插入下面语句
insert into
course(cno,cname,cpno,credit)
values('1','数据库','5','4');
insert into
course(cno,cname,cpno,credit)
values('2','数学',null,'2');
9)在sc表中插入元组(,1,92)和(,,),若不能正确插入,分析原因,给出解决办法
因为SC表中的属性参照了course和student的属性,所以应该在被参照的表中插入相关数据后,才能顺利插入下面语句
insert into
sc(sno,sno,grade)
values('1','数据库','5','4');
insert into
sc(sno,sno,grade)
values('2','数学',null,'2');
(2)使用系统存储过程,sp_help,
sp_helpconstraint等对约束进行查询和管理
查阅联机帮助文档,选中“索引”选项卡,分别输入sp_help,
sp_helpconstraint,阅读其帮助信息。
使SC数据库成为当前数据库,执行如下命令,简要解释执行结果
1)Exec
sp_help
sp_help 过程仅在当前数据库中查找对象。
如果未指定name,则sp_help
将列出当前数据库中所有对象的对象名称、所有者和对象类型。sp_helptrigger
提供有关触发器的信息。
如果执行不带参数的sp_help,则返回当前数据库中现有的所有类型对象的汇总信息。
如果name 是SQL Server
数据类型或用户定义数据类型,则sp_help
将返回此结果集。
如果name
是数据库对象而不是数据类型,则sp_help
将根据指定的对象类型返回此结果集,同时返回其他结果集。
根据指定的数据库对象,sp_help
将返回其他结果集。
如果name
是系统表、用户表或视图,则sp_help
将返回下列结果集。但是,不会为视图返回说明数据文件在文件组中位置的结果集。
2)Exec
sp_help
student
返回student结果集。但是,不会为视图返回说明数据文件在文件组中位置的结果集。
3)Exec
sp_help
course
返回course结果集。但是,不会为视图返回说明数据文件在文件组中位置的结果集。
4)Exec
sp_helpconstaint sc
显示sc表中的约束信息
5)Exec
sp_helpconstraint
student
显示student表中的约束信息
对存储过程sp_help和sp_helpconstraint进行总结,简要解释其用途
执行sp_help
table
将报告有关指定表的所有信息。若要仅查看约束信息,就要使用sp_helpconstraint。
(3)修改约束:关闭和打开某个约束。
1)在course表中插入元组(,数据结构,,),能够插入吗?为什么?
不能,因为course中的外码cpno参照了course中的主键cno,所以cpno必须是cno已经存在的值
2)禁用约束C_FK_CPNO,再次插入元组(,数据结构,,),能够插入吗?为什么?
能够,此时约束已经不存在了
alter table
course nocheck
constraint
fk_course;
insert into
course(cno,cname,cpno,credit);
values('1','数据库','5','4');
3)重新启用约束C_FK_CPNO
alter table
course check
constraint
fk_course;
3. 主要仪器设备及软件:
(1)PC
(2)Microsoft SQL Server
2005