Pra*_*tik 50 sql postgresql sqldatatypes
通过以下查询,我们可以获得PostgreSQL中表的列名和数据类型列表.
小智 81
打开psql commande行并输入:
\d+ table_name
Run Code Online (Sandbox Code Playgroud)
小智 69
select column_name,data_type
from information_schema.columns
where table_name = 'table_name';
Run Code Online (Sandbox Code Playgroud)
使用上面的查询,您可以使用列及其数据类型
Pra*_*tik 14
SELECT
a.attname as "Column",
pg_catalog.format_type(a.atttypid, a.atttypmod) as "Datatype"
FROM
pg_catalog.pg_attribute a
WHERE
a.attnum > 0
AND NOT a.attisdropped
AND a.attrelid = (
SELECT c.oid
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname ~ '^(hello world)$'
AND pg_catalog.pg_table_is_visible(c.oid)
);
Run Code Online (Sandbox Code Playgroud)

更多信息:http://www.postgresql.org/docs/9.3/static/catalog-pg-attribute.html
Mic*_*mza 12
支持在特定模式中查找表的列名和类型的版本,并使用没有任何子查询的 JOIN
SELECT
pg_attribute.attname AS column_name,
pg_catalog.format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS data_type
FROM
pg_catalog.pg_attribute
INNER JOIN
pg_catalog.pg_class ON pg_class.oid = pg_attribute.attrelid
INNER JOIN
pg_catalog.pg_namespace ON pg_namespace.oid = pg_class.relnamespace
WHERE
pg_attribute.attnum > 0
AND NOT pg_attribute.attisdropped
AND pg_namespace.nspname = 'my_schema'
AND pg_class.relname = 'my_table'
ORDER BY
attnum ASC;
Run Code Online (Sandbox Code Playgroud)
更新了 Pratik 答案以支持更多模式和可空值:
SELECT
"pg_attribute".attname as "Column",
pg_catalog.format_type("pg_attribute".atttypid, "pg_attribute".atttypmod) as "Datatype",
not("pg_attribute".attnotnull) AS "Nullable"
FROM
pg_catalog.pg_attribute "pg_attribute"
WHERE
"pg_attribute".attnum > 0
AND NOT "pg_attribute".attisdropped
AND "pg_attribute".attrelid = (
SELECT "pg_class".oid
FROM pg_catalog.pg_class "pg_class"
LEFT JOIN pg_catalog.pg_namespace "pg_namespace" ON "pg_namespace".oid = "pg_class".relnamespace
WHERE
"pg_namespace".nspname = 'schema'
AND "pg_class".relname = 'table'
);
Run Code Online (Sandbox Code Playgroud)
如果您有多个具有相同表名的架构,请不要忘记添加架构名称。
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'your_table_name' AND table_schema = 'your_schema_name';
Run Code Online (Sandbox Code Playgroud)
或使用psql:
\d+ your_schema_name.your_table_name
Run Code Online (Sandbox Code Playgroud)
SELECT column_name,data_type
FROM information_schema.columns
WHERE
table_name = 'your_table_name'
AND table_catalog = 'your_database_name'
AND table_schema = 'your_schema_name';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77903 次 |
| 最近记录: |