PostgreSql 查询表结构和说明

select
(select relname from  pg_class where oid=a.attrelid) relname ,
(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 when atttypmod-4>0 then atttypmod-4 else 0 end)data_length,

(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype=‘p‘)>0 then ‘Y‘ else ‘N‘ end) as 主键约束,

(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype=‘u‘)>0 then ‘Y‘ else ‘N‘ end) as 唯一约束,

(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype=‘f‘)>0 then ‘Y‘ else ‘N‘ end) as 外键约束,

(case when 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 oid from pg_class where relname in(select relname from pg_class where relkind =‘r‘ and relname not like ‘pg_%‘ ))

order by relname,a.attnum;

原文地址:https://www.cnblogs.com/hzy168/p/10293408.html

时间: 2024-11-14 11:42:50

PostgreSql 查询表结构和说明的相关文章

PostgreSQL - 查询表结构和索引信息

前言 PostgreSQL的表一般都是建立在public这个schema下的,假如现在有个数据表t_student,可以用以下几种方式来查询表结构和索引信息. 使用\d元命令查看表字段信息和索引信息 在cmd界面使用psql连接db后,输入\d加上表名即可: \d t_student 通过系统数据字典查询表结构 select col.table_schema, col.table_name, col.ordinal_position, col.column_name, col.data_type

Postgresql查询表和表结构

查询表名 SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg%' AND tablename NOT LIKE 'sql_%' ORDER BY tablename; 或者 select tablename from pg_tables where schemaname='public' 查询表结构 select col.table_schema, col.table_name, col.ordinal_position, c

sql server 查询表结构

--查询表结构start SELECT 序号 = a.colorder,字段名称 = a.name,字段描述 = f.value, 标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity') = 1 then '√' else '' end, 主键 = case when exists(SELECT 1 FROM sysobjects where xtype = 'PK' and parent_obj = a.id and name in (

SQL Server里查询表结构命令

现提供两条命令查询表结构: 1.sp_help table_name; 如: [sql] sp_help Student; 2.sp_columns table_name; 如: [sql] sp_columns Student; 推荐使用sp_help table_name,因为这条命令查询出的表结构信息更加详细.

Sqlserver查询表结构信息-字段说明、类型、长度等信息

Sqlserver 中查询表结构信息-字段说明.类型.长度等信息综合语法. SELECT 表名 = d.name,--case when a.colorder=1 then d.name else '' end, 表说明 = case when a.colorder=1 then isnull(f.value,'') else '' end, 字段序号 = a.colorder, 字段名 = a.name, 标识 = case when COLUMNPROPERTY( a.id,a.name,'

SQL语句查询表结构

SQL语句查询表结构 刚刚在做一个小项目,数据库中一张表有20来个字段,用我以前做的一个.NET实体类生成器一个一个的输入还是闲麻烦,于是打算找个时间来重新的改造一个那个.NET实体类,能够通过选择的数据库及用户表自动生成实体类,而且该实体类还有注释,即字段的说明字段,然后再加一个功能,生成数据库文档,其实也就是把字段字,类型,字段说明输出一个漂亮的表格而已,只要能获取出来生成HTML表格就比较容易了,上网找了一下,得到如下有用的SQL语句: -- 查询非系统数据库 Select name FR

<经验杂谈>查询表结构的SQL语句

在我们使用SQL数据库的过程中,经常会遇到查询表结构的情况,以下就是sql语句的写法: --查询非系统数据库 SELECT name FROM Master..SysDatabases WHERE dbid > 4 查询数据库下所有表 SELECT name FROM sysobjects WHERE xtype = 'U' OR xtype = 'S' 查询所有用户表 SELECT name FROM sysobjects WHERE xtype = 'U' AND OBJECTPROPERT

sqlserver 查询表结构,字段,类型,长度,描述信息通通有

select sys.columns.name as 字段名, sys.types.name as 字段类型, sys.columns.max_length as 长度, sys.columns.is_nullable as 是否可空, (select count(*) from sys.identity_columns where sys.identity_columns.object_id = sys.columns.object_id and sys.columns.column_id =

SQL语句 & 查询表结构

[group by] 对结果集进行分组,常与汇总函数一起使用. SELECT column,SUM(column) FROM table GROUP BY column HAVING 通常与 GROUP BY 子句同时使用.不使用 GROUP BY 时, HAVING 则与 WHERE 子句功能相似. Company                          Amount W3Schools                         5500 IBM