POSTGRESQL Export Table structure information

Source: Internet
Author: User
Tags postgresql

Project team to the table structure of the document, handwriting is too cumbersome to use the SLQ script to export a copy.

--Queries All table field information (with table name)

Select

(Select Relname| | ' --' | | (select description from pg_description where objoid=oid and objsubid=0) as comment from Pg_class where oid=a.attrelid) as TABLE_NAME,

A.attname as column_name,

Format_type (A.atttypid,a.atttypmod) as Data_type,

(case if Atttypmod-4>0 then atttypmod-4 else 0 end) Data_length,

(Case if (select COUNT (*) from pg_constraint where conrelid = A.attrelid and Conkey[1]=attnum and contype= ' P ') >0 then ' Y ' Else ' N ' end) as PRIMARY KEY constraint,

(Case if (select COUNT (*) from pg_constraint where conrelid = A.attrelid and Conkey[1]=attnum and contype= ' u ') >0 Then ' Y ' Else ' N ' end) as the only constraint,

(Case if (select COUNT (*) from pg_constraint where conrelid = A.attrelid and Conkey[1]=attnum and contype= ' F ') >0 then ' Y ' Else ' N ' end) as foreign KEY constraint,

(case is a.attnotnull=true then ' Y ' Else ' N ' end) as nullable,

Col_description (A.attrelid,a.attnum) as comment

From Pg_attribute A

where Attstattarget=-1 and Attrelid in (select OIDs from Pg_class where Relname in (select Relname from Pg_class where Relki nd = ' r ' and relname like ' exg_% '))

Order BY Table_name,a.attnum;

Execute the SQL statement and export the query results

Use comma delimiter, export CSV file, can be edited directly in Excle

Use local character set to prevent garbled characters

?

Attached: Other SQL scripts

--Query table name and description

Select Relname as TABLE_NAME, (select description from pg_description where objoid=oid and objsubid=0) as comment from Pg_c Lass where relkind = ' R ' and relname like ' exg_% ' ORDER by table_name;

?

--Query table field information

Select

A.attname as column_name,

Format_type (A.atttypid,a.atttypmod) as Data_type,

(case if Atttypmod-4>0 then atttypmod-4 else 0 end) Data_length,

(Case if (select COUNT (*) from pg_constraint where conrelid = A.attrelid and Conkey[1]=attnum and contype= ' P ') >0 then ' Y ' Else ' N ' end) as PRIMARY KEY constraint,

(Case if (select COUNT (*) from pg_constraint where conrelid = A.attrelid and Conkey[1]=attnum and contype= ' u ') >0 Then ' Y ' Else ' N ' end) as the only constraint,

(Case if (select COUNT (*) from pg_constraint where conrelid = A.attrelid and Conkey[1]=attnum and contype= ' F ') >0 then ' Y ' Else ' N ' end) as foreign KEY constraint,

(case is a.attnotnull=true then ' Y ' Else ' N ' end) as nullable,

Col_description (A.attrelid,a.attnum) as comment

From Pg_attribute A

where attstattarget=-1 and Attrelid = (select oid from pg_class where relname = ' exg_ms_alarm ');--Table name

POSTGRESQL Export Table structure information

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.