144 postgresql
如何从postgres中的表中获取特定字段的数据类型?例如,我有下表,student_details(stu_id integer,stu_name varchar(30),joined_date timestamp);
在这使用字段名称/或任何其他方式,我需要获取特定字段的数据类型.有可能吗?
Way*_*rad 158
您可以从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)
Run Code Online (Sandbox Code Playgroud)
Nat*_*usa 129
您可以使用pg_typeof()函数,该函数也适用于任意值.
SELECT pg_typeof("stu_id"), pg_typeof(100) from student_details limit 1;
Run Code Online (Sandbox Code Playgroud)
小智 29
试试这个请求:
SELECT column_name, data_type FROM information_schema.columns WHERE
table_name = 'YOUR_TABLE' AND column_name = 'YOUR_FIELD';
Run Code Online (Sandbox Code Playgroud)
如果您喜欢'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'
Run Code Online (Sandbox Code Playgroud)
结果:
column_name | myType
-------------+-------------------
test_id | test_domain
test_vc | varchar(15)
test_n | numeric(15,3)
big_n | bigint
ip_addr | inet
Run Code Online (Sandbox Code Playgroud)
信息模式视图和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
);
Run Code Online (Sandbox Code Playgroud)
使用psql并\d public.test正确显示数据类型的使用,test_domainvarchar(n)列的长度以及数字(p,s)列的精度和比例.
sandbox=# \d public.test
Table "public.test"
Column | Type | Modifiers
---------+-----------------------+-----------
test_id | test_domain |
test_vc | character varying(15) |
test_n | numeric(15,3) |
big_n | bigint |
ip_addr | inet |
针对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';
Run Code Online (Sandbox Code Playgroud)
column_name | data_type -------------+------------------- test_id | character varying test_vc | character varying test_n | numeric big_n | bigint ip_addr | inet
您可以通过加入其他information_schema视图或直接查询系统表来获取所有信息.psql -E可能会有所帮助.
该函数pg_typeof()正确显示了test_domainvarchar(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;
Run Code Online (Sandbox Code Playgroud)
test_id | test_vc | test_n | big_n | ip_addr -------------+-------------------+---------+--------+--------- test_domain | character varying | numeric | bigint | inet
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
Run Code Online (Sandbox Code Playgroud)
基于https://gis.stackexchange.com/a/97834。
| 归档时间: |
|
| 查看次数: |
131679 次 |
| 最近记录: |