MySQL PostgresQL查询所有表结构

MySQL

查询表名及注释

select table_name as name,table_comment as remarks from information_schema.tables 
where table_schema= 表空间 and table_type='base table'

查询表对应列

select table_name as tableName,column_name as name,ordinal_position as sort,is_nullable as isNullable,column_type as type ,
column_key as columnKey,column_comment as comment,column_default as columnDefault from 
information_schema.columns where table_schema= 表空间

PostgresQL

查询报名、注释以及OID

SELECT a.oid,
       a.relname AS name,
       b.description AS remarks
  FROM pg_class a
       LEFT OUTER JOIN pg_description b ON b.objsubid=0 AND a.oid = b.objoid
 WHERE a.relnamespace = (SELECT oid FROM pg_namespace WHERE nspname= 模式名 ) 
   AND a.relkind='r'
 ORDER BY a.relname

查询所有列 根据OID关联

SELECT 
 A.attnum as sort,
	( SELECT description FROM pg_catalog.pg_description WHERE objoid = A.attrelid AND objsubid = A.attnum ) AS comment,
	A.attname as name,
	( select typname from pg_type where oid = A.atttypid) AS type,
	A.atttypmod AS data_type,
	attlen,case when attnotnull then '否' else '是' end as isNullable,atthasdef
FROM
	pg_catalog.pg_attribute A
WHERE
	1 = 1 
	AND A.attrelid = 此处填入OID 
	AND A.attnum > 0 
	AND NOT A.attisdropped 
ORDER BY
	A.attnum

查询主键

SELECT
	  pg_class.oid,
    pg_attribute.attname AS colName
FROM
    pg_constraint
INNER JOIN pg_class ON pg_constraint.conrelid = pg_class.oid
INNER JOIN pg_attribute ON pg_attribute.attrelid = pg_class.oid
AND pg_attribute.attnum = pg_constraint.conkey [1]
INNER JOIN pg_type ON pg_type.oid = pg_attribute.atttypid
WHERE
   pg_constraint.contype = 'p' order by oid
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值