Search code examples
mysqldata-dictionary

How to find tables with no foreign keys?


Is there a query or command that shows which tables have no foreign keys?


Solution

  • You could query the information schema:

    SELECT table_schema, table_name
    FROM   information_schema.tables t
    WHERE  NOT EXISTS (SELECT *
                       FROM   table_constraints tc
                       WHERE  t.table_schema = tc.table_schema AND
                              t.table_name = tc.table_name AND
                              tc.constraint_type = 'FOREIGN KEY')