我是AWS的新手,有人可以告诉我redshifts与mysql命令等效吗?
show tables -- redshift command
describe table_name -- redshift command
Answers:
所有信息都可以在PG_TABLE_DEF
表格文档中找到。
列出public
模式中的所有表(默认)-show tables
等效:
SELECT DISTINCT tablename
FROM pg_table_def
WHERE schemaname = 'public'
ORDER BY tablename;
名为table_name的表中所有列的描述-describe table
等效:
SELECT *
FROM pg_table_def
WHERE tablename = 'table_name'
AND schemaname = 'public';
select distinct table_name from information_schema.columns where table_schema = 'public'
。
pg_table_def
表没有有关该表的任何信息。@j_c建议的查询-可以information_schema
代替查询。
我必须从信息模式中进行选择以获取表和列的详细信息。如果它可以帮助任何人:
SELECT * FROM information_schema.tables
WHERE table_schema = 'myschema';
SELECT * FROM information_schema.columns
WHERE table_schema = 'myschema' AND table_name = 'mytable';
pg_table_def
,您必须将架构添加到SEARCH_PATH:中SET SEARCH_PATH to '$user', public, YOUR_SCEHMA_NAME
。绝对不直观...
或者简单地:
\dt
显示表格
\d+ <table name>
描述一张桌子
编辑:使用psql命令行客户端工作
\dt
我会得到Error: Syntax error at or near "\"
。您是否以其他方式访问Redshift?
Tomasz Tybulewicz的答案是个好方法。
SELECT * FROM pg_table_def WHERE tablename = 'YOUR_TABLE_NAME' AND schemaname = 'YOUR_SCHEMA_NAME';
如果在搜索路径中未定义架构名称,则该查询将显示空结果。请首先通过以下代码检查搜索路径。
SHOW SEARCH_PATH
如果在搜索路径中未定义架构名称,则可以重置搜索路径。
SET SEARCH_PATH to '$user', public, YOUR_SCEHMA_NAME
您可以使用-desc /在Redshift中查看视图/表的定义。我一直在使用Workbench / J作为Redshift的SQL客户端,它在Results选项卡旁边的Messages选项卡中提供了定义。
在下面的文章中,我记录了从Redshift检索TABLE和COLUMN注释的查询。 https://sqlsylvia.wordpress.com/2017/04/29/redshift-comment-views-documenting-data/
请享用!
表注释
SELECT n.nspname AS schema_name
, pg_get_userbyid(c.relowner) AS table_owner
, c.relname AS table_name
, CASE WHEN c.relkind = 'v' THEN 'view' ELSE 'table' END
AS table_type
, d.description AS table_description
FROM pg_class As c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
LEFT JOIN pg_description As d
ON (d.objoid = c.oid AND d.objsubid = 0)
WHERE c.relkind IN('r', 'v') AND d.description > ''
ORDER BY n.nspname, c.relname ;
专栏评论
SELECT n.nspname AS schema_name
, pg_get_userbyid(c.relowner) AS table_owner
, c.relname AS table_name
, a.attname AS column_name
, d.description AS column_description
FROM pg_class AS c
INNER JOIN pg_attribute As a ON c.oid = a.attrelid
INNER JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
LEFT JOIN pg_description As d
ON (d.objoid = c.oid AND d.objsubid = a.attnum)
WHERE c.relkind IN('r', 'v')
AND a.attname NOT
IN ('cmax', 'oid', 'cmin', 'deletexid', 'ctid', 'tableoid','xmax', 'xmin', 'insertxid')
ORDER BY n.nspname, c.relname, a.attname;
捷径
\ d用于显示所有表格
\ d表名描述表
\?有关Redshift的更多快捷方式