背景

演示唯一约束怎样创建、删除、禁用和使用唯一性约束,已经多种数据库的差异。

什么是唯一约束

唯一性约束指表中一个字段或者多个字段联合起来可以唯一标识一条记录的约束, 字段中,可以包括空值。

唯一性约束能够在创建表时或使用ALTER TABLE语句创建。

唯一约束与唯一索引的区别

  • 唯一约束和唯一索引,都可以实现列数据的唯一,列值可以有null。
  • 创建唯一约束,会自动创建一个同名的唯一索引,该索引不能单独删除,删除约束会自动删除索引。唯一约束是通过唯一索引来实现数据的唯一。
  • 创建一个唯一索引,这个索引就是独立,可以单独删除。
  • 如果表的一个字段,要作为另外一个表的外键,这个字段必须有唯一约束(或是主键),如果只是有唯一索引,就会报错。

唯一约束和主键的区别

  • 主键(Primary Key):全部组成主键的列都不能包括空值。
  • 唯一性约束(Unique Constraint):假设唯一性约束由多列组成,当中的部分列能够包括空值。
  • 主键和唯一性约束自动建立了同名唯一性索引。
  • 一个表只能有一个主键,但可以有多个约束。

KingbaseES的唯一约束

  1. 单列约束

    kingbase=# create table t1 (c1 int, c2 int);
    CREATE TABLE
    kingbase=# alter table t1 add constraint t1_u1 unique (c1);
    ALTER TABLE
    kingbase=# insert into t1 values (1, 1);
    INSERT 0 1
    kingbase=# insert into t1 values (1, 2);
    错误: 重复键违反唯一约束"t1_u1"
    描述: 键值"(c1)=(1)" 已经存在
    kingbase=# insert into t1 values (null, 2);
    INSERT 0 1
    kingbase=# insert into t1 values (null, 3);
    INSERT 0 1
    kingbase=# select * from t1;
    c1 | c2
    ----+----
    1 | 1
    | 2
    | 3
    (3 行记录)

    KingbaseES的单列唯一约束,不检查null值的数据。

  2. 多列约束

    kingbase=# create table t1 (c1 int, c2 int);
    CREATE TABLE
    kingbase=# alter table t1 add constraint t1_u1 unique (c1, c2);
    ALTER TABLE
    kingbase=# insert into t1 values (1, 1);
    INSERT 0 1
    kingbase=# insert into t1 values (1, 1);
    错误: 重复键违反唯一约束"t1_u1"
    描述: 键值"(c1, c2)=(1, 1)" 已经存在
    kingbase=# insert into t1 values (null, 2);
    INSERT 0 1
    kingbase=# insert into t1 values (null, 2);
    INSERT 0 1
    kingbase=# insert into t1 values (3, null);
    INSERT 0 1
    kingbase=# insert into t1 values (3, null);
    INSERT 0 1
    kingbase=# insert into t1 values (null, null);
    INSERT 0 1
    kingbase=# insert into t1 values (null, null);
    INSERT 0 1
    kingbase=# select * from t1 ;
    c1 | c2
    ----+----
    1 | 1
    | 2
    | 2
    3 |
    3 |
    |
    |
    (7 行记录)

    KingbaseES的多列唯一约束,不检查含有null值的数据。如果某个约束列是null值,则不能保证记录的唯一。

  3. 使用唯一索引建立约束

    kingbase=# create table t1 (c1 int, c2 int);
    CREATE TABLE
    kingbase=# create unique index t1_c1 on t1 (c1);
    CREATE INDEX
    kingbase=# select indexrelid,indexrelid::regclass::text from pg_index where indrelid='t1'::regclass;
    indexrelid | indexrelid
    ------------+------------
    212572 | t1_c1
    (1 行记录) kingbase=# alter table t1 add constraint t1_u1 unique using index t1_c1;
    注意: ALTER TABLE / ADD CONSTRAINT USING INDEX 会把索引 "t1_c1" 重命名为 "t1_u1"
    ALTER TABLE
    kingbase=# select indexrelid,indexrelid::regclass::text from pg_index where indrelid='t1'::regclass;
    indexrelid | indexrelid
    ------------+------------
    212572 | t1_u1
    (1 行记录) kingbase=# drop index t1_c1;
    错误: 索引 "t1_c1" 不存在
    kingbase=# drop index t1_u1;
    错误: 无法删除 索引 t1_u1, 因为 在表 t1上的约束t1_u1 需要它
    提示: 您也可以删除 在表 t1上的约束t1_u1 代替.
    kingbase=# alter table t1 drop constraint t1_u1;
    ALTER TABLE
    kingbase=# select indexrelid,indexrelid::regclass::text from pg_index where indrelid='t1'::regclass;
    indexrelid | indexrelid
    ------------+------------
    (0 行记录)

    已存在唯一索引,被用于唯一约束之后,索引名会被修改为约束名。唯一索引同时不能单独被删除,删除唯一约束的同时,删除其使用的索引。

  4. 外键


    kingbase=# create table t1 (c1 int, c2 int);
    CREATE TABLE
    kingbase=# alter table t1 add constraint t1_u1 unique (c1);
    ALTER TABLE
    kingbase=# create unique index t1_c2 on t1 (c2);
    CREATE INDEX
    kingbase=# create table t2 (c1 int, c2 int);
    CREATE TABLE
    kingbase=# alter table t2 add constraint fk_c1 foreign key(c1) references t1(c1);
    ALTER TABLE
    kingbase=# alter table t2 add constraint fk_c2 foreign key(c2) references t1(c2);
    错误: there is no unique constraint matching given keys for referenced table "t1"
    kingbase=#

    外键,只能参考唯一约束,不能参考唯一索引。

MySQL的唯一约束

  1. 单列约束


    mysql> create table t1 (c1 int, c2 int);
    Query OK, 0 rows affected (0.01 sec) mysql> alter table t1 add constraint t1_u1 unique (c1);
    Query OK, 0 rows affected (0.01 sec)
    Records: 0 Duplicates: 0 Warnings: 0 mysql> insert into t1 values (1, 1);
    Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (1, 2);
    ERROR 1062 (23000): Duplicate entry '1' for key 't1.t1_u1'
    mysql> insert into t1 values (null, 2);
    Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (null, 3);
    Query OK, 1 row affected (0.00 sec) mysql> select * from t1;
    +------+------+
    | c1 | c2 |
    +------+------+
    | 1 | 1 |
    | NULL | 2 |
    | NULL | 3 |
    +------+------+
    3 rows in set (0.00 sec) mysql>

    MySQL的单列唯一约束,不检查null值的数据。

  2. 多列约束


    mysql> create table t1 (c1 int, c2 int);
    Query OK, 0 rows affected (0.01 sec) mysql> alter table t1 add constraint t1_u1 unique (c1);
    Query OK, 0 rows affected (0.01 sec)
    Records: 0 Duplicates: 0 Warnings: 0 mysql> insert into t1 values (1, 1);
    Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (1, 2);
    ERROR 1062 (23000): Duplicate entry '1' for key 't1.t1_u1'
    mysql> insert into t1 values (null, 2);
    Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (null, 3);
    Query OK, 1 row affected (0.00 sec) mysql> select * from t1;
    +------+------+
    | c1 | c2 |
    +------+------+
    | 1 | 1 |
    | NULL | 2 |
    | NULL | 3 |
    +------+------+
    3 rows in set (0.00 sec) mysql> drop table t1;
    Query OK, 0 rows affected (0.01 sec) mysql> create table t1 (c1 int, c2 int);
    Query OK, 0 rows affected (0.01 sec) mysql> alter table t1 add constraint t1_u1 unique (c1, c2);
    Query OK, 0 rows affected (0.01 sec)
    Records: 0 Duplicates: 0 Warnings: 0 mysql> insert into t1 values (1, 1);
    Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (1, 1);
    ERROR 1062 (23000): Duplicate entry '1-1' for key 't1.t1_u1'
    mysql> insert into t1 values (null, 2);
    Query OK, 1 row affected (0.01 sec) mysql> insert into t1 values (null, 2);
    Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (3, null);
    Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (3, null);
    Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (null, null);
    Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (null, null);
    Query OK, 1 row affected (0.00 sec) mysql> select * from t1 ;
    +------+------+
    | c1 | c2 |
    +------+------+
    | NULL | NULL |
    | NULL | NULL |
    | NULL | 2 |
    | NULL | 2 |
    | 1 | 1 |
    | 3 | NULL |
    | 3 | NULL |
    +------+------+
    7 rows in set (0.00 sec) mysql>

    MySQL的多列唯一约束,不检查含有null值的数据。如果某个约束列是null值,则不能保证记录的唯一。

  3. 使用唯一索引建立约束


    mysql> create table t1 (c1 int, c2 int);
    Query OK, 0 rows affected (0.01 sec) mysql> create unique index t1_c1 on t1 (c1);
    Query OK, 0 rows affected (0.01 sec)
    Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from t1;
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
    | t1 | 0 | t1_c1 | 1 | c1 | A | 0 | NULL | NULL | YES | BTREE | | | YES | NULL |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
    1 row in set (0.00 sec) mysql> alter table t1 add constraint t1_u1 unique (c1);
    Query OK, 0 rows affected, 1 warning (0.01 sec)
    Records: 0 Duplicates: 0 Warnings: 1 mysql> show index from t1;
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
    | t1 | 0 | t1_c1 | 1 | c1 | A | 0 | NULL | NULL | YES | BTREE | | | YES | NULL |
    | t1 | 0 | t1_u1 | 1 | c1 | A | 0 | NULL | NULL | YES | BTREE | | | YES | NULL |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
    2 rows in set (0.01 sec) mysql> alter table t1 drop constraint t1_u1;
    Query OK, 0 rows affected (0.01 sec)
    Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from t1;
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
    | t1 | 0 | t1_c1 | 1 | c1 | A | 0 | NULL | NULL | YES | BTREE | | | YES | NULL |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
    1 row in set (0.01 sec) mysql>

    已存在唯一索引,不会用于唯一约束,唯一约束会创建自用的唯一索引。

  4. 外键


    mysql> create table t1 (c1 int, c2 int);
    Query OK, 0 rows affected (0.01 sec) mysql> alter table t1 add constraint t1_u1 unique (c1);
    Query OK, 0 rows affected (0.01 sec)
    Records: 0 Duplicates: 0 Warnings: 0 mysql> create unique index t1_c2 on t1 (c2);
    Query OK, 0 rows affected (0.01 sec)
    Records: 0 Duplicates: 0 Warnings: 0 mysql> create table t2 (c1 int, c2 int);
    Query OK, 0 rows affected (0.01 sec) mysql> alter table t2 add constraint fk_c1 foreign key(c1) references t1(c1);
    Query OK, 0 rows affected (0.03 sec)
    Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table t2 add constraint fk_c2 foreign key(c2) references t1(c2);
    Query OK, 0 rows affected (0.02 sec)
    Records: 0 Duplicates: 0 Warnings: 0 mysql> SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_NAME = 't2';
    +-----------------+-----------------+
    | CONSTRAINT_NAME | CONSTRAINT_TYPE |
    +-----------------+-----------------+
    | fk_c1 | FOREIGN KEY |
    | fk_c2 | FOREIGN KEY |
    +-----------------+-----------------+
    2 rows in set (0.00 sec) mysql>

    外键,既可以参考唯一约束,也可以参考唯一索引。

Oracle的唯一约束

  1. 单列约束

    SQL> create table t1 (c1 int, c2 int);
    
    Table created.
    
    SQL> alter table t1 add constraint t1_u1 unique (c1);
    
    Table altered.
    
    SQL> select INDEX_NAME from SYS.USER_INDEXES where TABLE_NAME='T1';
    
    INDEX_NAME
    --------------------------------------------------------------------------------
    T1_U1 SQL> insert into t1 values (1, 1); 1 row created. SQL> insert into t1 values (1, 2);
    insert into t1 values (1, 1)
    *
    ERROR at line 1:
    ORA-00001: unique constraint (SCH01.T1_U1) violated SQL> insert into t1 values (null, 2); 1 row created. SQL> insert into t1 values (null, 3); 1 row created. SQL> select * from t1; C1 C2
    ---------- ----------
    1 1
    NULL 2
    NULL 3 3 rows selected. SQL> alter table t1 drop constraint t1_u1; Table altered. SQL> select INDEX_NAME from SYS.USER_INDEXES where TABLE_NAME='T1'; no rows selected

    Oracle的单列唯一约束,不检查null值的数据。

  2. 多列约束


    SQL> create table t1 (c1 int, c2 int); Table created. SQL> alter table t1 add constraint t1_u1 unique (c1, c2); Table altered. SQL> insert into t1 values (1, 1); 1 row created. SQL> insert into t1 values (1, 1);
    insert into t1 values (1, 1)
    *
    ERROR at line 1:
    ORA-00001: unique constraint (SCH01.T1_U1) violated SQL> insert into t1 values (null, 2); 1 row created. SQL> insert into t1 values (null, 2);
    insert into t1 values (null, 2)
    *
    ERROR at line 1:
    ORA-00001: unique constraint (SCH01.T1_U1) violated SQL> insert into t1 values (3, null); 1 row created. SQL> insert into t1 values (3, null);
    insert into t1 values (3, null)
    *
    ERROR at line 1:
    ORA-00001: unique constraint (SCH01.T1_U1) violated SQL> insert into t1 values (null, null); 1 row created. SQL> insert into t1 values (null, null); 1 row created. SQL> select * from t1 ; C1 C2
    ---------- ----------
    1 1
    NULL 2
    3 NULL
    NULL NULL
    NULL NULL 5 rows selected.

    Oracle的多列唯一约束,不检查约束列都是null值的数据。如果全部约束列是null值,则不能保证记录的唯一。

  3. 使用唯一索引建立约束


    SQL> create table t1 (c1 int, c2 int); Table created. SQL> create unique index t1_c1 on t1 (c1); Index created. SQL>
    select INDEX_NAME from SYS.USER_INDEXES where TABLE_NAME='T1';SQL> INDEX_NAME
    --------------------------------------------------------------------------------
    T1_C1 SQL> select INDEX_NAME from SYS.USER_INDEXES where TABLE_NAME='T1'; INDEX_NAME
    --------------------------------------------------------------------------------
    T1_C1 SQL> alter table t1 add constraint t1_u1 unique (c1); Table altered. SQL> select INDEX_NAME from SYS.USER_INDEXES where TABLE_NAME='T1'; INDEX_NAME
    --------------------------------------------------------------------------------
    T1_C1 SQL> drop index t1_c1;
    drop index t1_c1
    *
    ERROR at line 1:
    ORA-02429: cannot drop index used for enforcement of unique/primary key SQL> alter table t1 drop constraint t1_u1; Table altered. SQL> select INDEX_NAME from SYS.USER_INDEXES where TABLE_NAME='T1'; INDEX_NAME
    --------------------------------------------------------------------------------
    T1_C1

    已存在唯一索引,被用于唯一约束之后,索引名不会改变。同时不能单独被删除。唯一索引同时不能单独被删除,删除唯一约束的同时,不会删除其使用的索引。

  4. 外键

    SQL> create table t1 (c1 int, c2 int);
    
    Table created.
    
    SQL> alter table t1 add constraint t1_u1 unique (c1);
    
    Table altered.
    
    SQL> create unique index t1_c2 on t1 (c2);
    
    Index created.
    
    SQL> create table t2 (c1 int, c2 int);
    
    Table created.
    
    SQL> alter table t2 add constraint fk_c1 foreign key(c1) references t1(c1);
    
    Table altered.
    
    SQL> alter table t2 add constraint fk_c2 foreign key(c2) references t1(c2);
    alter table t2 add constraint fk_c2 foreign key(c2) references t1(c2)
    *
    ERROR at line 1:
    ORA-02270: no matching unique or primary key for this column-list SQL>

    外键,只能参考唯一约束,不能参考唯一索引。

SQLserver的唯一约束

  1. 单列约束


    1> create table t1 (c1 int, c2 int);
    2> go
    1> alter table t1 add constraint t1_u1 unique (c1);
    2> go
    1> insert into t1 values (1, 1);
    2> go (1 rows affected)
    1> insert into t1 values (1, 2);
    2> go
    Msg 2627, Level 14, State 1, Server buxspc, Line 1
    违反了 UNIQUE KEY 约束“t1_u1”。不能在对象“dbo.t1”中插入重复键。重复键值为 (1)。
    语句已终止。
    1> insert into t1 values (null, 2);
    2> go (1 rows affected)
    1> insert into t1 values (null, 3);
    2> go
    Msg 2627, Level 14, State 1, Server buxspc, Line 1
    违反了 UNIQUE KEY 约束“t1_u1”。不能在对象“dbo.t1”中插入重复键。重复键值为 (<NULL>)。
    语句已终止。
    1> select * from t1;
    2> go
    c1 c2
    ----------- -----------
    1 1
    NULL 2 (2 rows affected)
    1>

    SQLserver的单列唯一约束,检查null值的数据。

  2. 多列约束


    1> create table t1 (c1 int, c2 int);
    2> go
    1> alter table t1 add constraint t1_u1 unique (c1, c2);
    2> go
    1> insert into t1 values (1, 1);
    2> go (1 rows affected)
    1> insert into t1 values (1, 1);
    2> go
    Msg 2627, Level 14, State 1, Server buxspc, Line 1
    违反了 UNIQUE KEY 约束“t1_u1”。不能在对象“dbo.t1”中插入重复键。重复键值为 (1, 1)。
    语句已终止。
    1> insert into t1 values (null, 2);
    2> go (1 rows affected)
    1> insert into t1 values (null, 2);
    2> go
    Msg 2627, Level 14, State 1, Server buxspc, Line 1
    违反了 UNIQUE KEY 约束“t1_u1”。不能在对象“dbo.t1”中插入重复键。重复键值为 (<NULL>, 2)。
    语句已终止。
    1> insert into t1 values (null, null);
    2> go (1 rows affected)
    1> insert into t1 values (null, null);
    2> go
    Msg 2627, Level 14, State 1, Server buxspc, Line 1
    违反了 UNIQUE KEY 约束“t1_u1”。不能在对象“dbo.t1”中插入重复键。重复键值为 (<NULL>, <NULL>)。
    语句已终止。
    1> select * from t1 ;
    2> go
    c1 c2
    ----------- -----------
    NULL NULL
    NULL 2
    1 1 (3 rows affected)
    1>

    SQLserver的多列唯一约束,检查含有null值的数据。把null值当做有效值,保证记录的唯一。

  3. 使用唯一索引建立约束

    1> create table t1 (c1 int, c2 int);
    2> go
    1> create unique index t1_c1 on t1 (c1);
    2> go
    1> SELECT name FROM sys.indexes where object_id = (select object_id from sys.all_objects where name = 't1' ) and index_id >0;
    2> go
    name
    --------------------------------------------------------------------------------------------------------------------------------
    t1_c1 (1 rows affected)
    1> alter table t1 add constraint t1_U1 unique (c1);
    2> go
    1> SELECT name FROM sys.indexes where object_id = (select object_id from sys.all_objects where name = 't1' ) and index_id >0;
    2> go
    name
    --------------------------------------------------------------------------------------------------------------------------------
    t1_c1
    t1_U1 (2 rows affected)
    1> alter table t1 drop constraint t1_U1;
    2> go
    1> SELECT name FROM sys.indexes where object_id = (select object_id from sys.all_objects where name = 't1' ) and index_id >0;
    2> go
    name
    --------------------------------------------------------------------------------------------------------------------------------
    t1_c1 (1 rows affected)
    1>

    已存在唯一索引,不能被用于唯一约束,唯一约束会创建自用的唯一索引。

  4. 外键


    1> create table t1 (c1 int, c2 int);
    2> go
    1> alter table t1 add constraint t1_u1 unique (c1);
    2> go
    1> create unique index t1_c2 on t1 (c2);
    2> go
    1> create table t2 (c1 int, c2 int);
    2> go
    1> alter table t2 add constraint fk_c1 foreign key(c1) references t1(c1);
    2> go
    1> alter table t2 add constraint fk_c2 foreign key(c2) references t1(c2);
    2> go
    1> SELECT f.name AS foreign_key_name ,OBJECT_NAME(f.parent_object_id) AS table_name FROM sys.foreign_keys AS f WHERE f.parent_object_id = OBJECT_ID('t2');
    2> go
    foreign_key_name table_name
    ------------------------- ------------------
    fk_c1 t2
    fk_c2 t2 (2 rows affected)
    1>

    外键,既可以参考唯一约束,也可以参考唯一索引。

总结

null值处理

  • KingbaseES、 MySQL :只要约束列中包含null值,则不进行约束检查。如果某个约束列是null值,则不能保证记录的唯一。
  • Oracle : 全部约束列都是null值,则不进行约束检查。如果全部约束列是null值,则不能保证记录的唯一。
  • SqlServer : 约束列的null值,也精细约束检查。null值也被视为有效值,保证记录的唯一。

已存在唯一索引处理

  • KingbaseES、Oracle :唯一约束可以关联到已存在的索引。
  • MySQL 、SqlServer :唯一约束不能关联到已存在的索引。

外键关联处理

  • KingbaseES、Oracle :只能参考唯一约束,不能参考唯一索引。
  • MySQL 、SqlServer :既可以参考唯一约束,也可以参考唯一索引。****

表的唯一约束的作用 KingbaseES VS Oracle的更多相关文章

  1. mysql 给表添加唯一约束、联合唯一约束,指定唯一约束的名字

    表结构 FIELD          TYPE          COLLATION       NULL    KEY     DEFAULT  Extra           PRIVILEGES ...

  2. Sql Server约束的学习一(主键约束、外键约束、唯一约束)

    一.约束的分类 1.实体约束 实体约束是关于行的,比如某一行出现的值不允许出现在其他行,例如主键约束. 2.域约束 域约束是关于列的,对于所有行,某一列有那些约束,例如检查约束. 3.参照完整性约束 ...

  3. hibernate中怎样配置两个联合属性为唯一约束(非联合主键)

    Annotation中配置: @Table元素包括了一个schema和一个catalog属性,如果需要可以指定相应的值. 结合使用@UniqueConstraint注解可以定义表的唯一约束(uniqu ...

  4. Oracle 数据库表中已有重复数据添加唯一键(唯一约束)

    Oracle 数据库表中已有重复数据添加唯一键(唯一约束) 问题描述 以 demo 举例,模拟真实场景. 表 TEST_TABLE 有如下字段和数据:id 是主键,code 没有设置键和索引 ID C ...

  5. SQLServer中给表增加组合唯一约束

    将两个或者多个字段一起约束成一个唯一约束 alter table 表名 add constraint 约束名 unique (列名1,列名2)

  6. [置顶] T-sql sql server 设置主键约束、标示列、唯一约束、默认值、约束、创建表

    ----选择数据库 use ythome go ----查看表是否存在 if Exists ( select * from sysobjects where name='sys_menu' and t ...

  7. db2 将原表列notnull属性修改为null属性的方法 (查看主键约束,唯一约束去syscat.tabconst)

    好久没机会写点东西了,今天把自己遇到的一个小问题跟大家分享一下如何修改db2数据库表中列的属性--将列的非空属性改为允许空的属性,修改数据表的某一列属性其实很简单但是里面有需要细节需要dba注意,毕竟 ...

  8. SQL Server(第一章) 创建表 删除表 创建主键约束、唯一约束、外键约束、CHECK约束、默认约束

    1.Employees员工表 /** 创建Employees员工表 **/ USE TSQL2012 IF OBJECT_ID('dbo.Employees','U') IS NOT NULL DRO ...

  9. MySQL进阶13--常见六大约束: 非空/默认/主键/唯一约束/检查约束/外键约束--表级约束 / 列级约束

    /* MySQL进阶13 常见六大约束: 1.not null 非空 2.default :默认值,用于保证该字段的默认值 ; 比如年龄:1900-10-10 3.primary key : 主键,用 ...

  10. MySQL常见建表选项以约束

    一.CREATE TABLE 选项 1.在定义列的时候,指定列选项 1)DEFAULT <literal>:定义列的默认值 当插入一个新行到表中并且没有给该列明确赋值时,如果定义了列的默认 ...

随机推荐

  1. JavaScript选择器

    Js选择器 JS选择器常用的有getElementById().getElementsByClassName().getElementsByName().getElementsByTagName(). ...

  2. MVVM模式的理解

    MVVM模式的理解 MVVM全称Model-View-ViewModel是基于MVC和MVP体系结构模式的改进,MVVM就是MVC模式中的View的状态和行为抽象化,将视图UI和业务逻辑分开,更清楚地 ...

  3. Laravel入坑指南(7)——中间件Middleware

    Laravel框架中引入了"中间件"这个概念,笔者觉得不是太合适.这里的Middleware和Java Servlet中的过滤器(Filter)就是一个东西,但是想比之下Filte ...

  4. B - Bracket Sequence题解

    B - Bracket Sequence 思路: 用一个flag来标记括号的数目,如果括号数目是个偶数的话,就代表当前要执行'+'操作,反之就是'*'操作.对于最外层的数,是没有计算的. 所以最后要单 ...

  5. G water testing题解

    G water testing 题意:给你一个多边形(可能是凸多边形,也可能是凹多边形),问该多边形内有多少个整数点(不包含边界). 思路:皮克定理 + 叉乘计算三角形面积:皮克定理是指一个计算点阵中 ...

  6. 面试官:你知道Comparable 和 Comparator 的区别吗?我:巴拉巴拉

    写在开头 面试官:"我们在Java的集合和数据结构中都离不开比较器,请你聊一聊Comparable 和 Comparator 这两种的区别吧" 内心活动:"上来就这么直接 ...

  7. 硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件

    前言   有了原理图,可以设计硬件PCB,在设计PCB之间还有一个协同优先动作,就是映射封装,原理图库的元器件我们是自己设计的.为了更好的表述封装设计过程,本文描述了一个创建asm1117-3.3V封 ...

  8. Java JVM——6.本地方法接口

    本地方法接口 什么是本地方法? 简单地讲,一个 Native Method 就是一个Java调用非Java代码的接囗.一个 Native Method 是这样一个Java方法:该方法的实现由非Java ...

  9. 狂神说Git学习笔记整理

    Git 版本控制 ​ 在开发过程中,项目会进行版本迭代,新版本会取代旧版本,但是我们不希望直接删除旧版本,所以就需要一个版本管理器来管理新旧版本,不然就是手动控制... 多人开发必须使用版本控制!!! ...

  10. AI 让观众成为 3D 版《老友记》的导演了?

    <老友记>上线 3D 版了? 允许用户旋转镜头,且从近景切换到全景观看故事? 今年出炉的 3D 方向 AI 项目 SitCom3D,能够自动补齐<老友记>原剧中的三维拍摄空间, ...