我需要一个脚本来帮助我查找具有主键的表,而不指定数据库名称或表名。需要帮助...
答案 0 :(得分:0)
select
t.table_schema,t.table_name
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)) =1;
这很有效。
答案 1 :(得分:0)
对于MySQL,每个主键总有一个名为“PRIMARY”的约束。所以找到它们非常简单:
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_NAME = 'PRIMARY';