amazon-redshift - 在 Redshift 中获取表模式

标签 amazon-redshift

您好,我正在尝试检索现有表的架构。我是 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/

相关文章:

python - 使用 psycopg2 的 aws localstack redshift 连接问题

sql - 如何将 int (yyyymmdd) 转换为 SQL 中的时间戳?

python - 如何在本地错误日志文件中获取 Redshift 错误?

amazon-web-services - 跨两个不同的 AWS 账户将数据从 DynamoDB 复制到 Redshift 中?

sql - Redshift FULL OUTER JOIN 不输出 NULL

azure - 从 PowerBI 服务连接到私有(private) Amazon Redshift

sql - redshift 上 array_agg() 或 string_agg() 的替代方案

mysql - 一起使用 MySQL 和 RedShift 的坏习惯?

sql - 非聚合查询内部的聚合情况

amazon-web-services - 从 S3 迁移到 Redshift 数据库时如何删除标题行?