我們如何建立多列 UNIQUE 索引?
要建立多列 UNIQUE 索引,我們需要在多列上指定一個索引名稱。以下示例將在“empid”、“first_name”、“last_name”列上建立名為“id_fname_lname”的多列索引 -'employee' 表 -
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: 0 mysql> 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: 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' 檔案中的值是相同的,因為我們在表的所有列上建立了多列索引。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式語言
C++
C#
MongoDB
MySQL
Javascript
PHP