sql: postgreSQL sql script

--pg_catalog
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表的字段信息 Geovin Du 涂聚文 20150402
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运行状态
[postgres@192.168.20.165 ~]$ 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-02 00:58:55

sql: postgreSQL sql script的相关文章

面包含点-PostGresql SQL性能优化求助

问题描述 PostGresql SQL性能优化求助 点表:create table point_p(flong float8flat float8userid int4);insert into point_p(flongflatuserid) values (113.12655922.6553671);insert into point_p(flongflatuserid) values (113.02934522.6219592);insert into point_p(flongflatu

PostgreSQL SQL 语言:数据定义

本文档为PostgreSQL 9.6.0文档,本转载已得到原译者彭煜玮授权. 1.表基础 关系型数据库中的一个表非常像纸上的一张表:它由行和列组成.列的数量和顺序是固定的,并且每一列拥有一个名字.行的数目是变化的,它反映了在一个给定时刻表中存储的数据量.SQL并不保证表中行的顺序.当一个表被读取时,表中的行将以非特定顺序出现,除非明确地指定需要排序.这些将在Chapter 7介绍.此外,SQL不会为行分配唯一的标识符,因此在一个表中可能会存在一些完全相同的行.这是SQL之下的数学模型导致的结果,

PostgreSQL SQL OUTLINE插件sr_plan (保存、篡改、固定 执行计划)

标签 PostgreSQL , sql plan outline , 执行计划篡改 , query rewrite , sr_plan , pg plan hint 背景 功能较为强大的数据库,通常都有query rewrite的功能,比如JOIN时提升或下推条件,调整JOIN顺序等. 例如 create table a(id int, info text); create table b(id int, info text); create index idx_a_info on a (inf

jpa-JPA sql 原生sql 插入数据

问题描述 JPA sql 原生sql 插入数据 看了看,好像jpa往数据库(我用的MySql)里插入数据(对象),只能采用SQL原生语句.各位谁用过这玩意么? .createNativeQuery(insert into table values());就可以么? 如果是批量插入能搞么? 多谢!

PL/SQL动态SQL(原创)

概述 使用动态SQL是在编写PL/SQL过程时经常使用的方法之一.很多情况下,比如根据业务的需要,如果输入不同查询条件,则生成不同的执行SQL查询语句,对于这种情况需要使用动态SQL来完成.再比如,对于分页的情况,对于不同的表,必定存在不同的字段,因此使用静态SQL则只能针对某几个特定的表来形成分页.而使用动态的SQL,则可以对不同的表,不同的字段进行不同的分页.这些情况的处理通常都是用动态SQL来完成. 动态SQL和静态SQL静态SQL静态SQL通常用于完成可以确定的任务.比如传递部门号调用存

.NET编程和SQL Server ——Sql Server 与CLR集成 (学习笔记整理-1)

原文:.NET编程和SQL Server --Sql Server 与CLR集成 (学习笔记整理-1) 一.SQL Server 为什么要与CLR集成 1. SQL Server 提供的存储过程.函数等十分有限,经常需要外部的代码来执行一些繁重的移植: 2.与CLR集成可将原本需要独立的程序来实现的功能迁移到SQL Server 内部进行数据操作: 3.T-SQL数据查询语言在返回数据集方面很好,但是除此之外表现不佳.与CLR的集成可解决这一问题: 4..NET的操作代码和执行的速度比T-SQL

对存储过程进行加密和解密(SQL 2008/SQL 2012)

原文 http://www.cnblogs.com/wghao/archive/2012/12/30/2837642.html 开始:  在网络上,看到有SQL Server 2000和SQL Server 2005 的存储过程加密和解密的方法,后来分析了其中的代码,发现它们的原理都是一样的.后来自己根据实际的应用环境,编写了两个存储过程,一个加密存储过程 (sp_EncryptObject),和一个解密存储过程(sp_EncryptObject),它们可以应用于SQL Server中的储过程,

卸载了sql,重新安装sql后,一直连接超时

问题描述 卸载了sql,重新安装sql后,一直连接超时 今天一时冲动卸载了SQL,在重新安装的过程中,改了端口为3307,把选择服务器类型改为Developer Machine. 然后在运行一个卸载SQL之前能运行的小程序,运行不了,说连接超时. 我已经试过网上说的那个wait_timeout,这个也试过了,没用 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last pa

SQL, PL/SQL 之NUMBER数据类型

    NUMBER数据类型在Oracle中使用的较为广泛,可以存储零值,正负数,以及定长数,对于这个数据类型有个几个概念要搞清,否则容易搞混,下面给出具体描述.   1.可表示范围及存储空间    从1.0 x 10-130 到 1.0 x 10126(不包括),如果表达式或值大于1.0 x 10126,Oracle会返回错误信息    所需的存储空间为1到22个字节 2.Number类型表示法    NUMBER(p,s)   P 和S 可选     其中precision表示数字的总长度,