Finding tables without primary keys
COLUMNS
table from the INFORMATIOn_SCHEMA
,
and so I came up with this query, where I sum the number of columns
that are inside either a PRIMARY or UNIQUE key and filter only the ones
where such sum is zero (i.e. no primary or unique keys): check this query (IN should be faster than outer join if result is few tables):
SELECT table_schema, table_name
FROM information_schema.tables
WHERE (table_catalog, table_schema, table_name) NOT IN
(SELECT table_catalog, table_schema, table_name
FROM information_schema.table_constraints
WHERE constraint_type in ('PRIMARY KEY', 'UNIQUE'))
AND table_schema NOT IN ('information_schema', 'mysql');
alt.
select tables.table_schema
, tables.table_name
, tables.engine
from information_schema.tables
left join (
select table_schema
, table_name
from information_schema.statistics
group by table_schema
, table_name
, index_name
having
sum(
case
when non_unique = 0
and nullable != 'YES' then 1
else 0
end
) = count(*)
) puks
on tables.table_schema = puks.table_schema
and tables.table_name = puks.table_name
where puks.table_name is null
and tables.table_type = 'BASE TABLE'
alt.
select t.table_schema,t.table_name,engine from information_schema.tables t inner join information_schema .columns c on t.table_schema=c.table_schema and t.table_name=c.table_name group by t.table_schema,t.table_name having sum(if(column_key in ('PRI','UNI'), 1,0)) =0;
alt.
Finding tables without primary keys
COLUMNS
table from the INFORMATIOn_SCHEMA
,
and so I came up with this query, where I sum the number of columns
that are inside either a PRIMARY or UNIQUE key and filter only the ones
where such sum is zero (i.e. no primary or unique keys): alt.
select table_schema,table_name from information_schema.columns group by table_schema,table_name having sum(if(column_key in ('PRI','UNI'), 1,0)) = 0;
alt.
select t.table_schema, t.table_name from information_schema.tables t left join information_schema. statistics s on t.table_schema=s.table_schema and t.table_name=s.table_name and s.non_unique=0 where s.table_name is null;
No comments:
Post a Comment