数据库完整性约束1
SQL的安全機制:完整性定義約束,視圖機制,對用戶進行授權控制
完整性約束分類:
1).根據(jù)數(shù)據(jù)對象的狀態(tài)可分為: 靜態(tài)約束與動態(tài)約束
a.靜態(tài)約束: 隱式約束和顯式約束
隱式約束:隱含于數(shù)據(jù)模型中的完整性約束。關系模型的隱式約束有域約束和表約束
域約束即列值域的約束,只能使用CHECK來約束
表約束包括表級約束和列級約束 涉及PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, DEFAULT, NOT NULL
? 顯式約束:隱式約束不能完全描述現(xiàn)實中的規(guī)定或約束,特別是與具體應用有關的約束。其定義方式有:過程化定義、斷言、觸發(fā)器。斷言是定義的表與表之間的約束關系。和域約束一樣,斷言也只能使用CHECK約束。
b.動態(tài)約束:反映數(shù)據(jù)庫狀態(tài)變遷的約束。
2).根據(jù)數(shù)據(jù)庫的實體對象可分為實體本身的完整性和實體之間的參照完整性
?表約束:
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
轉載于:https://www.cnblogs.com/andhm/archive/2010/05/26/1744333.html
總結
- 上一篇: 玩转博客园的5个小技巧
- 下一篇: DataGridView控件初始化,添加