如何在 MySQL 中获取特定表的主键"列名"?
mysqlmysqli database
首先让我们创建一个表,其中有一个主键 CustomerId −
mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT, CustomerName varchar(20), CustomerAge int, CustomerCountryName varchar(100), PRIMARY KEY(CustomerId) ); Query OK, 0 rows affected (0.94 sec)
以下是获取 MySQL 中特定表的主键"列名"的查询 −
mysql> SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = 'DemoTable' AND CONSTRAINT_NAME = 'PRIMARY';
这将产生以下输出 −
+-------------+ | COLUMN_NAME | +-------------+ | CustomerId | +-------------+ 1 row in set, 2 warnings (0.12 sec)