zoukankan      html  css  js  c++  java
  • 必须会的SQL语句(八)数据库的完整性约束

    实体完整性

    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_表名_列
     
     
  • 相关阅读:
    POJ 3126 Prime Path
    POJ 2429 GCD & LCM Inverse
    POJ 2395 Out of Hay
    【Codeforces 105D】 Bag of mice
    【POJ 3071】 Football
    【POJ 2096】 Collecting Bugs
    【CQOI 2009】 余数之和
    【Codeforces 258E】 Devu and Flowers
    【SDOI 2010】 古代猪文
    【BZOJ 2982】 combination
  • 原文地址:https://www.cnblogs.com/mcad/p/4195445.html
Copyright © 2011-2022 走看看