如何在Redshift中查询表的主键

时间:2016-06-01 05:14:50

标签: postgresql amazon-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

我做错了什么还是另一个红移异常?

非常感谢任何帮助。

7 个答案:

答案 0 :(得分:5)

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/

以下是获取表格主键的一种方法:

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

答案 1 :(得分:2)

您可以使用以下sql获取模式“ schemaname”中表“ 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;

答案 2 :(得分:1)

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。

答案 3 :(得分:0)

在这个帮助下尝试: 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

答案 4 :(得分:0)

dsz的答案对我不起作用,但是非常接近! (必须更改“目录”的拼写,从key_column_usage中选择而不是table_constraints,然后再添加一个并添加到联接中)

这对我来说适用于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;

答案 5 :(得分:0)

可悲的是,ISO标准information_schema的观点并没有说明Redshift的全部情况。我怀疑约束information_schema.table_constraints中未列出,因为在Redshift中没有强制执行约束。

  

但是有办法

AWS提供了带有许多管理工具,实用程序和视图的github存储库。 视图是here

其中一个视图是v_generate_tbl_ddl

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

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

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

答案 6 :(得分:-1)

Redshift不强制执行主键http://docs.aws.amazon.com/redshift/latest/dg/t_Defining_constraints.html的概念,但可以使用身份attritube来设置唯一性。 (更多信息请见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