PHP MySQL Check If A Table Has A Primary Key

人盡茶涼 提交于 2019-12-07 06:13:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!