2012-10-02 1 views
4

어떤 인덱스가 고유한지 알아 내기 위해 MySQL 명령어 show indexes from table_name을 사용하려고합니다.MySQL은 인덱스의 유일성을보고하지 않습니까?

mysql> desc books; 
+------------------+--------------+------+-----+---------+-------+ 
| Field   | Type   | Null | Key | Default | Extra | 
+------------------+--------------+------+-----+---------+-------+ 
| id    | int(11)  | NO | PRI | NULL |  | 
| name    | varchar(255) | YES |  | NULL |  | 
| author_id  | int(11)  | YES |  | NULL |  | 
| coauthor_id  | int(11)  | YES |  | NULL |  | 
| publisher_id  | int(11)  | YES |  | NULL |  | 
| isbn    | varchar(255) | YES |  | NULL |  | 
| publication_year | int(11)  | YES |  | NULL |  | 
| shelf_id   | int(11)  | YES |  | NULL |  | 
+------------------+--------------+------+-----+---------+-------+ 
8 rows in set (0.00 sec) 

mysql> show indexes from books; 
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| books |   0 | PRIMARY |   1 | id   | A   |   0 |  NULL | NULL |  | BTREE  |   |    | 
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
1 row in set (0.00 sec) 

mysql> create unique index books_isbn on books (isbn); 
Query OK, 0 rows affected (0.23 sec) 
Records: 0 Duplicates: 0 Warnings: 0 

mysql> show indexes from books; 
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| books |   0 | PRIMARY |   1 | id   | A   |   0 |  NULL | NULL |  | BTREE  |   |    | 
| books |   0 | books_isbn |   1 | isbn  | A   |   0 |  NULL | NULL | YES | BTREE  |   |    | 
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
2 rows in set (0.00 sec) 

mysql> insert into books (id, name, isbn) values (0, 'foo', 'bar'); 
Query OK, 1 row affected, 1 warning (0.04 sec) 

mysql> insert into books (id, name, isbn) values (1, 'foo2', 'bar'); 
ERROR 1062 (23000): Duplicate entry 'bar' for key 'books_isbn' 
mysql> 

show indexesnon_unique 열에서 잘못된 일을보고 않으며, 어떻게 내가 인덱스가 고유 한에 대한 진정한 해답을 얻을 수 있습니까 :하지만 실수로 모든 인덱스가 고유하지 않은 것을보고? 당신은 MySQL의 매뉴얼을 선택하면

(. 이것은 MySQL은 5.5.24이며, 테이블 이노입니다)

+0

wouldnt가'NON_UNIQUE = 0 ' 그들이 실제로 유일하다는 것을 의미합니다 (0 = 거짓)? – prodigitalson

+0

음, 네 말이 맞아, 나는 바보 야. :-) –

+0

하하 ... 일어난다. – prodigitalson

답변

5

은 말한다 :

Non_unique 

0 if the index cannot contain duplicates, 1 if it can. 

확인 MySQL의 Manuals

+0

문서를 인용 할 경우 +1하십시오. – prodigitalson

+0

Upvote에 대한 감사 인사들! – Rush