如何检查MySQL中是否存在主键

时间:2018-08-06 18:06:35

标签: mysql sql

我尝试了几个sql脚本,但是失败了。这是我最接近的一个:

SELECT *
FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_schema = schema()
AND   table_name   = 'my_table_name'
AND   index_name   = 'column_name';

2 个答案:

答案 0 :(得分:1)

我认为您需要以下声明

https://www.db-fiddle.com/f/jbdzXwpWkYichsBL1ib2Rj/2

VBA

或以下 https://www.db-fiddle.com/f/jbdzXwpWkYichsBL1ib2Rj/3

   select constraint_name
from information_schema.table_constraints
where table_name = 'mytable'
and table_schema = 'myschema'
and constraint_name = 'PRIMARY'
    and constraint_name = 'PRIMARY'

答案 1 :(得分:0)

SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = schema()
AND column_key = 'PRI'
AND table_name = 'my_table_name'
AND column_name = 'column_name';