数据库完整性约束1
SQL的安全機(jī)制:完整性定義約束,視圖機(jī)制,對(duì)用戶進(jìn)行授權(quán)控制
完整性約束分類(lèi):
1).根據(jù)數(shù)據(jù)對(duì)象的狀態(tài)可分為: 靜態(tài)約束與動(dòng)態(tài)約束
a.靜態(tài)約束: 隱式約束和顯式約束
隱式約束:隱含于數(shù)據(jù)模型中的完整性約束。關(guān)系模型的隱式約束有域約束和表約束
域約束即列值域的約束,只能使用CHECK來(lái)約束
表約束包括表級(jí)約束和列級(jí)約束 涉及PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, DEFAULT, NOT NULL
? 顯式約束:隱式約束不能完全描述現(xiàn)實(shí)中的規(guī)定或約束,特別是與具體應(yīng)用有關(guān)的約束。其定義方式有:過(guò)程化定義、斷言、觸發(fā)器。斷言是定義的表與表之間的約束關(guān)系。和域約束一樣,斷言也只能使用CHECK約束。
b.動(dòng)態(tài)約束:反映數(shù)據(jù)庫(kù)狀態(tài)變遷的約束。
2).根據(jù)數(shù)據(jù)庫(kù)的實(shí)體對(duì)象可分為實(shí)體本身的完整性和實(shí)體之間的參照完整性
?表約束:
1).DEFAULT約束
DEFAULT 'abc'
DEFAULT 2010
DEFAULT getdate()
DEFAULT null
2).NOT NULL約束
create table test(id int, name nvarchar(20) not null)
3).CHECK約束
constraint ck_test check(starttime<=endtime and endtime is null)
4).UNIQUE約束
create table test(id int unique, name nvarchar(20) unique, code varchar(11) not null)
????? create table test2(id int , name nvarchar(20) , code varchar(11) not null constraint uq_test unique(id,name))
5).FOREIGN KEY約束
deptno char(2) not null default '01' references dept(deptno)
constraint fk_test foreign key(deptno) references dept(deptno)
禁止和使用約束:
alter table test disable/enable constraint ck_test
alter table test disable/enable primary key
轉(zhuǎn)載于:https://www.cnblogs.com/andhm/archive/2010/05/26/1744333.html
總結(jié)
- 上一篇: 玩转博客园的5个小技巧
- 下一篇: linux cmake编译源码,linu