mysql> create unique index id_fname_lname on employee(empid,first_name,last_name);query ok, 0 rows affected (0.41 sec)records: 0 duplicates: 0 warnings: 0mysql> 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)
从上面查询的结果集中,我们可以看到表上定义了多重索引。忘记有关索引的详细信息,我们可以运行以下查询 -
mysql> show index from employee\g*************************** 1. row ***************************table: employeenon_unique: 0key_name: id_fname_lnameseq_in_index: 1column_name: empidcollation: acardinality: 0sub_part: nullpacked: nullnull: yesindex_type: btreecomment:index_comment:*************************** 2. row ***************************table: employeenon_unique: 0key_name: id_fname_lnameseq_in_index: 2column_name: first_namecollation: acardinality: 0sub_part: nullpacked: nullnull: yesindex_type: btreecomment:index_comment:*************************** 3. row ***************************table: employeenon_unique: 0key_name: id_fname_lnameseq_in_index: 3column_name: last_namecollation: acardinality: 0sub_part: nullpacked: nullnull: yesindex_type: btreecomment:index_comment:3 rows in set (0.00 sec)
从上面的结果集中可以看出,‘key_name’字段中的值是相同的,因为我们已经在表的所有列上创建了多列索引。
以上就是我们如何创建多列unique索引?的详细内容。
