user2269905
user2269905

Reputation: 27

How do i get the auto increment field mysql

How can i get the auto increment field name using ALTER TABLE

Or

Is there any other idea to get auto increment field

Upvotes: 0

Views: 104

Answers (3)

Kickstart
Kickstart

Reputation: 21513

If you look in the information_schema database you can find the auto increment value for a table in the TABLES table. The COLUMNS table contains details of each column, and the extra field on this table will show auto_increment for auto incrementing columns.

Upvotes: 0

alwaysLearn
alwaysLearn

Reputation: 6950

You can use information_schema database for that

SELECT column_name, column_key, extra
FROM information_schema.columns
WHERE table_schema = 'yourdatabase'
AND table_name = 'yourtable'
AND extra = 'auto_increment'

Upvotes: 0

chandresh_cool
chandresh_cool

Reputation: 11830

You can get table details something like this

$res = $mysqli->query('SHOW COLUMNS FROM tablename');

while($row = $res->fetch_assoc()) {
     if ($row['Extra'] == 'auto_increment')
     echo 'Field with auto_increment = '.$row['Field'];
     if ($row['Key'] == 'PRI')
     echo 'Field with primary key = '.$row['Field'];
}

Upvotes: 4

Related Questions