sql: postgreSQL sql script

SELECT * 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

SELECT 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表的字段信息 20150402 Geovin Du 涂聚文
SELECT 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.oid

SELECT * 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_kb
FROM pg_class c, pg_class c2, pg_index i
WHERE c.relname = ‘bookkindlist‘ AND
c.oid = i.indrelid AND
c2.oid = i.indexrelid
ORDER 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 = 24610
select * from pg_get_functiondef(24610);

--查看表大小(不含索引等信息)
select pg_relation_size(‘bookkindlist‘);                         --368640 byte
select pg_size_pretty(pg_relation_size(‘bookkindlist‘))   --360 kB

--查看DB大小
select pg_size_pretty(pg_database_size(‘geovindu‘));   --12M

--查看服务器DB运行状态
[[email protected] ~]$ pg_ctl status -D $PGDATA
pg_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.description
FROM
    information_schema.columns col LEFT JOIN pg_description des
        ON col.table_name::regclass = des.objoid
    AND col.ordinal_position = des.objsubid
WHERE
    table_schema = ‘public‘
    AND table_name = ‘bookkindlist‘
ORDER BY
    ordinal_position;

select * from pg_namespace

select * from pg_class where relname=‘bookkindlist‘
---
SELECT
n.nspname ,
relname
FROM
pg_class c ,
pg_namespace n
WHERE
c.relnamespace = n.oid
AND nspname=‘public‘
AND relkind = ‘r‘
AND relhassubclass
ORDER BY
nspname ,
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.atttypid
inner 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‘;
-- 主键字段名attname

select * from pg_attribute;
select * from pg_attribute where attstorage=‘p‘;
select attrelid,attname,attnum from pg_attribute where attstorage=‘p‘;

  

时间: 2024-11-08 23:40:27

sql: postgreSQL sql script的相关文章

sql:PostgreSQL

PostgreSQL sql script: -- Database: geovindu -- DROP DATABASE geovindu; CREATE DATABASE geovindu WITH OWNER = postgres ENCODING = 'UTF8' TABLESPACE = pg_default LC_COLLATE = 'Chinese_People''s Republic of China.936' LC_CTYPE = 'Chinese_People''s Repu

SQL Tuning Health-Check Script (SQLHC)

1. 纯手工打造 工具:程序员的双手 特点:手写客户端与服务器端验证代码 2. 半手工半自动 工具:jquery.validate(客户端) + DataAnnotations & DataAnnotationsExtensions(服务器端) 特点:客户端手写部分验证代码,服务器端只需声明验证规则 3. 全自动 工具:jquery.validate & jquery.validate.unobtrusive (客户端) + DataAnnotations & DataAnnota

Hibernate SQL实际sql语句监控- p6spy+hibernate+proxool 设置

由于ORM工具的缘故,我们调试程序的时候远没有直接在程序里直接写个string的SQL简单,想当年查个sql是有多么的幸福,一行sql = "select * from ..."找找就行了, 现在用了hibernate, 就算在JPA中开了show_sql=true, 打出来的还是一串 "name = ? and id = ? " 好在找到一个工具, p6spy, 作为真正JDBC的一个代理, 截获了所有sql, 让我们看到一个真实sql的机会. 先下载,以下配置默

【SQL】sql版Split函数。用于拆分字符串为单列表格

原文:[SQL]sql版Split函数.用于拆分字符串为单列表格 功能与.net版string.Split函数类似,只不过.net返回的是数组,这个返回的是一个单列表格,每个拆分出来的子串占一行.可选是否移除空格子串和重复项.市面上类似的函数不算少,但大多都是在循环中对原串进行改动,我感觉这样不好,虽然不知道sql的字符串是不是像.net的一样具有不可变性,但感觉尽量不要去动原串最好,万一sql的字串也不可变,那变一次就要产生一份,尤其是每圈循环都在变,内存消耗让人心疼,所以才有重新造个轮子的想

使用showplan.sql分析sql Performance

在HelloDBA网站找到一个分析sql性能的工具-showplan,记录一下 showplan.sql下载路径:http://www.HelloDBA.com/Download/showplan.zip 使用方式就是调用该工具,传入SQL_ID作为参数. SQL> @/dmp/showplan.sql 26xj87b2f8g6u Usage: @showplan <SQL_ID> [Plan Hash Value] [Details: [+](B)inds|SQL (T)ext|(Pe

SQL Server SQL性能优化之--通过拆分SQL提高执行效率,以及性能高低背后的原因

复杂SQL拆分优化 拆分SQL是性能优化一种非常有效的方法之一, 具体就是将复杂的SQL按照一定的逻辑逐步分解成简单的SQL,借助临时表,最后执行一个等价的逻辑,已达到高效执行的目的 一直想写一遍通过拆分SQL来优化的博文,最近刚好遇到一个实际案例,比较有代表性,现分享出来, 我们来通过一个案例来分析,为什么拆分语句可以提高SQL执行效率,更重要的是弄清楚,拆分前为什么慢,拆分后为什么快了? 幼稚的话,各位看官莫笑 先看一下相关表的数据量,大表也有5900多万,小表有160多万 (声明:我从来没

查询Oracle正在运行SQL的SQL命令

查询Oracle正在运行SQL的SQL命令: select a.EVENT, a.SQL_ID, a.MACHINE, b.SQL_TEXT, b.SQL_FULLTEXT,b.FIRST_LOAD_TIME,b.LAST_LOAD_TIME,b.LAST_ACTIVE_TIME from v$session a, v$sql b where a.SQL_ID = b.SQL_ID and a.USERNAME is not null and a.STATUS = 'ACTIVE'

[sql异常]SQL Server 阻止了对组件 &#39;Ad Hoc Distributed Queries&#39; 的

//执行远程数据库表查询 select * from openrowset( 'SQLOLEDB', '192.168.5.547'; 'sa'; '密码',[数据库名称].[dbo].[表名]) 出现异常: 消息 15281,级别 16,状态 1,第 1 行SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭.系统管理员

2.PL/SQL——PL/SQL概述及其基本结构

一.什么是PL/SQL PL/SQL是对SQL语言的扩展. SQL语言是一种非过程性语言,即用户只需要告诉计算机做什么,而不需要理解计算机是怎么做的.比如用户要查询一张表里的某一列数据,只需要输入:selectCOLUNM_NAME from TABLE_NAME where CONDITION:就可以了,而不需要了解计算机是如何查询的.这既是优点也是缺点,优点就是该语言非常容易学习,缺点则是缺乏过程控制,限制了其功能,比如无法进行if...else的条件判断,也无法进行for循环操作. 基于S