首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL不报告索引唯一性吗?

MySQL不报告索引唯一性吗?
EN

Stack Overflow用户
提问于 2012-10-03 03:45:04
回答 1查看 86关注 0票数 4

我尝试使用MySQL命令show indexes from table_name来找出哪些索引是惟一的。但它错误地报告了所有索引都不是唯一的:

代码语言:javascript
复制
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 5.5.24,表是InnoDB。)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-03 03:50:20

如果你查看MySQL手册,它会说:

代码语言:javascript
复制
 Non_unique

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

检查MySQL Manuals

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12697177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档