Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to get the primary key “column name” of a specific table in MySQL?


1 Answer
George John

Let us first create a table wherein we have a Primary Key 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)

Following is the query to get the primary key “column name” of a specific table in MySQL −

mysql> SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = 'DemoTable'
AND CONSTRAINT_NAME = 'PRIMARY';

This will produce the following output −

+-------------+
| COLUMN_NAME |
+-------------+
| CustomerId  |
+-------------+
1 row in set, 2 warnings (0.12 sec)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.