我们如何创建多列UNIQUE索引?
为了创建多列UNIQUE索引,我们需要在多个列上指定索引名称。以下示例将在'employee'表的'empid','first_name','last_name'列上创建一个名为'id_fname_lname'的多列索引-
Create UNIQUE INDEX id_fname_lname on employee(empid,first_name,last_name);Records: 0 Duplicates: 0 Warnings: 0
describe employee;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| empid | int(11) | YES | MUL | NULL | |
| first_name | varchar(20) | YES | | NULL | |
| last_name | varchar(20) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.12 sec)
从上面查询的结果集中,我们可以看到在表上定义了多个索引。忘记有关索引的详细信息,我们可以运行以下查询-
Show index from employee\G*************************** 1. row ***************************
Table: employee
Non_unique: 0
Key_name: id_fname_lname
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: id_fname_lname
Seq_in_index: 2
Column_name: first_name
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: id_fname_lname
Seq_in_index: 3
Column_name: last_name
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
3 rows in set (0.00 sec)
从上面的结果集中我们可以看到,'key_name'字段中的值是相同的,因为我们已经在表的所有列上创建了多列索引。
以上是 我们如何创建多列UNIQUE索引? 的全部内容, 来源链接: utcz.com/z/327248.html