什么样的查询可用于获取任何表的索引的详细信息?我需要这个来找出任何表的primarykey / autoincremented值。 请帮助/指导我...
答案 0 :(得分:2)
您可以使用
show indexes from your_table;
有关更多信息: 12.4.5.23. SHOW INDEX Syntax
作为快速演示(在未完全优化的表格上):
mysql> show indexes from post;
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| post | 0 | PRIMARY | 1 | id | A | 7 | NULL | NULL | | BTREE | |
| post | 1 | id_blog_idx | 1 | id_blog | A | 2 | NULL | NULL | | BTREE | |
| post | 1 | id_user_idx | 1 | id_user | A | 7 | NULL | NULL | | BTREE | |
| post | 1 | code_syntax_idx | 1 | code_syntax | A | 7 | NULL | NULL | | BTREE | |
| post | 1 | code_status_idx | 1 | code_status | A | 2 | NULL | NULL | | BTREE | |
| post | 1 | id_category_idx | 1 | id_category | A | 7 | NULL | NULL | | BTREE | |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
6 rows in set (0,00 sec)
但请注意,这将显示索引 - 而auto_increment
与索引没有多大关系。
如果您想查看表格的auto_increment
,可以使用desc
:
desc your_table;
了解更多信息:12.8.1. DESCRIBE Syntax
例如,使用相同的表:
mysql> desc post;
+--------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| id_blog | int(10) unsigned | NO | MUL | NULL | |
| id_user | int(10) unsigned | NO | MUL | NULL | |
...
...
| nb_comments | smallint(6) | NO | | 0 | |
+--------------------+------------------+------+-----+---------+----------------+
17 rows in set (0,05 sec)
答案 1 :(得分:1)
另外,另一种方法(也显示AUTO_INCREMENT计数器的当前值)是:
=> SHOW CREATE TABLE activations;
屈服
CREATE TABLE `activations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=104974 DEFAULT CHARSET=utf8