PostgreSQL’de bir tablonun sütun isimlerini ve veri tiplerini almak

PostgreSQL’de bir tablonun sütunlarının isimlerini ve bu sütunlara ait veri tiplerini almaya ihtiyacım oldu. Biraz araştırmadan sonra güzel bir SQL buldum ve aşağıdaki kodu hazırladım. İşine yarayan olabilir diye paylaşayım.

$tablename = 'tablename';
 
$sql =  'SELECT
a.attname AS "Sutun",
pg_catalog.format_type(a.atttypid, a.atttypmod) AS "Veritipi"
FROM
pg_catalog.pg_attribute a
WHERE
a.attnum > 0
AND NOT a.attisdropped
AND a.attrelid = (
SELECT c.oid
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname ~ '^('.$tablename.')$'
AND pg_catalog.pg_table_is_visible(c.oid)
)';
 
$resulted_array = pg_fetch_all(pg_query($sql));

Benzer Konular

Tags: , , , , , ,


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button
12345 (No Ratings Yet)
Loading ... Loading ...

Leave a Reply