How to find if a column is auto_increment in MySQL

user1616338 :

I want to check whether a table has an auto increment column

I've seen:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'magazine' AND COLUMN_NAME = 'id' AND EXTRA like '%auto_increment%'

but when I run this code in my application it can't access the schema database so it just returns a blank entry.

Is it possible to interrogate the table and column directly to find out if it is set to auto increment?

Every post seems to say use the schema but I can't do this.

Any ideas?

Dave :

Use SHOW COLUMNS and check the extra column in the response.

SHOW COLUMNS FROM magazine;

The response might be something like this:

| id | int(11) | NO | PRI | NULL | auto_increment |

| eventkey | varchar(27) | NO | MUL | NULL |

You may also do the query and limit the response to only columns that DO have are auto-increment.

SHOW COLUMNS FROM magazine WHERE extra LIKE '%auto%';

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=238718&siteId=1