Find tables that are not referenced by foreign keys in MariaDB database

The query below lists tables that are not referenced by foreign keys.

See also:

Query

select 'No FKs >-' as foreign_keys,
    tab.table_schema as database_name,
    tab.table_name
from information_schema.tables tab
left join information_schema.referential_constraints ref
          on ref.unique_constraint_schema = tab.table_schema
          and ref.referenced_table_name = tab.table_name
where tab.table_type = 'BASE TABLE'
      and tab.table_schema not in ('mysql', 'information_schema',
                                   'performance_schema', 'sys')
      and ref.constraint_name is null
     -- and tab.table_schema = 'your database name'
order by tab.table_schema,
         tab.table_name;

Columns

  • foreign_keys - symbol indicating the lack of FK references
  • database_name - database (schema) name
  • table_name - table name

Rows

  • One row: represents one table
  • Scope of rows: all tables in a database (schema) that are not referenced by foreign keys
  • Ordered by: database name and table name

Sample results