如何在MySQL中获取表的主键?
要获取表的主键,可以使用show命令。语法如下-
SHOW INDEX FROM yourDatebaseName.yourTableName WHERE Key_name = 'PRIMARY';
假设我们有一个包含两个主键的表;其中一个是“Id”,第二个是“RollNum”。对表的查询如下-
mysql> create table TwoOrMorePrimary −> ( −> Id int, −> Name varchar(200), −> RollNum int −> , −> Primary key(Id,Age) −> );
应用以上语法来获取表的主键。查询如下-
mysql> SHOW INDEX FROM business.TwoOrMorePrimary −> WHERE Key_name = 'PRIMARY';
以下是输出-
+------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression | +------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ | twoormoreprimary | 0 | PRIMARY | 1 | Id | A | 0 | NULL | NULL | | BTREE | | | YES | NULL | | twoormoreprimary | 0 | PRIMARY | 2 | RollNum | A | 0 | NULL | NULL | | BTREE | | | YES | NULL | +------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ 2 rows in set (0.12 sec)