mysql - 在表中查找具有唯一约束的列
问题描述
我正在从 MySQL 迁移到 PostgreSQL。我需要在表中找到所有具有唯一约束的列。PostgreSQL 中以下查询的等价物是什么?
SELECT column_name, index_name
FROM information_schema.statistics
WHERE table_schema='db' and table_name='tb' and non_unique=0 and index_name != 'PRIMARY'
解决方案
使用 information_schema 的解决方案:
SELECT
cu.column_name,
tc.constraint_name
FROM
information_schema.table_constraints AS tc
JOIN
information_schema.constraint_column_usage AS cu
ON
tc.constraint_name = cu.constraint_name
WHERE
constraint_type = 'UNIQUE'
AND
tc.table_catalog = 'db'
AND
tc.table_schema = 'public'
AND
tc.table_name = 'tb';
推荐阅读
- java - 使用“java.security.krb5.conf”System.property() 更新 kerberors krb.conf 文件不起作用
- electron - 可以更新便携式电子应用程序吗?
- javascript - Woocommerce 变体产品价格不再显示在“添加到购物车”按钮中
- excel - 如何创建交互式图纸管理器?
- angular - Angular Material DatePicker 允许字符串值
- css - 悬停时的 CSS 过渡:之后
- audio - 具有极长音频的 FFMPEG 视频
- node.js - 直接从 url createReadStream
- quarkus - 外部依赖项需要扩展吗?
- sql - 你如何获得按天计算的值的总和,但在 SQL 列中按天计算