主鍵,外鍵等約束詳解實(shí)例_第1頁
主鍵,外鍵等約束詳解實(shí)例_第2頁
主鍵,外鍵等約束詳解實(shí)例_第3頁
主鍵,外鍵等約束詳解實(shí)例_第4頁
主鍵,外鍵等約束詳解實(shí)例_第5頁
已閱讀5頁,還剩1頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、1、-創(chuàng)建表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為主鍵,且數(shù)值類型相同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、-創(chuàng)建表,無主鍵create table Tb_Class(ClassID char(8) not null,ClassName varchar(30) not null,DeptId char(2) not null,ClassStuNumber int)5、-創(chuàng)建表,同時定義主鍵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、-外鍵級聯(lián)更新,刪除,簡化形式

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、-外鍵級聯(lián)更新,刪除,標(biāo)準(zhǔn)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、-創(chuàng)建無外鍵的表create table tb_student(studId char(10) Primary key,StudName char(8) not null,DeptID char(2) not Null)11、-給相應(yīng)的列(DeptID)添加外鍵約束Alter table tb_StudentADDConstraint FK_DeptID Foreign key(DeptID)References tb_Dept(D

6、eptID)12、-刪除外鍵約束Alter table tb_StudentDropConstraint fk_DeptID 13、-創(chuàng)建表是創(chuàng)建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、-創(chuàng)建表結(jié)束后,添加、刪除Unique約束-添加Unique約束alter table tb_StudentADDConstraint Uk_Depname Unique(Deptname)15、-刪除unique約束Alter table tb_studentDropConstraint uk_Depname16、-創(chuàng)建默認(rèn)值約束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、-添加默認(rèn)值約束alter table tb_studentADDconstraint DEF_sex default 'M' for sex18、-刪除默認(rèn)值約束alter table tb_studentDropConstraint DEF_Sex19、-

9、創(chuàng)建表時,創(chuàng)建check約束create 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' ),constrain

10、t ck_StudID Check (StudId like 'S0-90-90-90-90-90-90-90-90-9')20、-check約束的其他例子check(coursescore >= 0 and coursescore < = 100)check(empld like 'A-ZA-ZA-Z1-90-90-90-90-9FM'or empld like 'A-ZA-Z1-90-90-90-90-9FM')check(telno in ('86022679','86022235','8

11、6022879','86022886','86028225') or telno like '8602210-90-9')check(salary between 3000 and 10000)check(is_manager = 1 and sex = 'F')check(case when is_manager<> 1 and sex = 'F') Then 1Else 0 End = 0 21、-添加check約束alter table tb_student with nocheckADD

12、Constraint ck_Sex check(sex in ('M','F')22、-刪除check約束alter table tb_studentDropconstraint ck_sex-數(shù)據(jù)完整性總結(jié)1、-Primary key 約束-非聚集索引不超過個,聚集索引最多個-primary key未指定索引,索引類型與Unique相同-Primary key的所有列必須非空not null2、-Unique約束-默認(rèn)使用nonclustered-每個Unique都生成一個索引,非聚集索引不超過,聚集索引最多個3、-Foreign key-Foreign ke

13、y列輸入非空值,該值必須在被引用列中存在-Foreign key約束僅能引用同一服務(wù)器的數(shù)據(jù)庫表,跨數(shù)據(jù)庫的引用必須通過觸發(fā)器實(shí)現(xiàn)-列級的Foreign key約束的references子句只能列出一個引用列,且數(shù)據(jù)類型必須相同-表級Foreign key約束的references子句引用列的數(shù)目必須與約束列的列數(shù)相同,沒個列的數(shù)據(jù)類型必須相同-類型為timestamp的列是外鍵、被引用鍵的部分,不能指定cascade、set Null、set default-臨時表不強(qiáng)制Foreign key約束-Foreign key只能引用所引用的表的Primary key或unique 約束中的列,或

14、引用的表上的Unique Index中的列4、-Default定義-每列只能有一個Default定義-Default定義可以包含常量值,函數(shù),或Null-不能對類型為timestamp的列,或自增長型的列,創(chuàng)建Default定義5、-Check約束-列級Check約束只能引用被約束的列,表級Check約束只能引用同一表中的列-不能在text、ntext、或image列上定義check約束6、-其他約束相關(guān)信息-為約束創(chuàng)建的索引不能用Drop Index刪除,必須用Alter table刪除約束-如果某個表有約束以及觸發(fā)器,則將在觸發(fā)器執(zhí)行前先檢查約束條件-若要獲得關(guān)于表及其列的報表,請使用sp

15、_help或sp_helpconstraint表名-若要獲得與表相關(guān)的視圖和存儲過程的報表,請使用sp_depends-若列為計算列,是否為空由系統(tǒng)確定。使用帶AllowsNull屬性的ColumnProperty函數(shù)-索引-索引:包含鍵(表或視圖中的一個或多個列所生成),以及指針(映射到數(shù)據(jù)的存儲位置的指針)-索引是一個單獨(dú)的,物理的數(shù)據(jù)庫結(jié)構(gòu),依賴表而建,提供編排表中數(shù)據(jù)的內(nèi)部方法-一個表的存儲由兩部分組成:存儲數(shù)據(jù)的頁面,存儲索引的頁面-建立索引,提高查詢速度,但是過多索引,會占據(jù)過多的磁盤空間-創(chuàng)建索引的列:常用,外鍵或主鍵,值唯一Create Index lx_Stuname On

16、tb_Student(StuName)go-聚集索引與數(shù)據(jù)存儲均為物理順序,查找效率很高,因此每個表只能有一個聚集索引-主鍵的索引默認(rèn)為聚集索引Create Nonclustered index lx_StuName_Sex On tb_Student(StuName,Sex)go-非聚集索引,由指針(指向表中數(shù)據(jù))與一個索引值(一般為鍵)構(gòu)成。Create unique Index UK_StuName on Tb_Student(StuName)Go-唯一索引:確保所有數(shù)據(jù)行,任意兩行,不存在包括Null在內(nèi)的重復(fù)值-不允許有兩行具有相同的索引值1、-查看索引sp_helpindex 表名

17、稱2、-修改索引名sp_rename 表名稱.索引名,新索引名3、-刪除索引Drop Index 表名.索引名-必須指出表名,指明是哪個表的索引文件,否則無法刪除索引-建表實(shí)例if not exists(select * from sysobjects where name = 'tb_Dept')begin create table tb_Dept(DeptID char(2) Primary key,DeptName char(20) not null)endgoif not exists(select * from sysobjects where name = '

18、;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(teacherID char(6) Primary key,teacherName char(8) not null)end

19、goif 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,ClassStuNumber int Default '0',Constraint FK_DirectorID foreign key (DirectorID) references tb_teacher(teacherID)endgoif not exists(select * from sysobjects where name = 'tb_Student')be

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論