我会查询MySQL服务器以检查表是否有主键?类似的东西:
if(mysql_send("SELECT TABLE table HAS PRIMARY KEY") == TRUE) {
// do stuff here
}
答案 0 :(得分:14)
SHOW INDEXES FROM TABLE WHERE Key_name = 'PRIMARY'
答案 1 :(得分:3)
SELECT EXISTS(
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'db'
and table_name='table name'
and column_key = 'PRI'
) As HasPrimaryKey