I have tried several sql scripts but failed on it. Here is the one where I got close to:
SELECT *
FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_schema = schema()
AND table_name = 'my_table_name'
AND index_name = 'column_name';
I think you need below statement
https://www.db-fiddle.com/f/jbdzXwpWkYichsBL1ib2Rj/2
select constraint_name
from information_schema.table_constraints
where table_name = 'mytable'
and table_schema = 'myschema'
and constraint_name = 'PRIMARY';
or below https://www.db-fiddle.com/f/jbdzXwpWkYichsBL1ib2Rj/3
SELECT EXISTS(
SELECT 1
FROM information_schema.columns
WHERE
table_name='messages'
and column_key = 'PRI'
) As HasPrimaryKey;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With