How do I get a list of all constraints from a particular database?
+2
A:
Use the information_schema.table_constraints
table to get the names of the constraints defined on each table:
select *
from information_schema.table_constraints
where constraint_schema = 'YOUR_DB'
Use the information_schema.key_column_usage
table to get the fields in each one of those constraints:
select *
from information_schema.key_column_usage
where constraint_schema = 'YOUR_DB'
If instead you are talking about foreign key constraints, use information_schema.referential_constraints
:
select *
from information_schema.referential_constraints
where constraint_schema = 'YOUR_DB'
Senseful
2010-06-11 17:47:37
This list will only include UNIQUE, PRIMARY KEY, or FOREIGN KEY constraints. CHECK is possible, but isn't enforced. DEFAULT constraints will not be visible using this query.
OMG Ponies
2010-06-11 17:54:41
MySQL does not store CHECK constraints. If you try to define one, it parses and silently discards it.
Bill Karwin
2010-06-11 18:18:42
The DEFAULT value doesn't count as a constraint. It's stored in `information_schema.columns.column_default`.
Bill Karwin
2010-06-11 18:19:57