【发布时间】:2011-01-09 22:36:43
【问题描述】:
如何从 postgres 的表中获取特定字段的数据类型? 例如 我有下表, 学生详细信息( stu_id 整数, stu_name varchar(30 ), 加入日期时间戳 );
在此使用字段名称/或任何其他方式,我需要获取特定字段的数据类型。有没有可能?
【问题讨论】:
标签: postgresql
如何从 postgres 的表中获取特定字段的数据类型? 例如 我有下表, 学生详细信息( stu_id 整数, stu_name varchar(30 ), 加入日期时间戳 );
在此使用字段名称/或任何其他方式,我需要获取特定字段的数据类型。有没有可能?
【问题讨论】:
标签: postgresql
https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-PATTERNS
\gdesc显示描述(即列名和数据 types) 当前查询缓冲区的结果。查询不是 实际执行;但是,如果它包含某种类型的语法错误, 该错误会以正常方式报错。如果当前查询缓冲区为空,则最近发送的查询为 而是描述。
所以你可以table student_details limit 0 \gdesc
输出占用的空间小于\d
【讨论】:
试试这个请求:
SELECT column_name, data_type FROM information_schema.columns WHERE
table_name = 'YOUR_TABLE' AND column_name = 'YOUR_FIELD';
【讨论】:
table_name = 'YOUR_TABLE' AND column_name = 'YOUR_FIELD';
可以从information_schema 提取数据类型,但不方便(需要使用case 语句连接多个列)。或者,可以使用format_type 内置函数来执行此操作,但它适用于在pg_attribute 中可见但在information_schema 中不可见的内部类型标识符。示例
SELECT a.attname as column_name, format_type(a.atttypid, a.atttypmod) AS data_type
FROM pg_attribute a JOIN pg_class b ON a.attrelid = b.relfilenode
WHERE a.attnum > 0 -- hide internal columns
AND NOT a.attisdropped -- hide deleted columns
AND b.oid = 'my_table'::regclass::oid; -- example way to find pg_class entry for a table
【讨论】:
b.relfilenode 替换为 b.oid
如果您喜欢“Mike Sherrill”解决方案但不想使用 psql,我使用此查询来获取缺失的信息:
select column_name,
case
when domain_name is not null then domain_name
when data_type='character varying' THEN 'varchar('||character_maximum_length||')'
when data_type='numeric' THEN 'numeric('||numeric_precision||','||numeric_scale||')'
else data_type
end as myType
from information_schema.columns
where table_name='test'
结果:
column_name | myType
-------------+-------------------
test_id | test_domain
test_vc | varchar(15)
test_n | numeric(15,3)
big_n | bigint
ip_addr | inet
【讨论】:
您可以使用pg_typeof() 函数,该函数也适用于任意值。
SELECT pg_typeof("stu_id"), pg_typeof(100) from student_details limit 1;
【讨论】:
SELECT pg_typeof( date_part( 'year', now() ) ) AS expr 可能与您的预期不同。
pg_typeof 适用于来自存储过程的字段,对于这些字段,即使存在后端表,也是未知/不清楚的。 select state, qstart, pg_typeof(qstart) as ty_qstart from listconn()。 information_schema 在这里没有多大帮助。
信息模式视图和 pg_typeof() 返回不完整的类型信息。在这些答案中,psql 给出了最精确的类型信息。 (OP 可能不需要这么精确的信息,但应该知道这些限制。)
create domain test_domain as varchar(15);
create table test (
test_id test_domain,
test_vc varchar(15),
test_n numeric(15, 3),
big_n bigint,
ip_addr inet
);
使用psql 和\d public.test 正确显示了数据类型test_domain 的使用、varchar(n) 列的长度以及 numeric(p, s) 列的精度和小数位数。
这个针对 information_schema 视图的查询没有显示使用了test_domain。它也不会报告 varchar(n) 和 numeric(p, s) 列的详细信息。
select column_name, data_type
from information_schema.columns
where table_catalog = 'sandbox'
and table_schema = 'public'
and table_name = 'test';
列名 |数据类型
-------------+--------
test_id |性格多变
测试_vc |性格多变
测试_n |数字
大_n |大整数
ip_addr |网络
您可能能够通过加入其他 information_schema 视图或直接查询系统表来获取所有这些信息。 psql -E 可能会对此有所帮助。
函数pg_typeof() 正确显示了test_domain 的使用,但没有报告varchar(n) 和numeric(p, s) 列的详细信息。
select pg_typeof(test_id) as test_id,
pg_typeof(test_vc) as test_vc,
pg_typeof(test_n) as test_n,
pg_typeof(big_n) as big_n,
pg_typeof(ip_addr) as ip_addr
from test;
test_id |测试_vc |测试_n |大_n | ip_addr
-------------+-------------------+------------+------ --+---------
测试域 |性格变化|数字 |大整数 |网络
【讨论】:
运行psql -E,然后运行\d student_details
【讨论】:
您可以从 information_schema 获取数据类型(此处引用了 8.4 文档,但这不是新功能):
=# select column_name, data_type from information_schema.columns
-# where table_name = 'config';
column_name | data_type
--------------------+-----------
id | integer
default_printer_id | integer
master_host_enable | boolean
(3 rows)
【讨论】:
where table_catalog = ? and table_schema = ? and table_name = ?; 但是这个 information_schema 视图没有考虑到 DDL 可能使用了domains。
pg_typeof一起使用