
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to get the primary key “column name” of a specific table in MySQL?
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)
- Related Articles
- How to get primary key of a table in MySQL?
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- How to reset the primary key of a table in mysql?
- How do you get whether a column is a primary key in MySQL?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How to make MySQL table primary key auto increment?
- ALTER TABLE to add a composite primary key in MySQL?
- How to identify composite primary key in any MySQL database table?
- Set existing column as Primary Key in MySQL?
- How to refer primary key as Foreign to various table in MySQL?
- MySQL ALTER column to remove primary key and auto_increment?
- Can we remove a primary key from MySQL table?
- How to find the primary key of a DB2 table TAB1?
- How can we set PRIMARY KEY on multiple columns of a MySQL table?
- How to add a primary key constraint to a column of a table in a database using JDBC API?

Advertisements