最新消息:20210917 已从crifan.com换到crifan.org

[已解决]如何确定MySQL的表中哪些key是unique的

MySQL crifan 2138浏览 0评论

折腾:

[已解决]去除MySQL中某个表中某几列重复的数据

后,想要得知mysql中表的某几个key是unique的?

搜:

mysql check if unique

mysql check whether unique

Check whether a field has the property `UNIQUE` in mysql and PHP – Stack Overflow

MySQL :: MySQL 5.0 Reference Manual :: 13.7.5.18 SHOW INDEX Syntax

unique – check duplicate mysql row – Stack Overflow

MariaDB [guanzhi_removeDuplicate]> SHOW INDEXES FROM selection;
+———–+————+———-+————–+————-+———–+————-+———-+——–+——+————+———+—————+
| Table     | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+———–+————+———-+————–+————-+———–+————-+———-+——–+——+————+———+—————+
| selection |          0 | PRIMARY  |            1 | id          | A         |        2082 |     NULL | NULL   |      | BTREE      |         |               |
| selection |          0 | staff_id |            1 | staff_id    | A         |         416 |     NULL | NULL   |      | BTREE      |         |               |
| selection |          0 | staff_id |            2 | car_sn      | A         |        2082 |     NULL | NULL   |      | BTREE      |         |               |
| selection |          0 | staff_id |            3 | bid_price   | A         |        2082 |     NULL | NULL   |      | BTREE      |         |               |
+———–+————+———-+————–+————-+———–+————-+———-+——–+——+————+———+—————+
4 rows in set (0.00 sec)
MariaDB [guanzhi_removeDuplicate]> SHOW INDEXES FROM selection WHERE NOT Non_unique;
+———–+————+———-+————–+————-+———–+————-+———-+——–+——+————+———+—————+
| Table     | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+———–+————+———-+————–+————-+———–+————-+———-+——–+——+————+———+—————+
| selection |          0 | PRIMARY  |            1 | id          | A         |        2470 |     NULL | NULL   |      | BTREE      |         |               |
| selection |          0 | staff_id |            1 | staff_id    | A         |         494 |     NULL | NULL   |      | BTREE      |         |               |
| selection |          0 | staff_id |            2 | car_sn      | A         |        2470 |     NULL | NULL   |      | BTREE      |         |               |
| selection |          0 | staff_id |            3 | bid_price   | A         |        2470 |     NULL | NULL   |      | BTREE      |         |               |
+———–+————+———-+————–+————-+———–+————-+———-+——–+——+————+———+—————+
4 rows in set (0.00 sec)

可以看出:

staff_id,car_sn,bid_price是Non_unique为0

-》即unique==1,是unique的。

[总结]

用:

SHOW INDEXES FROM table;

即可查看到表中,那些No_unique为0的列示unique的。

转载请注明:在路上 » [已解决]如何确定MySQL的表中哪些key是unique的

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.232 seconds, using 23.29MB memory