实训7 数据完整性操作.xls
文本预览下载声明
Sheet3
Sheet2
Sheet1
目的和要求
实验环境
实验原理
实验内容与步骤
use xk
go
alter table department
add constraint pk_department primary key (departNO)
alter table course
add constraint pk_course primary key (couNO)
alter table class
add constraint pk_class primary key (classNO)
以下为:在创建表的同时创建主键约束。
create table department
(departno nvarchar(2) not null,
departname nvarchar(20) not null,
constraint PK_department primary key (departNO)
)
任务5 使用transact-sql语句删除department表名字为PK_Department的主键约束。
drop constraint pk_department
USE Xk
GO
ALTER TABLE StuCou
ADD CONSTRAINT StuNo_FK FOREIGN KEY (StuNo)
REFERENCES Student (StuNo)
任务7 使用transact-sql语句删除stucou表中名为stuNo_fk的foreign key 约束
DROP CONSTRAINT STuNo_FK
任务8 使用transact-sql语句在stucou表中为stuno列重新创建一个带有级联删除功能的外键约束。
alter table stucou
add constraint stuno_fk foreign key (stuNO)
references student (stuno) on delete cascade
任务9 使用Management studio 在XK数据库中为stucou表创建基于couno的约束,该约束限制couno列值必须是course表中couno列已存在的值。
任务10 使用Management studio删除stucou表名字为couNO_FK的foreign key约束。
任务11使用transact-sql语句创建如下约束,class表的departNO 参照department表的departno.
course表的departNO 参照department表的departNO.student表的classNO参照class表的classNO.
add constraint fk_class_department foreign key (departNO)
references department(departNO)
add constraint fk_course_department foreign key (departNO)
alter table student
add constraint fk_student_class foreign key (classNO)
references class(classNO)
任务12 使用transact-sql语句在xk数据库为course表的couName创建唯一约束un_couname.
创建后用transact-sql语句删除该唯一约束。
alter table course
add constraint un_couname unique(couname)
drop constraint un_couname
任务13 使用transact-sql语句在xk数据库为student表创建名为ch_stuNO的check约束,该约束检查StuNO列值只允许8位数字。
ALTER TABLE Student
ADD CONSTRAINT CK_StuNo CHECK (StuNo like [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
AND StuN
任务14 使用transact-sql语句删除student表中名字为CK_stuNO的check约束。
DROP CONSTRAINT CK_StuNo
任务15 使用transact-sql语句在xk数据库中为stucou表创建名为ch_willorder的check约束,该约束限制willorder的值只能在1-5之间(willorder表示学生报名的志愿号,本系统最多允许学生报5个志愿,故如此
显示全部