如何在 Redshift 中查询 table 的主键

How to query for a table's primary keys in Redshift

我尝试使用 Postgresql wiki (https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns) 上建议的代码:

SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type
FROM   pg_index i
JOIN   pg_attribute a ON a.attrelid = i.indrelid
                     AND a.attnum = ANY(i.indkey)
WHERE  i.indrelid = 'tablename'::regclass
AND    i.indisprimary;

不幸的是,它似乎在 Redshift 中不起作用。我得到这个错误:

ERROR:  op ANY/ALL (array) requires array on right side

我是不是做错了什么或者这是另一个红移异常?

如有任何帮助,我们将不胜感激。

试试这个: https://bitbucket.org/zzzeek/sqlalchemy/pull-request/6/sqlalchemy-to-support-postgresql-80/diff

SELECT attname column_name, attnotnull, 
  format_type(atttypid, atttypmod) as column_type, atttypmod,
  i.indisprimary as primary_key,
  col_description(attrelid, attnum) as description
FROM pg_attribute c
  LEFT OUTER JOIN pg_index i
  ON c.attrelid = i.indrelid AND i.indisprimary AND
  c.attnum = ANY(string_to_array(textin(int2vectorout(i.indkey)), ' '))
where c.attnum > 0 AND NOT c.attisdropped AND c.attrelid = :tableOid
order by attnum

Redshift 不强制执行主键的概念http://docs.aws.amazon.com/redshift/latest/dg/t_Defining_constraints.html but identity attritube can be used to set uniqueness. (more info at http://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html)

现有表的详细信息,可以使用如下查询

select column_name, is_nullable, data_type, character_maximum_length 
from information_schema.columns 
where table_schema='schema_name' 
and table_name='table_name' 
order by ordinal_position

Redshift doesn't have the concept of primary keys http://docs.aws.amazon.com/redshift/latest/dg/t_Defining_constraints.html but identity attritube can be used to set uniqueness. (more info at http://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html)

这不是真的。

Redshift 不强制执行主键约束,但可以使用它们。它们在自动化数据管道或数据质量检查时很有用。在设计星型模式时,redshift 也推荐使用它们,因为它们被查询优化器用作提示。 https://aws.amazon.com/blogs/big-data/optimizing-for-star-schemas-and-interleaved-sorting-on-amazon-redshift/

这是获取 table 的主键的一种方法:

SELECT
  schemaname,
  tablename,
  replace(substr(ddl, POSITION('(' IN ddl)+1 ),')','') primary_key
FROM
  admin.v_generate_tbl_ddl
WHERE
  schemaname = 'schema'
  AND tablename='table'
  AND upper(ddl) LIKE '%PRIMARY%';

视图 admin.v_generate_tbl_ddl 的代码在这里:https://github.com/awslabs/amazon-redshift-utils/tree/master/src/AdminViews

您可以使用以下 sql 获取架构 "schemaname"

中 table "tablename" 的主键列表
SELECT
  att.attname
FROM pg_index ind, pg_class cl, pg_attribute att
WHERE 
  cl.oid = 'schemaname."tablename"'::regclass 
  AND ind.indrelid = cl.oid 
  AND att.attrelid = cl.oid
  and att.attnum = ANY(string_to_array(textin(int2vectorout(ind.indkey)), ' '))
  and attnum > 0
  AND ind.indisprimary
order by att.attnum;

INFORMATION_SCHEMA:

开始就容易多了
select TC.column_name
from information_schema.table_constraints AS TC
inner join information_schema.key_column_usage AS KCU
  on KCU.constraint_catalogue = TC.constraint_catalogue
  and KCU.constraint_schema = TC.constraint_schema
  and KCU.constraint_name = TC.constraint_name
where TC.constraint_type = 'PRIMARY KEY'
and TC.table_schema = '<my schema>'
and TC.table_name = '<my table>'
order by KCU.ordinal_position

是的,这适用于 Redshift。

dsz 的回答对我没有用,但非常接近! (必须将 "catalogue"、select 的拼写从 key_column_usage 改为 table_constraints,并在连接中添加一个额外的 and )

这适用于我的 redshift 和 MySQL。尚未明确尝试过 Postgres,但应该可以:

select KCU.table_schema, KCU.table_name, KCU.column_name
from information_schema.table_constraints AS TC
inner join information_schema.key_column_usage AS KCU
 on KCU.constraint_catalog = TC.constraint_catalog
 and KCU.constraint_schema = TC.constraint_schema
 and KCU.table_name = TC.table_name
 and KCU.constraint_name = TC.constraint_name
where TC.constraint_type = 'PRIMARY KEY'
and TC.table_schema = '<my schema>'
and TC.table_name = '<my table>'
order by KCU.ordinal_position;

ISO 标准 information_schema 观点遗憾的是没有讲述 Redshift 的全部故事。我怀疑约束没有在 information_schema.table_constraints 中列出,因为它们在 Redshift 中没有强制执行。

HOWEVER there is a way

AWS 提供了一个 github 存储库,其中包含许多管理工具、实用程序和视图。 观看次数为 here

其中一个观点是 v_generate_tbl_ddl

此视图可为您提供完整的 DDL 以重新创建表,包括指定 Primary Key

我已经提取了视图的相关部分,它将为您提供主键。该视图的其他部分展示了如何获取 dist key、sort key 和其他有用的东西:

SELECT 
    c.oid::bigint AS table_id, 
    n.nspname AS schemaname,
    c.relname AS tablename, 
     pg_get_constraintdef(con.oid)::character varying AS PRIMARYKEY /*AS ddl*/
FROM pg_constraint con
JOIN pg_class c ON c.relnamespace = con.connamespace AND c.oid = con.conrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'::"char" AND pg_get_constraintdef(con.oid) !~~ 'FOREIGN KEY%'::text

information_schema 并不适用于所有用户

SELECT
    f.attname AS column_name
FROM
    pg_catalog.pg_namespace n
JOIN pg_catalog.pg_class c ON
    n.oid = c.relnamespace
JOIN pg_catalog.pg_attribute f ON
    c.oid = f.attrelid
JOIN pg_catalog.pg_constraint p ON
    p.conrelid = c.oid
    AND f.attnum = ANY (p.conkey)
WHERE
    n.nspname = 'schema_name'
    AND c.relkind = 'r'
    AND c.relname = 'table_name'
    AND p.contype = 'p'
    AND f.attnum > 0
ORDER BY
    f.attnum;