Question

Comment puis-je obtenir de champ spécifique type de données de la table dans Postgres? Par exemple Je le tableau ci-dessous,   (student_details        stu_id nombre entier,        stu_name varchar (30),        horodatage joined_date    );

Dans ce en utilisant le nom du champ / ou de toute autre manière, je dois obtenir le type de données du champ spécifique. Est-il possible?

Pas de solution correcte

Autres conseils

Vous pouvez obtenir les types de données de la information_schema (8.4 docs ici référencés, mais ce n'est pas une nouvelle fonctionnalité):

=# 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)

Vous pouvez utiliser le pg_typeof ( ) fonction, qui fonctionne aussi bien pour des valeurs arbitraires.

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

run psql -E puis \d student_details

Essayer cette demande:

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

Si vous aimez solution « Mike Sherrill », mais ne veulent pas utiliser psql, j'ai utilisé cette requête pour obtenir les informations manquantes:

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'

avec le résultat:

column_name |     myType
-------------+-------------------
 test_id     | test_domain
 test_vc     | varchar(15)
 test_n      | numeric(15,3)
 big_n       | bigint
 ip_addr     | inet

Les vues du schéma d'information et pg_typeof () renvoient des informations de type incomplet. Parmi ces réponses, psql donne les informations de type plus précis. (L'OP peut-être pas besoin de ces informations précises, mais devrait connaître les limites.)

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
);

Utilisation psql et \d public.test montre correctement l'utilisation du type de données test_domain, la longueur des colonnes varchar (n), et la précision et l'échelle des colonnes numériques (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                  |

Cette requête sur une vue information_schema ne pas montrer l'utilisation de test_domain du tout. Il ne signale pas les détails des colonnes varchar (n) et numériques (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 |     data_type
-------------+-------------------
 test_id     | character varying
 test_vc     | character varying
 test_n      | numeric
 big_n       | bigint
 ip_addr     | inet

peut être en mesure d'obtenir toutes ces informations en se joignant à d'autres vues information_schema, ou en interrogeant les tables système. psql -E pourrait aider.

La fonction pg_typeof() montre correctement l'utilisation de test_domain, mais ne signale pas les détails de varchar (n) et les colonnes numériques (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 | character varying | numeric | bigint | inet

Tirer le type de données à partir information_schema est possible, mais pas pratique (nécessite joindre plusieurs colonnes avec une instruction case). En variante, on peut utiliser format_type fonction intégrée pour le faire, mais il fonctionne sur les identifiants de type internes qui sont visibles dans pg_attribute mais pas dans information_schema. Exemple

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

Basé sur https://gis.stackexchange.com/a/97834 .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top