DESCRIBE selecting the PRIMARY keys of a table in mysql
I wanted to dynamically select the primary key of a MySQL table (tbl_name) in my php code. Actually I wanted to determine all primary keys of a table, in cases where it had more than one) .
Queries I tried that failed to generate a result:
SELECT KEY FROM tbl_name
DESCRIBE tbl_name WHERE Key=’PRI’
SHOW COLUMNS FROM tbl_name WHERE Key=’PRI’
SHOW COLUMNS FROM tbl_name WHERE Key LIKE ‘%PRI%’
I Google-searched for:
* Mysql query to select primary key of a table DESCRIBE Show
* Mysql query to select primary key of table
* sql query to select primary key of table
* describe SELECTING THE PRIMARY KEYS OF A table Mysql
* SELECTING THE PRIMARY KEYS OF A table Mysql
—–
I looked at:
http://dev.mysql.com/doc/refman/5.0/en/show-columns.html
I found the correct, or (at least) workable, answer on:
http://dev.mysql.com/doc/refman/5.0/en/show-index.html
Queries I tried that succeeded:
SHOW INDEX FROM tbl_name
SHOW INDEX FROM tbl_name WHERE Key_name = ‘PRIMARY’
SHOW INDEX FROM tbl_name_has_multiple_primary_keys WHERE Key_name = ‘PRIMARY’
So, there you have it!