View all foreign key constraints for entire MySQL database

Scott Wolf picture Scott Wolf · Apr 21, 2010 · Viewed 35.7k times · Source

I have a large database with over 150 tables that I've recently been handed. I'm just wondering if there is an easy way to view all foreign key constraints for the entire DB instead of on a per-table basis.

Answer

D'Arcy Rittich picture D'Arcy Rittich · Apr 21, 2010

You can use the INFORMATION_SCHEMA tables for this. For example, the INFORMATION_SCHEMA TABLE_CONSTRAINTS table.

Something like this should do it:

select *
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where CONSTRAINT_TYPE = 'FOREIGN KEY'