主键,外键等约束详解实例.docx

上传人:PIYPING 文档编号:11518605 上传时间:2021-08-12 格式:DOCX 页数:6 大小:25.72KB
返回 下载 相关 举报
主键,外键等约束详解实例.docx_第1页
第1页 / 共6页
主键,外键等约束详解实例.docx_第2页
第2页 / 共6页
主键,外键等约束详解实例.docx_第3页
第3页 / 共6页
主键,外键等约束详解实例.docx_第4页
第4页 / 共6页
主键,外键等约束详解实例.docx_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《主键,外键等约束详解实例.docx》由会员分享,可在线阅读,更多相关《主键,外键等约束详解实例.docx(6页珍藏版)》请在三一文库上搜索。

1、1、-创建表create table tb_Dept(Deptid char(2) Primary key,DeptName char(16) Not Null)2、-外键约束create table tb_Student(Studid char(10) Primary key,Studname char(8) Not null,Deptid char(2) Not null,Constraint FK_DeptID Foreign Key (Deptid)References Tb_Dept(DeptID)3、-外键约束简化形式,必须要求tb_Dept表中DeptID为主键,且数值类型相同c

2、reate table Tb_Student(StudId char(10) Primary key,StudName char(8) Not null,DeptID char(2) not null References Tb_Dept)4、-创建表,无主键create table Tb_Class(ClassID char(8) not null,ClassName varchar(30) not null,DeptId char(2) not null,ClassStuNumber int)5、-创建表,同时定义主键create table Tb_Class(classid char(8

3、) not null,ClassName varchar(30) not null,DeptID char(2) not null,ClassStuNumber intconstraint PK_ClassID Primary key(ClassID,ClassName)6、-新增主键Alter table Tb_classADDConstraint PK_ClassID primary key(Classid)7、-删除主键Alter table tb_ClassDeleteConstraint PK_ClassID Primary key(ClassID)8、-外键级联更新,删除,简化形式

4、Create table tb_student(studID char(10) Primary key,StudName char(10) not null,DeptID char(2) not null References tb_Dept On Update cascade on delete cascade)9、-外键级联更新,删除,标准create table tb_student(studid char(10) Primary key,StudName char(8) not null,DeptID char(2) not null,Constraint FK_Deptid fore

5、ign key(DeptID)References Tb_Dept(DeptID) on update Cascade on delete cascade )10、-创建无外键的表create table tb_student(studId char(10) Primary key,StudName char(8) not null,DeptID char(2) not Null)11、-给相应的列(DeptID)添加外键约束Alter table tb_StudentADDConstraint FK_DeptID Foreign key(DeptID)References tb_Dept(D

6、eptID)12、-删除外键约束Alter table tb_StudentDropConstraint fk_DeptID 13、-创建表是创建Unique约束Create table tb_Student(studId char(10) Primary key,Studname char(8) not null Unique nonclustered,DeptID char(2) not null references Tb_Dept)create table tb_student(studID char(10) Primary key,Studname char(8) not null,

7、deptid char(2) not null references tb_dept,constraint Uk_Stuname Unique(Stuname)14、-创建表结束后,添加、删除Unique约束-添加Unique约束alter table tb_StudentADDConstraint Uk_Depname Unique(Deptname)15、-删除unique约束Alter table tb_studentDropConstraint uk_Depname16、-创建默认值约束Create table tb_student(stuId char(10) Primary key

8、,stuName char(8) not null,DeptID char(2) Not null references tb_Dept,sex char(2) not null default M,Birthday smalldatetime not null default getdate()17、-添加默认值约束alter table tb_studentADDconstraint DEF_sex default M for sex18、-删除默认值约束alter table tb_studentDropConstraint DEF_Sex19、-创建表时,创建check约束create

9、 table tb_student(StudId char(10) Primary key,Studname char(8) not null,DeptId char(2) not null references tb_Dept,sex char(2) not null default M check(sex in (M,F),zipcode char(6) not null check (Zipcode like 0-90-90-90-90-90-9 ),constraint ck_StudID Check (StudId like S0-90-90-90-90-90-90-90-90-9)

10、20、-check约束的其他例子check(coursescore = 0 and coursescore = 100)check(empld like A-ZA-ZA-Z1-90-90-90-90-9FMor empld like A-ZA-Z1-90-90-90-90-9FM)check(telno in (86022679,86022235,86022879,86022886,86028225) or telno like 8602210-90-9)check(salary between 3000 and 10000)check(is_manager = 1 and sex = F)c

11、heck(case when is_manager 1 and sex = F) Then 1Else 0 End = 0 21、-添加check约束alter table tb_student with nocheckADDConstraint ck_Sex check(sex in (M,F)22、-删除check约束alter table tb_studentDropconstraint ck_sex-数据完整性总结1、-Primary key 约束-非聚集索引不超过个,聚集索引最多个-primary key未指定索引,索引类型与Unique相同-Primary key的所有列必须非空n

12、ot null2、-Unique约束-默认使用nonclustered-每个Unique都生成一个索引,非聚集索引不超过,聚集索引最多个3、-Foreign key-Foreign key列输入非空值,该值必须在被引用列中存在-Foreign key约束仅能引用同一服务器的数据库表,跨数据库的引用必须通过触发器实现-列级的Foreign key约束的references子句只能列出一个引用列,且数据类型必须相同-表级Foreign key约束的references子句引用列的数目必须与约束列的列数相同,没个列的数据类型必须相同-类型为timestamp的列是外键、被引用键的部分,不能指定cas

13、cade、set Null、set default-临时表不强制Foreign key约束-Foreign key只能引用所引用的表的Primary key或unique 约束中的列,或引用的表上的Unique Index中的列4、-Default定义-每列只能有一个Default定义-Default定义可以包含常量值,函数,或Null-不能对类型为timestamp的列,或自增长型的列,创建Default定义5、-Check约束-列级Check约束只能引用被约束的列,表级Check约束只能引用同一表中的列-不能在text、ntext、或image列上定义check约束6、-其他约束相关信息-

14、为约束创建的索引不能用Drop Index删除,必须用Alter table删除约束-如果某个表有约束以及触发器,则将在触发器执行前先检查约束条件-若要获得关于表及其列的报表,请使用sp_help或sp_helpconstraint表名-若要获得与表相关的视图和存储过程的报表,请使用sp_depends-若列为计算列,是否为空由系统确定。使用带AllowsNull属性的ColumnProperty函数-索引-索引:包含键(表或视图中的一个或多个列所生成),以及指针(映射到数据的存储位置的指针)-索引是一个单独的,物理的数据库结构,依赖表而建,提供编排表中数据的内部方法-一个表的存储由两部分组成

15、:存储数据的页面,存储索引的页面-建立索引,提高查询速度,但是过多索引,会占据过多的磁盘空间-创建索引的列:常用,外键或主键,值唯一Create Index lx_Stuname On tb_Student(StuName)go-聚集索引与数据存储均为物理顺序,查找效率很高,因此每个表只能有一个聚集索引-主键的索引默认为聚集索引Create Nonclustered index lx_StuName_Sex On tb_Student(StuName,Sex)go-非聚集索引,由指针(指向表中数据)与一个索引值(一般为键)构成。Create unique Index UK_StuName on

16、 Tb_Student(StuName)Go-唯一索引:确保所有数据行,任意两行,不存在包括Null在内的重复值-不允许有两行具有相同的索引值1、-查看索引sp_helpindex 表名称2、-修改索引名sp_rename 表名称.索引名,新索引名3、-删除索引Drop Index 表名.索引名-必须指出表名,指明是哪个表的索引文件,否则无法删除索引-建表实例if not exists(select * from sysobjects where name = tb_Dept)begin create table tb_Dept(DeptID char(2) Primary key,DeptN

17、ame char(20) not null)endgoif not exists(select * from sysobjects where name = tb_SPec)begin create table tb_SPec(SpecID char(4) Primary key,SpecName char(20) not null,DeptID char(2) references tb_Dept,)endgoif not exists(select * from sysobjects where name = tb_teacher)begin create table tb_teacher

18、(teacherID char(6) Primary key,teacherName char(8) not null)endgoif not exists(select * from sysobjects where name = tb_class)begin create table tb_class(ClassID char(8) Primary key,ClassName nchar(32) not null Unique,DeptID char(2) not null references tb_Dept,DirectorID char(6) not null,ClassStuNum

19、ber int Default 0,Constraint FK_DirectorID foreign key (DirectorID) references tb_teacher(teacherID)endgoif not exists(select * from sysobjects where name = tb_Student)begin create table tb_Student(StuID char(10) Primary key,StuName char(8) not null Unique,DeptId char(2) not null references tb_Dept,

20、SpecID char(4) not null references tb_Spec,ClassID char(8) not null references tb_Class,Sex char(1) not null Default M check(Sex in (M,F),Birthday smalldatetime not null,SPassword varchar(10) not null Default 123456 check(SPassword like 0-Z0-Z0-Z0-Z0-Z0-Z),ZipCode char(6) not null check(Zipcode like 0-90-90-90-90-90-9),Address nvarchar(50)endgoselect * from tb_Deptselect * from tb_SPecselect * from tb_teacherselect * from tb_classselect * from tb_Student

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1