时间:2021-05-24
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 confreltableFROM 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);补充:查询PostgreSQL库中所有表的表结构信息SQL
我就废话不多说了,大家还是直接看代码吧~
select(select relname as comment from pg_class where oid=a.attrelid) as table_name, row_number() over(partition by (select relname as comment from pg_class where oid=a.attrelid) order by a.attnum),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 '是' else '否' end) as 主键约束,(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='u')>0 then '是' else '否' end) as 唯一约束,(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='f')>0 then '是' else '否' end) as 外键约束,(case when a.attnotnull=true then '是' else '否' end) as nullable,col_description(a.attrelid,a.attnum) as commentfrom pg_attribute awhere attstattarget=-1 and attrelid in (select oid from pg_class where relname in(select relname from pg_class where relkind ='r' and relname in (select tablename from pg_tables where tablename not like 'pg_%' and tablename not like 'sql_%' and schemaname not in(XXXX) and tablename not in(XXXX)))order by table_name,a.attnum;以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
介绍了帝国cms系统中通过用户自定义函数获取用户发表新闻数量的方法,主要是分享下帝国cms自定义函数功能。在帝国cms中实现获取用户发表的新闻数量,没有现成的方
自定义列表一、自定义列表功能说明自定义列表功能:除了系统内置固定的栏目页、专题页等列表页面外,用户还可以通过SQL条件生成相应信息列表页面,实现各种信息集合列表
1.自定义用户表注意事项必须在settings中配置AUTH_USER_MODEL这个字段#覆盖默认的用户模型,使用自定义用户模型#语法:'app的名称.自定义
前言SQLServer开发过程中,为了传入数据集类型的变量(比如接受C#中的DataTable类型变量),需要定义“用户自定义表类型”,通过“用户自定义表类型”
在帝国cms中实现获取用户发表的新闻数量,没有现成的方法,不过可以用自定义函数来实现,一起来看看。帝国cms中添加用户自定义函数。就是在e/class/user