问题
What would I query my MySQL server to check if a table has a primary key or not? Something like:
if(mysql_send("SELECT TABLE table HAS PRIMARY KEY") == TRUE) {
// do stuff here
}
回答1:
SHOW INDEXES FROM TABLE WHERE Key_name = 'PRIMARY'
回答2:
SELECT EXISTS(
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'db'
and table_name='table name'
and column_key = 'PRI'
) As HasPrimaryKey
来源:https://stackoverflow.com/questions/4982558/php-mysql-check-if-a-table-has-a-primary-key