国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

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

2024-08-31 01:02:47
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了sqlserver中數據庫的完整性約束使用方法,需要的朋友可以參考下
 
 

實體完整性
1.建表時定義主鍵

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

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

  create table 表名
  (
      sno int identity(1,1) primary key,
      cno int not null,
      foreign key(cno) References
      表名2(Cno)
      on Delete cascade     --級聯刪除
      on update cascade    --級聯更新
      -- 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.默認約束
   alter table 表名
   add constraint DF_表名_列名 default('男')
   for gender
刪除約束    --刪除約束
   alter table 表名 drop constraint DF_表名_列


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 电白县| 台东县| 五原县| 新源县| 博客| 呼和浩特市| 华容县| 城固县| 法库县| 宁城县| 商洛市| 昆明市| 嘉荫县| 德惠市| 珠海市| 启东市| 汕尾市| 惠安县| 成安县| 江川县| 松阳县| 潞西市| 宝兴县| 南城县| 嘉兴市| 佳木斯市| 乌兰浩特市| 保靖县| 钦州市| 广灵县| 泊头市| 永城市| 柏乡县| 吉林市| 建瓯市| 双峰县| 措美县| 富锦市| 泰和县| 明光市| 左云县|