postgresのテーブルから特定のフィールドのデータ型を取得するにはどうすればよいですか?たとえば、次のテーブル、student_details(stu_id integer、stu_name varchar(30)、joined_date timestamp)があります。
これでフィールド名/またはその他の方法を使用して、特定のフィールドのデータ型を取得する必要があります。可能性はありますか?
postgresのテーブルから特定のフィールドのデータ型を取得するにはどうすればよいですか?たとえば、次のテーブル、student_details(stu_id integer、stu_name varchar(30)、joined_date timestamp)があります。
これでフィールド名/またはその他の方法を使用して、特定のフィールドのデータ型を取得する必要があります。可能性はありますか?
回答:
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がドメインを使用した可能性があるとは考慮されていません。
pg_typeof
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はここではあまり役に立ちません。
「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()は不完全な型情報を返します。これらの回答のうち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
);
データ型の使用、varchar(n)列の長さ、numeric(p、s)列の精度とスケールを使用psql
して\d public.test
正しく示していtest_domain
ます。
sandbox =#\ d public.test テーブル "public.test" コラム| タイプ| 修飾子 --------- + ----------------------- + ----------- test_id | test_domain | test_vc | 文字の変化(15)| test_n | 数値(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';
column_name | データ・タイプ ------------- + ------------------- test_id | キャラクターの変化 test_vc | キャラクターの変化 test_n | 数値 big_n | bigint 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 | test_vc | test_n | big_n | ip_addr ------------- + ------------------- + --------- + ------ -+ --------- test_domain | キャラクターの変化| 数値| bigint | イネット
からデータ型をプルする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