sql: postgreSQL sql script

来源:互联网 发布:淘宝网触屏版不能操作 编辑:程序博客网 时间:2024/06/07 11:44
--pg_catalogSELECT * from pg_class c,pg_attribute a,pg_type t where c.relname='BookKindList' and a.attnum>0 and a.attrelid=c.oid and a.atttypid=t.oidSELECT a.attname from pg_class c,pg_attribute a,pg_type t where c.relname='BookKindList' and a.attnum>0 and a.attrelid=c.oid and a.atttypid=t.oid--查询BookKindList表的字段信息 Geovin Du 涂聚文 20150402SELECT a.attnum,a.attname AS field,t.typname AS type,a.attlen AS length,a.atttypmod AS lengthvar,a.attnotnull AS notnull from pg_class c,pg_attribute a,pg_type t where c.relname='BookKindList' and a.attnum>0 and a.attrelid=c.oid and a.atttypid=t.oidSELECT * FROM information_schema.columns; --查表的列表SELECT * FROM information_schema.columns where table_catalog='geovindu' and table_schema='public' and table_name='bookkindlist';--select * from pg_database where datname='bookkindlist';select datname,dattablespace from pg_database where datname='bookkindlist';--查看数据库select * from pg_database;--查看表空间select * from pg_tablespace; --查看语言select * from pg_language; --查看角色用户select * from pg_user;select * from pg_shadow;select * from pg_roles; --查看会话进程select * from pg_stat_activity; --查看表SELECT * FROM pg_tables where schemaname = 'public'; --查看表字段select * from information_schema.columns where table_schema = 'public' and table_name = 'bookkindlist'; --查看视图select * from pg_views where schemaname = 'public';select * from information_schema.views where table_schema = 'public'; --查看触发器select * from information_schema.triggers; --查看序列select * from information_schema.sequences where sequence_schema = 'public';  --查看约束select * from pg_constraint where contype = 'p'  --u unique,p primary,f foreign,c check,t trigger,x exclusion select a.relname as table_name,b.conname as constraint_name,b.contype as constraint_type from pg_class a,pg_constraint b where a.oid = b.conrelid and a.relname = 'bookkindlist'; --查看索引select * from pg_index ; --查看表上存在哪些索引以及大小select relname,n.amname as index_type from pg_class m,pg_am n where m.relam = n.oid and m.oid in (select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 'bookkindlist'); SELECT c.relname,c2.relname, c2.relpages*8 as size_kbFROM pg_class c, pg_class c2, pg_index iWHERE c.relname = 'bookkindlist' ANDc.oid = i.indrelid ANDc2.oid = i.indexrelidORDER BY c2.relname;  --查看索引定义select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 'bookkindlist';select pg_get_indexdef(b.indexrelid); --查看过程函数定义select oid,* from pg_proc where proname = 'insert_platform_action_exist'; --oid = 24610select * from pg_get_functiondef(24610); --查看表大小(不含索引等信息)select pg_relation_size('bookkindlist');                         --368640 byteselect pg_size_pretty(pg_relation_size('bookkindlist'))   --360 kB --查看DB大小select pg_size_pretty(pg_database_size('geovindu'));   --12M --查看服务器DB运行状态[postgres@192.168.20.165 ~]$ pg_ctl status -D $PGDATApg_ctl: server is running (PID: 2373)/home/postgres/bin/postgres "-D" "/database/pgdata"  --查看每个DB的使用情况(读,写,缓存,更新,事务等)select * from pg_stat_database --查看索引的使用情况select * from pg_stat_user_indexes; --查看表所对应的数据文件路径与大小SELECT pg_relation_filepath(oid), relpages FROM pg_class WHERE relname = 'bookkindlist'; --查看索引与相关字段及大小 SELECT n.nspname AS schema_name,        r.rolname as table_owner,       bc.relname AS table_name,       ic.relname AS index_name,       a.attname  AS column_name,       bc.relpages*8 as index_size_kb       FROM pg_namespace n,       pg_class bc,             -- base class       pg_class ic,             -- index class       pg_index i,       pg_attribute a,           -- att in base       pg_roles r  WHERE bc.relnamespace = n.oid     and i.indrelid = bc.oid     and i.indexrelid = ic.oid     and bc.relowner = r.oid     and i.indkey[0] = a.attnum     and i.indnatts = 1     and a.attrelid = bc.oid     and n.nspname = 'public'     and bc.relname = 'bookkindlist'  ORDER BY schema_name, table_name, index_name, attname; --查看PG锁select * from pg_locks; 备注:relpages*8 是实际所占磁盘大小 --查看表空间大小select pg_tablespace_size('pg_default'); --查看序列与表的对应关系  WITH fq_objects AS (SELECT c.oid,c.relname AS fqname ,                           c.relkind, c.relname AS relation                    FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace ),      sequences AS (SELECT oid,fqname FROM fq_objects WHERE relkind = 'S'),      tables    AS (SELECT oid, fqname FROM fq_objects WHERE relkind = 'r' )          SELECT       s.fqname AS sequence,       '->' as depends,       t.fqname AS table      FROM       pg_depend d JOIN sequences s ON s.oid = d.objid                  JOIN tables t ON t.oid = d.refobjid           WHERE       d.deptype = 'a' and t.fqname = 'bookkindlist';--select * from information_schema.columns where table_catalog= 'geovindu' AND table_name = 'bookkindlist';select * from pg_description;  ---一个查询表结构的SQL  SELECT    col.table_schema ,    col.table_name ,    col.ordinal_position,    col.column_name ,    col.data_type ,    col.character_maximum_length,    col.numeric_precision,    col.numeric_scale,    col.is_nullable,    col.column_default ,    des.descriptionFROM    information_schema.columns col LEFT JOIN pg_description des        ON col.table_name::regclass = des.objoid    AND col.ordinal_position = des.objsubidWHERE    table_schema = 'public'    AND table_name = 'bookkindlist'ORDER BY    ordinal_position;select * from pg_namespaceselect * from pg_class where relname='bookkindlist'---SELECTn.nspname ,relnameFROMpg_class c ,pg_namespace nWHEREc.relnamespace = n.oidAND nspname='public'AND relkind = 'r'AND relhassubclassORDER BYnspname ,relname;--select * from information_schema.columns where table_name = 'bookkindlist';--表结构select table_schema,table_name,column_name,data_type,column_default,character_maximum_length,is_nullable from information_schema.columns where table_name = 'bookkindlist';select table_schema,table_name,column_name as FieldName,data_type as   FieldType,column_default,character_maximum_length as   FieldLength,is_nullable from information_schema.columns where table_name = 'bookkindlist';--表主键名称select pg_constraint.conname as pk_name from pg_constraint  inner join pg_class  on pg_constraint.conrelid = pg_class.oid where pg_class.relname = 'bookkindlist' and pg_constraint.contype='p';--表主键字段select pg_constraint.conname as pk_name,pg_attribute.attname as column_name,pg_type.typname as data_type,pg_class.relname as table_name, info.character_maximum_length,info.is_nullable  from pg_constraint  inner join pg_class on pg_constraint.conrelid = pg_class.oid inner join pg_attribute on pg_attribute.attrelid = pg_class.oid and  pg_attribute.attnum = pg_constraint.conkey[1]inner join pg_type on pg_type.oid = pg_attribute.atttypidinner join information_schema.columns as info on info.column_name=pg_attribute.attname where pg_class.relname = 'bookkindlist' and pg_constraint.contype='p';--表主键名称select conname,conrelid,connamespace from pg_constraint where contype='p';select * from pg_constraint where contype='p';---表和表主键名称select oid,relname from pg_class;select * from pg_class where relnamespace=2200;select * from pg_class;--表select * from pg_type where typnamespace=2200;select typnamespace,typname,oid from pg_type where typnamespace=2200;select * from pg_type where typname='bookkindlist';-- 主键字段名attnameselect * from pg_attribute;select * from pg_attribute where attstorage='p';select attrelid,attname,attnum from pg_attribute where attstorage='p';

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我在淘宝上买了货不发货咋怎么办 在快递公司寄的东西丢了我该怎么办 不小心给了快递员子一个差评怎么办 不小心用发霉了的杯子喝了水怎么办 唐三复活了小舞失去的魂环怎么办了 我该怎么办?身陷动漫城输了很多钱 庄家开2球大小球踢成2球怎么办 去哪儿网订机票时邮箱写错了怎么办 在南航航班上把手机丢飞机上怎么办 买了品牌鞋穿了一周就破了怎么办 狗让狠狠的打了一顿不理人了怎么办 调好米粉宝宝吃的时候就凉了怎么办 情人间闹分手删了微信后悔了怎么办 8个月宝宝不坐椅子一直要抱怎么办 2个月婴儿3天没有拉大便了怎么办 8个月的宝宝不吃米糊和稀饭怎么办 2岁零5个月的宝宝不说话怎么办 两岁宝宝不拔掉老是拉在裤上怎么办 一岁的宝宝吞了一颗五子棋该怎么办 别人欠我钱还把我拉黑我该怎么办 欠我钱的人耍赖不还我该怎么办 交易猫买的炉石传说号被找回怎么办 淘宝上卖水果过季了不想下架怎么办 两岁宝宝被蚊子咬了挠破流水怎么办 我打了人一拳他就躺地下了怎么办 在微信上被认识的人骗了钱该怎么办 微信上面被不认识的人骗了钱怎么办 柜体和订做的柜门颜色对不上怎么办 拉鞭炮的车压了我的电车不陪怎么办 脚爱出汗穿高跟凉鞋总往前滑怎么办 视频的格式是VⅠD打开很慢怎么办 汕头普法学法我点了考试没考怎么办 德云的生活攻略第三天卡关了怎么办 我的世界房子被参观的人烧了怎么办 新买的手表返厂维修弄划伤了怎么办 糖猫手表丢了别人捡了换了卡怎么办 我妈总怀疑我爸偷她东西怎么办啊 在百度上买的演出票不配送了怎么办 北交大预报名信息填错了怎么办保研 我租了个店面房子但是写了拆怎么办 电话换了微信账号密码都忘了怎么办