programing

postgres 필드의 데이터 유형 선택

telecom 2023. 5. 25. 21:32
반응형

postgres 필드의 데이터 유형 선택

포스트그레스의 테이블에서 특정 필드의 데이터 유형을 가져오려면 어떻게 해야 합니까?예를 들어 student_details 테이블(stud_id 정수, stud_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)

pg_type of() 함수를 사용할 수 있으며, 이 함수는 임의 값에도 잘 작동합니다.

SELECT pg_typeof("stu_id"), pg_typeof(100) from student_details limit 1;

다음 요청 시도:

SELECT column_name, data_type FROM information_schema.columns WHERE 
table_name = 'YOUR_TABLE' AND column_name = 'YOUR_FIELD';

달려.psql -E그리고 나서.\d student_details

만약 당신이 '마이크 셰릴' 솔루션을 좋아하지만 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_type of()가 불완전한 형식 정보를 반환합니다.이 답변들 중에서,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막대(n) 열의 길이, 숫자(p, s) 열의 정밀도 및 척도.

sandbox=# \d public.시험표 "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) 및 숫자(p, s) 열의 상세 내역을 보고하지 않습니다.

select column_name, data_type 
from information_schema.columns 
where table_catalog = 'sandbox'
  and table_schema = 'public'
  and table_name = 'test';
columnn_name | data_type-------------+-------------------test_id | 다양한 문자test_vc | 다양한 문자test_n | 숫자big_n | bigintip_addr | inet

다른 information_schema 보기에 참여하거나 시스템 테이블을 직접 쿼리하여 이러한 모든 정보를 얻을 수 있습니다. psql -E도움이 될 수도 있습니다.

함수pg_typeof()의 사용을 올바르게 보여줍니다.test_domain그러나 varchar(n) 및 숫자(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 | 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

https://gis.stackexchange.com/a/97834 을 기반으로 합니다.

https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-PATTERNS

\gdesc현재 쿼리 버퍼의 결과에 대한 설명(열 이름 및 데이터 유형)을 표시합니다.쿼리가 실제로 실행되지는 않지만 구문 오류가 포함된 경우 해당 오류가 일반적인 방법으로 보고됩니다.

현재 쿼리 버퍼가 비어 있으면 가장 최근에 보낸 쿼리가 대신 설명됩니다.

할 수 있습니다.table student_details limit 0 \gdesc다 출력이 공간을 했습니다.\d

언급URL : https://stackoverflow.com/questions/2146705/select-datatype-of-the-field-in-postgres

반응형