Postgres中数据字典的用法(二)

4.pg_attribute

postgres=# \d+ pg_attribute;
                          数据表 "pg_catalog.pg_attribute"
     栏位      |   类型    |  可空的  |   存储   | 描述
---------------+-----------+----------+----------+------
 attrelid      | oid       | not null | plain    |列所属的表
 attname       | name      | not null | plain    |列名
 atttypid      | oid       | not null | plain    |列的数据类型
 attstattarget | integer   | not null | plain    |attstattarget控制由ANALYZE对此列收集的统计信息的细节层次。
 attlen        | smallint  | not null | plain    |本列类型的pg_type.typlen一个拷贝
 attnum        | smallint  | not null | plain    |列的编号
 attndims      | integer   | not null | plain    |如果该列是一个数组类型,这里就是其维度数;否则为0。
 attcacheoff   | integer   | not null | plain    |在存储中总是为-1
 atttypmod     | integer   | not null | plain    |atttypmod记录了在表创建时提供的类型相关数据
 attbyval      | boolean   | not null | plain    |该列类型的pg_type.typbyval的一个拷贝
 attstorage    | "char"    | not null | plain    |通常是该列类型的pg_type.typstorage的一个拷贝
 attalign      | "char"    | not null | plain    |通常是该列类型的pg_type.typalign的一个拷贝该列类型的pg_type.typstorage的一个拷贝。
 attnotnull    | boolean   | not null | plain    |这表示一个非空约束
 atthasdef     | boolean   | not null | plain    |该列有一个默认值,在此情况下在pg_attrdef目录中会有一个对应项来真正记录默认值。
 atthasmissing | boolean   | not null | plain    |该列在行中完全缺失时会用到这个列的值
 attidentity   | "char"    | not null | plain    |如果是一个零字节(''),则不是一个标识列。否则,a =总是生成,d = 默认生成。
 attisdropped  | boolean   | not null | plain    |该列被删除且不再有效。
 attislocal    | boolean   | not null | plain    |该列是由关系本地定义的。
 attinhcount   | integer   | not null | plain    |该列的直接祖先的编号。
 attcollation  | oid       | not null | plain    |该列被定义的排序规则,如果该列不是一个可排序数据类型则为0。
 attacl        | aclitem[] |          | extended |列级访问权限
 attoptions    | text[]    |          | extended |属性级选项,以“keyword=value”形式的字符串
 attfdwoptions | text[]    |          | extended |属性级的外部数据包装器选项,以“keyword=value”形式的字符串
             这个列中是一个含有 一个元素的数组,其中的值被用于该列在行中完全缺失时,
             如果在行创建之后增加一个有非易失DEFAULT值的列,就会发生这种情况attmissingval | anyarray  |          | extended |
索引:
    "pg_attribute_relid_attnam_index" UNIQUE, btree (attrelid, attname)
    "pg_attribute_relid_attnum_index" UNIQUE, btree (attrelid, attnum)

5. pg_constraint

目录pg_constraint存储表上的检查、主键、唯一、外键和排他约束(列约束也不会被特殊对待。每一个列约束都等同于某种表约束。)

postgres=# \d+ pg_constraint;
                           数据表 "pg_catalog.pg_constraint"
     栏位      |     类型     |  可空的  |   存储   |描述
---------------+--------------+----------+----------+-----
 conname       | name         | not null | plain    |约束名字(不需要唯一!)
 connamespace  | oid          | not null | plain    | 包含此约束的名字空间的OID
 contype       | "char"       | not null | plain    |c = 检查约束, f =外键约束, p = 主键约束, u = 唯一约束, t = 约束触发器, x = 排他约束
 condeferrable | boolean      | not null | plain    |该约束是否能被延迟
 condeferred   | boolean      | not null | plain    |该约束是否默认被延迟
 convalidated  | boolean      | not null | plain    |此约束是否被验证过
 conrelid      | oid          | not null | plain    该约束所在的表,如果不是表约束则为0
 contypid      | oid          | not null | plain    |该约束所在的域,如果不是域约束则为0
 conindid      | oid          | not null | plain    |如果该约束是唯一、主键、外键或排他约束,此列表示支持此约束的索引,否则为0
 conparentid   | oid          | not null | plain    |如果这是一个分区中的约束,则是父分区表中对应的约束;否则为0
 confrelid     | oid          | not null | plain    |如果此约束是一个外键约束,此列为被引用的表,否则为0
 confupdtype   | "char"       | not null | plain    |
 confdeltype   | "char"       | not null | plain    |
 confmatchtype | "char"       | not null | plain    |
 conislocal    | boolean      | not null | plain    |
 coninhcount   | integer      | not null | plain    |
 connoinherit  | boolean      | not null | plain    |
 conkey        | smallint[]   |          | extended |
 confkey       | smallint[]   |          | extended |
 conpfeqop     | oid[]        |          | extended |
 conppeqop     | oid[]        |          | extended |
 conffeqop     | oid[]        |          | extended |
 conexclop     | oid[]        |          | extended |
 conbin        | pg_node_tree |          | extended |
 consrc        | text         |          | extended |
索引:
    "pg_constraint_conrelid_contypid_conname_index" UNIQUE, btree (conrelid, contypid, conname)
    "pg_constraint_oid_index" UNIQUE, btree (oid)
    "pg_constraint_conname_nsp_index" btree (conname, connamespace)
    "pg_constraint_conparentid_index" btree (conparentid)
    "pg_constraint_contypid_index" btree (contypid)
有 OIDs:yes 

6.查看数据库关系表常用的SQL

  • 1)查看一个shcema下的所有关系表的字段、字段类型、字段名称、字段长度、数据类型小数点后精度等
select table_catalog as 数据库名,
       table_schema as 模式名,
    table_name  as 表名,
    column_name as 字段名,
    data_type  as 字段类型,
    character_maximum_length as 字段类型长度,
    ordinal_position aS 表字段排序,
    numeric_precision as 数据类型精度,
    numeric_scale as 数据类型小数点后精度,
    a.datetime_precision as 时间类型精度,
    a.is_nullable as 是否位空值
from   information_schema.columns a
where  a.table_schema ='public'
order by
      table_catalog,
   table_name,
   ordinal_position,
   column_name;

2)获取主键信息

select 
      pg_class.relname as pl_table,
   pg_attribute.attname as colname,
   pg_type.typname as tyepname,
   pg_constraint.conname as pk_name
from  pg_constraint
join  pg_class on pg_constraint.conrelid = pg_class.oid
join  pg_attribute on pg_attribute.attrelid = pg_class.oid
and   pg_attribute.attnum = pg_constraint.conkey[1]
join  pg_type  on pg_type.oid = pg_attribute.atttypid
where  pg_constraint.contype = 'p'
order by pg_class.relname;
  • 3)查询数据库下sequence
select c.relname,c.relkind from pg_class c where c.relkind = 'S';
  • 4)查看数据库一个shcema下的所有关系表的列信息,如表名、索引、唯一主键等
SELECT a.schemaname,
       a.tablename,
    a.indexname,
    b.amname,
    c.indexrelid,
    c.indnatts,
    c,indisunique,
    c.indisclustered,
    d.description,
    e.schemaname
from   pg_am b
left join pg_class f on b.oid = f.relam
left join pg_stat_all_indexes e on f.oid = e.indexrelid
left join pg_index c on e.indexrelid = c.indexrelid
left join pg_description D on c.indexrelid = d.objoid,
pg_indexes A
where a.schemaname = e.schemaname
and  a.tablename = e.relname
and  a.indexname = e.indexrelname
and e.schemaname = 'public';

你可能感兴趣的:(postgresql)