欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

必須會的SQL語句(八) 數(shù)據(jù)庫的完整性約束

 更新時(shí)間:2015年01月03日 01:21:11   投稿:mdxy-dxy  
這篇文章主要介紹了sqlserver中數(shù)據(jù)庫的完整性約束使用方法,需要的朋友可以參考下

實(shí)體完整性
1.建表時(shí)定義主鍵

  Create table 表名
   (
        Sno int identity(1,1),
        Sname nvarchar(20),
        --設(shè)置主鍵
        Primary key (Sno)
   )
 
2.添加主鍵

    alter table 表名
    add constraint PK_表名_Sno
    primary key(id)
參照完整性1.建表時(shí)定義外鍵

  create table 表名
  (
      sno int identity(1,1) primary key,
      cno int not null,
      foreign key(cno) References
      表名2(Cno)
      on Delete cascade     --級聯(lián)刪除
      on update cascade    --級聯(lián)更新
      -- on delete on action  刪除管制
  )
 
2.添加外鍵
   alter table 表名
   add constraint FK_表名_表名2
   Foreign key(cid) references 表名2(cid)
用戶定義完整性1.非空約束
   alter table 表名
   alter column name varchar(20) not null
 
2.唯一約束
   alter table 表名
   add constraint UQ_表名_列名 unique(列)
 
3.檢查約束
   alter table 表名
   add constraint CK_表名_列名 check(age>5)
 
4.默認(rèn)約束
   alter table 表名
   add constraint DF_表名_列名 default('男')
   for gender
刪除約束    --刪除約束
   alter table 表名 drop constraint DF_表名_列

相關(guān)文章

最新評論