mysql - 如何在 SQL 中查找表中的唯一键
问题描述
我在 SQL 中有 100 多个表,这些表在列上没有主键或索引。
我正在手动检查每个表的每一列,以查看它们是否具有唯一键。
如果表中不存在唯一键列,如何查询它们?
解决方案
请尝试以下查询
select stat.table_schema as database_name,
stat.table_name,
stat.index_name,
group_concat(stat.column_name
order by stat.seq_in_index separator ', ') as columns,
tco.constraint_type
from information_schema.statistics stat
join information_schema.table_constraints tco
on stat.table_schema = tco.table_schema
and stat.table_name = tco.table_name
and stat.index_name = tco.constraint_name
where stat.non_unique = 0
and stat.table_schema not in ('information_schema', 'sys',
'performance_schema', 'mysql')
and (tco.constraint_type !='UNIQUE' OR tco.constraint_type !='PRIMARY KEY') //You can made changes here if needed
group by stat.table_schema,
stat.table_name,
stat.index_name,
tco.constraint_type
order by stat.table_schema,
stat.table_name;
推荐阅读
- security - 我应该使用 JWT 令牌对游戏玩家进行身份验证吗?
- xml - SLD 代码不起作用,但未显示任何错误消息
- python - 为什么 kivy 应用程序中不显示下一个屏幕?
- google-bigquery - 有没有办法在 BigQuery 标准 SQL 中播种随机数生成过程
- github - 我如何自动将我的代码上传到 Github 存储库
- javascript - Redux Action Dispatches 但不映射到状态
- c++ - (C++) for 循环中的空初始语句是否保存所有编译器中变量的先前初始化?
- case - T-SQL Server ORDER BY 日期和空值最后
- kubernetes - 具有多个容器的 Kubernetes 烧瓶应用程序
- laravel - 尝试安装 laravel/ui composer 失败