【问题标题】:Find columns with unique constraints in a table [duplicate]在表中查找具有唯一约束的列[重复]
【发布时间】:2020-11-13 08:13:04
【问题描述】:

我正在从 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'

【问题讨论】:

  • 感谢@ThomasG!该链接肯定有帮助。我从下面的 Adrian 那里找到了我所需要的答案。

标签: mysql postgresql


【解决方案1】:

使用 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';

【讨论】:

  • 谢谢@Adrian Klaver!正是我需要的,尽管我必须在 WHERE 子句 AND tc.table_catalog = 'db' AND tc.table_schema = 'public' 中做一些小改动
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-13
  • 1970-01-01
  • 2010-10-11
相关资源
最近更新 更多