如何使用psql命令在PostgreSQL中执行Oracle的DESCRIBE TABLE?


当前回答

/dt是列出数据库中所有表的命令。使用/d命令和/d+,我们可以获得表的详细信息。sysntax将如下所示*/d table_name(或)\d+table_name

其他回答

描述表的最佳方式,如列、类型、列的修饰符等。

\d+ tablename or \d tablename

下面的命令可以简单地描述多个表

\dt <table> <table>

下面的命令可以详细描述多个表:

\d <table> <table>

下面的命令可以更详细地描述多个表:

\d+ <table> <table>

除了PostgreSQL方式(\d“something”或\dt“table”或\ds“sequence”等)

SQL标准方式,如下所示:

select column_name, data_type, character_maximum_length, column_default, is_nullable
from INFORMATION_SCHEMA.COLUMNS where table_name = '<name of table>';

它受到许多db引擎的支持。

当表名以大写字母开头时,应将表名放在引号中。

示例:\d“用户”

我为get表模式编写了以下脚本。

'CREATE TABLE ' || 'yourschema.yourtable' || E'\n(\n' ||
array_to_string(
array_agg(
'    ' || column_expr
)
, E',\n'
) || E'\n);\n'
from
(
SELECT '    ' || column_name || ' ' || data_type || 
coalesce('(' || character_maximum_length || ')', '') || 
case when is_nullable = 'YES' then ' NULL' else ' NOT NULL' end as column_expr
FROM information_schema.columns
WHERE table_schema || '.' || table_name = 'yourschema.yourtable'
ORDER BY ordinal_position
) column_list;