如果我将多次在同一列上添加UNIQUE约束会怎样?

当我们多次在同一列上添加UNIQUE约束时,MySQL将多次在该列上创建索引,我们已经添加了UNIQUE约束。

示例

假设我们有一个表“ employee”,其中在“ empid”列上有UNIQUE约束。可以从以下查询中检查-

mysql> Describe employee;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| empid      | int(11)     | YES  | UNI | NULL    |       |
| first_name | varchar(20) | YES  |     | NULL    |       |
| last_name  | varchar(20) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.12 sec)

现在,当我们运行查询SHOW INDEX时,它给出索引的名称,只有一个索引在列'empid'上创建。

mysql> Show index from employee\G;
*************************** 1. row ***************************
        Table: employee
   Non_unique: 0
     Key_name: empid
 Seq_in_index: 1
  Column_name: empid
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
1 row in set (0.00 sec)

借助以下查询,我们在同一列“ empid”上又添加了一个UNIQUE约束-

mysql> Alter table employee ADD UNIQUE(empid);
Records: 0 Duplicates: 0 Warnings: 0

现在,当我们运行查询SHOW INDEX时,它给出了索引的名称,即在列“ empid”上创建的两个索引“ empid”和“ empid_2”。

mysql> Show index from employee12\G;
*************************** 1. row ***************************
        Table: employee
   Non_unique: 0
     Key_name: empid
 Seq_in_index: 1
  Column_name: empid
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 2. row ***************************
        Table: employee
   Non_unique: 0
     Key_name: empid_2
 Seq_in_index: 1
  Column_name: empid
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
2 rows in set (0.00 sec)

借助以下查询,我们在同一列“ empid”上又添加了一个UNIQUE约束-

mysql> Alter table employee ADD UNIQUE(empid);
Records: 0 Duplicates: 0 Warnings: 0

现在,当我们运行查询SHOW INDEX时,它将给出索引的名称,即在“ empid”列上创建的三个索引“ empid”和“ empid_2”,“ empid_3”。

mysql> Alter table employee ADD UNIQUE(empid);
Records: 0 Duplicates: 0 Warnings: 0

mysql> Show index from employee\G;
*************************** 1. row ***************************
        Table: employee
   Non_unique: 0
     Key_name: empid
 Seq_in_index: 1
  Column_name: empid
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 2. row ***************************
        Table: employee
   Non_unique: 0
     Key_name: empid_2
 Seq_in_index: 1
  Column_name: empid
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
*************************** 3. row ***************************
        Table: employee
   Non_unique: 0
     Key_name: empid_3
 Seq_in_index: 1
  Column_name: empid
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
3 rows in set (0.00 sec)

从这个意义上讲,我们可以说MySQL将在该列上添加UNIQUE约束的次数上创建该列的唯一索引。

猜你喜欢