时间:2021-05-24
获取表名及注释:
select relname as tabname,cast(obj_description(relfilenode,'pg_class') as varchar) as comment from pg_class c where relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%' order by relname过滤掉分表:
加条件 and relchecks=0 即可
获取字段名、类型、注释、是否为空:
SELECT col_description(a.attrelid,a.attnum) as comment,format_type(a.atttypid,a.atttypmod) as type,a.attname as name, a.attnotnull as notnull FROM pg_class as c,pg_attribute as a where c.relname = '表名' and a.attrelid = c.oid and a.attnum>0补充:PostgreSQL查询表主键及注释内容
网上关于pgSql获取表主键的内容都是千篇一律,并且对于存在多主键的场景不支持。
附上测试后可获取多个主键字段值的SQL
SELECT string_agg(DISTINCT t3.attname,',') AS primaryKeyColumn ,t4.tablename AS tableName , string_agg(cast(obj_description(relfilenode,'pg_class') as varchar),'') as commentFROM pg_constraint t1 INNER JOIN pg_class t2 ON t1.conrelid = t2.oid INNER JOIN pg_attribute t3 ON t3.attrelid = t2.oid AND array_position(t1.conkey,t3.attnum) is not null INNER JOIN pg_tables t4 on t4.tablename = t2.relname INNER JOIN pg_index t5 ON t5.indrelid = t2.oid AND t3.attnum = ANY (t5.indkey) LEFT JOIN pg_description t6 on t6.objoid=t3.attrelid and t6.objsubid=t3.attnumWHERE t1.contype = 'p' AND length(t3.attname) > 0 AND t2.oid = '表名' :: regclass group by t4.tablename目前只找到了获取指定表的主键信息,对于批量获取没有找到。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
1、修改字段名: altertable表名renamecolumnAtoB 2、修改字段类型: altertable表名altercolumn字段名t
1、加字段:altertable表名ADD字段名类型;eg:altertablesys_cwzdADDSCCLLJVARCHAR2(50);2、加备注:comm
--查询数据库里所有表名和字段名的语句--SQL查询所有表名:SELECTNAMEFROMSYSOBJECTSWHERETYPE='U'SELECT*FROMI
1.获取表中普通信息:如字段名,字段类型等SELECTcolumn_name,data_type,ordinal_position,is_nullableFRO
下面为大家介绍利用SQL查询语句获取Mysql数据库中表的表名,表描述、字段ID、字段名、数据类型、长度、精度、是否可以为null、默认值、是否自增、是否是主键