您好,我正在尝试检索现有表的架构。我是 mysql 开发人员,正在尝试使用 amazon redshift。如何导出现有表的架构。在mysql中我们可以使用show create table命令。
SHOW CREATE TABLE tblName;
最佳答案
最近我写了一个 python 脚本来在 redshift 集群之间克隆表模式。如果你只想要一个表的列和列类型,你可以通过:
select column_name,
case
when data_type = 'integer' then 'integer'
when data_type = 'bigint' then 'bigint'
when data_type = 'smallint' then 'smallint'
when data_type = 'text' then 'text'
when data_type = 'date' then 'date'
when data_type = 'real' then 'real'
when data_type = 'boolean' then 'boolean'
when data_type = 'double precision' then 'float8'
when data_type = 'timestamp without time zone' then 'timestamp'
when data_type = 'character' then 'char('||character_maximum_length||')'
when data_type = 'character varying' then 'varchar('||character_maximum_length||')'
when data_type = 'numeric' then 'numeric('||numeric_precision||','||numeric_scale||')'
else 'unknown'
end as data_type,
is_nullable,
column_default
from information_schema.columns
where table_schema = 'xxx' and table_name = 'xxx' order by ordinal_position
;
但是如果你需要压缩类型和distkey/sortkeys,你需要查询另一个表:
select * from pg_table_def where tablename = 'xxx' and schemaname='xxx';
关于amazon-redshift - 在 Redshift 中获取表模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23379308/