---
title: "oracle常用查询语句"
author: "Edision"
date: "2012-04-23T17:31:52Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2012-04-23T17:31:37Z"
id: 1495610
url: "https://edision.iteye.com/blog/1495610"
---

--查询表的大小  
select t.owner,  
       t.segment\_name,  
       (sum(t.blocks) * 8) / 1024 || 'M' as S,  
       t.segment\_type  
  from dba\_segments t  
 where t.owner = 'USER\_NAME'  
 group by t.owner, t.segment\_name, t.segment\_type  
 order by t.owner, S desc

--修改表所在表空间

select T.owner,  t.segment\_name, t.tablespace\_name --, sum(t.blocks)  
  from dba\_segments t  
 where t.owner = 'USER\_NAME'  
ORDER BY T.owner

select 'alter table ' || t.TABLE\_NAME || ' move tablespace BIP\_TS;' from user\_all\_tables t  
select 'alter index '|| index\_name ||' rebuild tablespace BIP\_TS;' from user\_indexes;

--查看索引是否生效

  select index\_name, index\_type, tablespace\_name, table\_type, status  
  from user\_indexes t  
 where t.tablespace\_name = 'TABLESPACENAME'  
   and status = 'UNUSABLE'

-- 清空回收站

purge recyclebin;

--查询建表语句

select dbms\_metadata.get\_ddl('TABLE','UNIEAP\_REPORT\_CATEGORY') FROM DUAL

select DBMS\_METADATA.GET\_DDL('TABLESPACE','BIP\_TS') FROM DUAL

--获取授权信息

select dbms\_metadata.get\_granted\_ddl('SYSTEM\_GRANT') from dual;

--查询表空间使用率的语句  
select a.a1 表空间名称,  
       c.c2 类型,  
       c.c3 区管理,  
       b.b2 / 1024 / 1024 表空间大小M,  
       (b.b2 - a.a2) / 1024 / 1024 已使用M,  
       substr((b.b2 - a.a2) / b.b2 * 100, 1, 5) 利用率  
  from (select tablespace\_name a1, sum(nvl(bytes, 0)) a2  
          from dba\_free\_space  
         group by tablespace\_name) a,  
       (select tablespace\_name b1, sum(bytes) b2  
          from dba\_data\_files  
         group by tablespace\_name) b,  
       (select tablespace\_name c1, contents c2, extent\_management c3  
          from dba\_tablespaces) c  
 where a.a1 = b.b1  
   and c.c1 = b.b1;

--修改分区名

alter table TABLE\_NAME rename partition OLD\_PARTITION\_NAME to NEW\_PARTITION\_NAME;

--查询分区脚本  
select TABLE\_NAME ,PARTITION\_NAME from user\_tab\_partitions where table\_name like '%'  
select TABLE\_NAME ,PARTITION\_NAME from user\_tab\_partitions where table\_name like '%'  
\--查询索引脚本  
SELECT INDEX\_NAME,TABLE\_NAME FROM USER\_INDEXES WHERE INDEX\_NAME LIKE '%'

--查看oracle连接用户  
select s.USERNAME,s.MACHINE from v$session s where s.STATUS = 'ACTIVE'

--查看oracle最大连接数  
select value from v$parameter where name = 'processes'

修改最大连接数:  
alter system set processes = 300 scope = spfile;