• SQL增加约束


    1.主键约束:

    格式为:
    alter table 表格名称 add constraint 约束别名    约束类型 (列名)

    例子:
    alter table table_name add constraint abc000  primary key (id);

    2.check约束:就是给一列的数据进行了限制
    格式:
    alter table 表名称 add constraint 约束别名    约束类型 (列名)

    例子:
    alter table table_name   add constraint abc001 check(age>20);

    3.unique约束:这样的约束就是给列的数据追加的不重复的约束类型

    格式:
    alter table 表名 add constraint 约束别名     约束类型(列名)
    例子:
    alter table table_name add constraint abc002 unique(class);

    4.默认约束:意思很简单就是为此列的数据设置默认值

    格式:
    alter table 表名称 add constraint    约束别名    约束类型   默认值   for  列名

    例子:

    alter table table_name add constraint   abc003     default  99  for score;

    5.外键约束:即当前表的外键,引用外面另一个表的主键,每个值唯一且对应;要建立外键关系,首先要保证用来建立外键关系的列具有唯一性,即具有 UNIQUE 约束

    格式:
    alter table 表名 add constraint   约束别名   约束类型 (列名)   references   引用的表名称 (列名)

    例子:
    alter table table_name add constraint   abc004   foreign key (did)    references   dept (id);

    6. 非空约束:

      即是设置一列值不空

        ALTER TABLE TABLE_NAME     ALTER COLUMN COLUMN_NAME   TYPE_OF_  NOT NULL;
  • 相关阅读:
    pycharm的常规使用
    python-引用/模块
    6-4 函数
    5-21文件的操作
    5-21python数据类型
    python-基础
    5-7接口测试工具之jmeter的使用
    接口测试基础
    把命令结果作为变量赋值
    shell变量子串
  • 原文地址:https://www.cnblogs.com/bernard-shen/p/13159179.html
Copyright © 2020-2023  润新知