pgsql用户自定义表结构信息获取

####1. 获取表中普通信息:如字段名,字段类型等

SELECT column_name, data_type, ordinal_position, is_nullable 
FROM information_schema."columns"
WHERE "table_name"='TABLE-NAME' -- 将 'TABLE-NAME' 换成自己的表

####2.获取所有的表和视图

SELECT table_name, table_type FROM INFORMATION_SCHEMA.tables WHERE table_schema='public' AND table_type IN ('BASE TABLE','VIEW')

####3.获取约束注释

SELECT obj_description(oid, 'pg_constraint') AS d FROM pg_constraint WHERE conname = constraint_name;

####4.获取表的约束

-- conname 约束名称
-- contype 约束类型(p:主键, f:外键, c: 检查约束, u:唯一约束)
-- conkey 约束字段
-- confkey 外键字段
-- consrc 检查约束条件
-- confreltable 外键字段引用的表
SELECT conname, contype, conkey, consrc, 
	(SELECT array_agg(column_name::text) FROM INFORMATION_SCHEMA.COLUMNS WHERE ordinal_position = any(conkey) AND table_name= 'TABLE-NAME') AS conkey, 
	(SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE ordinal_position = any(confkey) AND table_name='TABLE-NAME') AS confkey, 
	(SELECT relname FROM pg_class WHERE oid = confrelid) AS confreltable
FROM pg_constraint WHERE conrelid=(SELECT oid FROM pg_class WHERE relname ='TABLE-NAME'); -- 将 'TABLE-NAME' 换成自己的表

####5.获取表的触发器

SELECT trigger_name, event_manipulation, event_object_table, action_statement, action_orientation, action_timing FROM INFORMATION_SCHEMA.TRIGGERS;

####6.获取字段的注释

--table_oid 表的oid
--col_position 字段的位置
SELECT col_description(table_oid, col_position);

你可能感兴趣的:(数据库,pgsql)