How to find if a column is auto_increment in MySQL?

MySQLMySQLi Database

To find if a column is auto_increment in MySQL, you can use the following syntax −

select COLUMN_NAME from information_schema.columns where
TABLE_SCHEMA='yourDatabaseName' and TABLE_NAME='yourTableName' and EXTRA
like '%auto_increment%';

Let us first create a table. Here, ClientId is set AUTO_INCREMENT −

mysql> create table autoIncrementTableDemo
   -> (
   -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ClientName varchar(20),
   -> ClientAge int,
   -> ClientAddress varchar(100),
   -> ClientCountryName varchar(100)
   -> );
Query OK, 0 rows affected (0.61 sec)

Now, let us find whether any of the column is auto_increment −

mysql> select COLUMN_NAME from information_schema.columns where
TABLE_SCHEMA='test' and TABLE_NAME='autoIncrementTableDemo' and EXTRA like
'%auto_increment%';

Following is the output that gives the column i.e. auto_increment −

+-------------+
| COLUMN_NAME |
+-------------+
| ClientId    |
+-------------+
1 row in set (0.00 sec)
raja
Published on 16-Apr-2019 13:22:05
Advertisements