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

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

ID:57904354

大小:25.72 KB

页数:6页

时间:2020-09-02

主键-外键等约束详解实例.docx_第1页
主键-外键等约束详解实例.docx_第2页
主键-外键等约束详解实例.docx_第3页
主键-外键等约束详解实例.docx_第4页
主键-外键等约束详解实例.docx_第5页
资源描述:

《主键-外键等约束详解实例.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、1、--创建表createtabletb_Dept(Deptidchar(2)Primarykey,DeptNamechar(16)NotNull)2、--外键约束createtabletb_Student(Studidchar(10)Primarykey,Studnamechar(8)Notnull,Deptidchar(2)Notnull,ConstraintFK_DeptIDForeignKey(Deptid)ReferencesTb_Dept(DeptID))3、--外键约束简化形式,必须要求tb_Dept表中DeptID为主键,且数值类型相同createt

2、ableTb_Student(StudIdchar(10)Primarykey,StudNamechar(8)Notnull,DeptIDchar(2)notnullReferencesTb_Dept)4、--创建表,无主键createtableTb_Class(ClassIDchar(8)notnull,ClassNamevarchar(30)notnull,DeptIdchar(2)notnull,ClassStuNumberint)5、--创建表,同时定义主键createtableTb_Class(classidchar(8)notnull,ClassName

3、varchar(30)notnull,DeptIDchar(2)notnull,ClassStuNumberintconstraintPK_ClassIDPrimarykey(ClassID,ClassName))6、--新增主键AltertableTb_classADDConstraintPK_ClassIDprimarykey(Classid)7、--删除主键Altertabletb_ClassDeleteConstraintPK_ClassIDPrimarykey(ClassID)8、--外键级联更新,删除,简化形式Createtabletb_student(

4、studIDchar(10)Primarykey,StudNamechar(10)notnull,DeptIDchar(2)notnullReferencestb_DeptOnUpdatecascadeondeletecascade)9、--外键级联更新,删除,标准createtabletb_student(studidchar(10)Primarykey,StudNamechar(8)notnull,DeptIDchar(2)notnull,ConstraintFK_Deptidforeignkey(DeptID)ReferencesTb_Dept(DeptID)

5、onupdateCascadeondeletecascade)10、--创建无外键的表createtabletb_student(studIdchar(10)Primarykey,StudNamechar(8)notnull,DeptIDchar(2)notNull)11、--给相应的列(DeptID)添加外键约束Altertabletb_StudentADDConstraintFK_DeptIDForeignkey(DeptID)Referencestb_Dept(DeptID)12、--删除外键约束Altertabletb_StudentDropConstrai

6、ntfk_DeptID13、--创建表是创建Unique约束Createtabletb_Student(studIdchar(10)Primarykey,Studnamechar(8)notnullUniquenonclustered,DeptIDchar(2)notnullreferencesTb_Dept)createtabletb_student(studIDchar(10)Primarykey,Studnamechar(8)notnull,deptidchar(2)notnullreferencestb_dept,constraintUk_StunameUn

7、ique(Stuname))14、--创建表结束后,添加、删除Unique约束--添加Unique约束altertabletb_StudentADDConstraintUk_DepnameUnique(Deptname)15、--删除unique约束Altertabletb_studentDropConstraintuk_Depname16、--创建默认值约束Createtabletb_student(stuIdchar(10)Primarykey,stuNamechar(8)notnull,DeptIDchar(2)Notnullreferencestb_De

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。