PHP MySQL检查表是否有主键

时间:2011-02-13 05:11:05

标签: mysql primary-key

我会查询MySQL服务器以检查表是否有主键?类似的东西:


if(mysql_send("SELECT TABLE table HAS PRIMARY KEY") == TRUE) {
   // do stuff here
}

2 个答案:

答案 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