Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql show create constraint?

Is there a easy way to query a table for its constraints(foreignkeys specificaly) like show create table, but for the constraints only?

thanks,

pvgoddijn

like image 376
pvgoddijn Avatar asked Mar 18 '10 14:03

pvgoddijn


1 Answers

To show only the foreign key constraints you can check the constraint_type in information_schema.table_constraints and get the affected columns in information_schema.key_column_usage via a join

SELECT b.table_name, b.column_name, b.constraint_name,
       b.referenced_table_name, b.referenced_column_name
FROM information_schema.table_constraints a
JOIN information_schema.key_column_usage b
ON a.table_schema = b.table_schema AND a.constraint_name = b.constraint_name
WHERE a.table_schema=database() AND a.constraint_type='FOREIGN KEY'
ORDER BY b.table_name, b.constraint_name;
like image 104
Alex Jasmin Avatar answered Oct 29 '22 14:10

Alex Jasmin