سؤال

كيف يمكنني الحصول على نوع البيانات من حقل معين من الجدول في 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)

يمكنك استعمال ال pg_typeof () وظيفة ، والتي تعمل أيضا بشكل جيد للقيم التعسفية.

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

يركض psql -E وثم \d student_details

جرب هذا الطلب:

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

إذا كنت تحب حل "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
);

استخدام psql و \d public.test يعرض بشكل صحيح استخدام نوع البيانات test_domain, ، طول أعمدة Varchar (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) والأرقام الرقمية (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

أنت ربما تكون قادرًا على الحصول على كل هذه المعلومات من خلال الانضمام إلى طرق عرض المعلومات الأخرى ، أو عن طريق الاستعلام عن جداول النظام مباشرة. 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 | 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

مرتكز على https://gis.stackexchange.com/a/97834.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top