• 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  删除管制

      )

    添加外键

       alter table 表名

       add constraint FK_表名_表名2

       Foreign key(cid) references 表名2(cid)

    用户定义完整性1.非空约束

       alter table 表名

       alter column name varchar(20) not null

    3.唯一约束

       alter table 表名

       add constraint UQ_表名_列名 unique(列)

    4.检查约束

       alter table 表名

       add constraint CK_表名_列名 check(age>5)

    5.默认约束

       alter table 表名

       add constraint DF_表名_列名 default('男')

       for gender

    6.删除约束 

      alter table 表名 drop constraint DF_表名_列

  • 相关阅读:
    Codeforces 812E Sagheer and Apple Tree
    bzoj 4765: 普通计算姬
    bzoj 4552: [Tjoi2016&Heoi2016]排序
    bzoj 1096: [ZJOI2007]仓库建设
    bzoj 1030: [JSOI2007]文本生成器
    bzoj 1095: [ZJOI2007]Hide 捉迷藏
    JS实现HashMap
    A4纸表格打印
    JAVA字符串格式化-String.format()的使用
    证书打印CSS知识点总结
  • 原文地址:https://www.cnblogs.com/LoveSuk/p/5733234.html
Copyright © 2020-2023  润新知