Amazon redshift 如何从红移中获取表和列信息?

Amazon redshift 如何从红移中获取表和列信息?,amazon-redshift,Amazon Redshift,pg_表格提供了表格列表。是否有pg_columns或其等效文件提供列列表 在DB2中,我将查询sysibm.systables/columns以获取此类信息。红移的等效值是什么?使用表格获取该信息: 看起来是这样的: Pg_table_def可以提供一些有用的信息,但它不会告诉您列顺序、默认值或字符字段大小。这里有一个查询,可以向您展示所有这些注意事项,我从原始帖子开始更新了这个查询,现在它包括列编码、diststyle/distkey、sortkey和主键,以及打印出显示表所有者的语句:

pg_表格提供了表格列表。是否有pg_columns或其等效文件提供列列表

在DB2中,我将查询sysibm.systables/columns以获取此类信息。红移的等效值是什么?

使用表格获取该信息:

看起来是这样的:


Pg_table_def可以提供一些有用的信息,但它不会告诉您列顺序、默认值或字符字段大小。这里有一个查询,可以向您展示所有这些注意事项,我从原始帖子开始更新了这个查询,现在它包括列编码、diststyle/distkey、sortkey和主键,以及打印出显示表所有者的语句:

选择pk.pkey,tm.schemaname | |'。| | tm.tablename,'create table'| | tm.schemaname | |'。| | tm.tablename ||' ' ||科尔德夫 -主键 ||decodepk.pkey,null,,pk.pkey -diststyle和dist键 ||已解码。distkey,null,“diststyle”| | dist| u style | |“”,d.distkey -排序键 ||从选择中选择decodeskey、null、skey “sortkey”| |子字符串 数组选择“,”| | castcolumn_名称作为varchar100作为str from 从information_schema.columns中选择column_name,其中col.table_schema=tm.schemaname和col.table_name=tm.tablename 参加 -给分类cols 从pg_属性pa中选择attrelid作为tableid,attname作为colname,attsortkeyord作为sort_col_顺序 pa.attnum>0,而不是pa.attisdropped和pa.attsortkeyord>0 ston tm.tableid=st.tableid和c2.column\u name=st.colname按排序顺序 , ,210000 | |作为斯凯 ||';' -此处的其他alter table查询用于设置所有者 ||“将表格”| | tm.schemaname | |'。| | tm.tablename | | |所有者更改为”| | tm.owner |” 从…起 -t主表列表 选择substringn.nspname,1100作为schemaname,substringc.relname,1100作为tablename,c.oid作为tableid,use2.usename作为所有者,decodec.reldiststyle,0,'偶数',1,'KEY',8,'ALL'作为dist_样式 来自pg_名称空间n,pg_类c,pg_用户使用2 其中n.oid=c.REL名称空间 和nspname不在“pg_目录”、“pg_土司”、“信息_架构”中 和c.relname“临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时临时 和c.relowner=use2.usesysid 商标 -cp为创建字符串创建col参数 参加 选择 substrstr,charindex'QQQ',str+3,charindex'ZZZ',str-charindex'QQQ',str+3作为表格ID ,substrresplacereplacetstr,'ZZZ','QQQ'| | substrstrstrstrstr,charindex'QQQ',str+3,charindex'ZZZ',str-charindex'QQQ',str+3,,210000 as coldef 从…起 选择阵列到字符串阵列 选择'QQQ'| | castt.tableid为varchar10 | |'ZZZ'| |'、'| |列|名称| | | | | | | | | | | | | | | | | | | | | | | | | | | | | decode -违约 ||decodesubstrcolumn_default,2,8,'identity',null,,'default'| | | |列| | |'' -可空 ||decodeis_NULL可为'YES','NULL','NO','notnull' -身份 ||decodesubstrcolumn_default,2,8,'identity','identity'| | | substrcolumn_default,charindex,column_default+1,lengthcolumn_default-charindex,reversecolumn_default-charindex,column|u default | |“, -编码 ||解码、无、编码 as-str 从…起 -ci所有列信息 选择castt.tableid为int,casttable_模式为varchar100,casttable_名称为varchar100,castcolumn_名称为varchar100, castordinal_位置为int,castcolumn_默认为varchar100,castis_可为null为varchar20,castudt_名称为varchar50,castcharacter_最大长度为int, 排序列顺序,已解码。列名称,空,0,1距离键,e.enc 从…起 从information_schema.c列中选择*,其中c.table_schema=t.schemaname和c.table_name=t.tablename c 左连接 -给分类cols 从pg_属性a中选择attrelid作为tableid,attname作为colname,attsortkeyord作为sort_col_顺序 a、 attnum>0而非a.ATITSDROPPED和a.attsortkeyord>0 t.tableid=s.tableid和c.column_name=s.colname上的s 左连接 -给出编码 选择attrelid作为tableid,attname作为colname,format_encodinga.attencodingtype::integer作为pg_属性a中的enc,其中 a、 attnum>0而不是a。attisdropped t.tableid=e.tableid和c.column\u name=e.colname上的e 左连接 -给距离 从pg_属性a中选择attrelid作为tableid,attname作为colname,其中 a、 attnum>0,而不是a.attisdropped和a.attisdistkey='t' t.tableid=d.tableid和c.column\u name=d.colname上的d 按序号位置排序 词 -对于工作数组函数 ,as str 从…起 -需要表格ID吗 选择substringn.nspname,1100作为schemaname,substringc.relname,1100作为tablename,c.oid作为tableid 来自pg_名称空间n,pg_类c 其中n.oid=c.REL名称空间 和名称N “pg_目录”、“pg_土司”、“信息模式”中的OT T tm.tableid上的cp=cp.tableid -这里的主键查询 左连接 选择c.oid作为tableid',主键'| | substringpg_get_indexdefindexrelid,charindex,pg_get_indexdefindexrelid-1,60作为pkey 从pg_索引i,pg_名称空间n,pg_类c 其中i.primary=true i.indrelid=c.oid 和n.oid=c.rel名称空间 tm.tableid上的主键=主键tableid -距离键 左连接 选择 -在主键后关闭col defs || “distkey”| | castcolumn|名称为varchar100 | |作为distkey,t.tableid 从信息_schema.c列 参加 -需要表格ID吗 选择substringn.nspname,1100作为schemaname,substringc.relname,1100作为tablename,c.oid作为tableid 来自pg_名称空间n,pg_类c 其中n.oid=c.REL名称空间 和nspname不在“pg_目录”、“pg_土司”、“信息_架构”中 t在c.table_schema=t.schemaname和c.table_name=t.tablename上 参加 -给距离 从pg_属性a中选择attrelid作为tableid,attname作为colname,其中 a、 attnum>0,而不是a.attisdropped和a.attisdistkey='t' t.tableid=d.tableid和c.column\u name=d.colname上的d tm.tableid上的d=d.tableid 其中tm.schemaname | |'。| | tm.tablename='myschema.mytable' 发件人:

信息模式是一个非常重要的模式,是ANSI标准的一部分,但不是很标准。如果所有关系数据库都支持它,那就太好了,但它们并不都支持——MySQL 5、SQL Server 2000+和PostgreSQL 7.4+都支持它们。Oracle和DB2仍然没有,但还有希望。对于支持information_模式的DBMS,有不同的级别,但在所有级别中,您可以非常肯定地找到具有相同命名字段的表、视图、列,这些字段包含数据库中所有表的完整列表、视图列表和视图定义DDL以及所有列、列大小和数据类型

pg_目录模式是标准的PostgreSQL元数据和核心模式。您将在这里找到预定义的全局postgres函数,以及非常特定于postgres的有关数据库的有用元数据。这是postgres用于内部管理事物的模式。许多信息与信息模式中的信息重叠,但是对于信息模式中存在的数据,信息模式更容易查询,并且需要较少或不需要连接来获得基本信息

因此,对于基本查询,最好使用信息模式

此页面显示了一个漂亮的查询列表,用于获取有关架构的信息。以下是与此问题相关的内容:

SELECT a.attname
  FROM pg_class c, pg_attribute a, pg_type t
 WHERE c.relname = 'test2'
   AND a.attnum > 0
   AND a.attrelid = c.oid
  AND a.atttypid = t.oid

-- with INFORMATION_SCHEMA:

SELECT column_name
  FROM information_schema.columns
 WHERE table_name = 'test2';
有关详细信息,请参阅:

SELECT a.attnum AS ordinal_position,
         a.attname AS column_name,
         t.typname AS data_type,
         a.attlen AS character_maximum_length,
         a.atttypmod AS modifier,
         a.attnotnull AS notnull,
         a.atthasdef AS hasdefault
    FROM pg_class c,
         pg_attribute a,
         pg_type t
   WHERE c.relname = 'test2'
     AND a.attnum > 0
     AND a.attrelid = c.oid
     AND a.atttypid = t.oid
ORDER BY a.attnum;

-- with INFORMATION_SCHEMA:

  SELECT ordinal_position,
         column_name,
         data_type,
         column_default,
         is_nullable,
         character_maximum_length,
         numeric_precision
    FROM information_schema.columns
   WHERE table_name = 'test2'
ORDER BY ordinal_position;

是否按列顺序返回?仅当用户有权访问表时才起作用。不列出数据库中的所有表确保您要查找的架构位于搜索路径中。如果我有一个表的架构怎么办?如果提供了架构,则不起作用。有关提取列信息的一系列更简单的方法,请参见,包括orderFor更简单、更清晰的方法,请查看此处的答案:几个月前,AWS发布了一些视图,以帮助处理Redshift内部目录中一些更为深奥的组件。我建议将此作为获取表DDL的最终答案:如果您的用例是创建一个新表或在表列元数据和另一个表之间使用SET操作,则必须使用pg_类、pg_属性和pg_类型。否则,您将获得无效的操作:红移表不支持指定的类型或函数,每个信息消息一个。当您试图对信息模式或pg_表_def表执行这些操作时。
SELECT a.attname
  FROM pg_class c, pg_attribute a, pg_type t
 WHERE c.relname = 'test2'
   AND a.attnum > 0
   AND a.attrelid = c.oid
  AND a.atttypid = t.oid

-- with INFORMATION_SCHEMA:

SELECT column_name
  FROM information_schema.columns
 WHERE table_name = 'test2';
SELECT a.attnum AS ordinal_position,
         a.attname AS column_name,
         t.typname AS data_type,
         a.attlen AS character_maximum_length,
         a.atttypmod AS modifier,
         a.attnotnull AS notnull,
         a.atthasdef AS hasdefault
    FROM pg_class c,
         pg_attribute a,
         pg_type t
   WHERE c.relname = 'test2'
     AND a.attnum > 0
     AND a.attrelid = c.oid
     AND a.atttypid = t.oid
ORDER BY a.attnum;

-- with INFORMATION_SCHEMA:

  SELECT ordinal_position,
         column_name,
         data_type,
         column_default,
         is_nullable,
         character_maximum_length,
         numeric_precision
    FROM information_schema.columns
   WHERE table_name = 'test2'
ORDER BY ordinal_position;