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_表名_列
     
     
  • 相关阅读:
    Go interface{}、类型断言
    相关资料
    php实践
    安装zookeeper
    对象池化,对象池
    java getResourcesAsStream()如何获取WEB-INF下的文件流
    android--SDK Manager下载Connection to http://dl-ssl.google.com refused
    Intellij idea 切换SVN路径
    Intellij Idea @Autowired取消提示
    恢复文件默认打开方式
  • 原文地址:https://www.cnblogs.com/mcad/p/4195445.html
Copyright © 2011-2022 走看看