`

Oracle维护常用SQL语句

阅读更多

查找数据库中所有字段  以对应的表
select C.column_name,C.TABLE_NAME from dba_tab_columns C where owner=''

查每个科目class 分数scro前三名
select id, name, class, scro
   from (select row_number() over(partition by class order by scro desc)  cnt,
                id,
                name,
                class,
                scro
           from student) a
  where a.cnt <= 3;

查找排序后的前三行
  select *
  from (select rw.*, rownum
          from (select *
                  from student d
                 where d.class = 'b'
                 order by d.scro desc) rw
         where rw.id >= 1
         order by rw.class desc) n
where rownum <= 3


表复制
insert into table_a (id,name,age) select b.id,b.name,b.age from table_b;

--删除表数据的触发器
CREATE OR REPLACE PROCEDURE delete_data
IS
BEGIN
   delete from test ;
   COMMIT;
EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
      NULL;
   WHEN OTHERS
   THEN
      RAISE;
END delete_data;


--定时删除  每隔5分钟执行一次的计划
DECLARE
  X NUMBER;
BEGIN
  SYS.DBMS_JOB.SUBMIT
  ( job       => X
   ,what      => 'delete_data;'
   ,next_date => to_date('25/08/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
   ,interval  => 'sysdate+1/24/12'
   ,no_parse  => FALSE
  );
  SYS.DBMS_OUTPUT.PUT_LINE('Job Number is: ' || to_char(x));
COMMIT;
END;

--查看当前oracle计划
select job,next_date,next_sec,failures,broken from user_jobs;

--删除一个job
begin
dbms_job.remove(46);--46为job号
end;

--给新表插入旧表对应字段的所有数据
insert into aaa(id,name) select b.id,b.name from bbb;


文章搜索:     【点击打包该文章】
【到本站论坛,与同行交流】

select * from all_users;                ##查看所有用户
select name from v$database;             ##查看当前数据库
database test;                    ##进入test数据库
select * from v$instance;                ##查看所有的数据库实例
shutdown immediate                ##关闭数据库
alter user sys identified by new_password;        ##更改用户密码
select username,password from dba_users; ##查看当实例中的用户和密码
show parameter control_files;           ## 查看控制文件;
select member from v$logfile;        ##查看日志文件
show parameter ;                 ## 查看数据库参数
select * from user_role_privs;        ##查看当前用户的角色
select username,default_tablespace from user_users; ##查看当前用户的缺省表空间
alter user system identified by [password]        ##修改用户的密码
ALTER USER "SCOTT" ACCOUNT UNLOCK                 ##解锁SCOTT用户
show parameter processes;                    ##查看最大会话数


查看当前库的所有数据表:
SQL> select TABLE_NAME from all_tables;
select * from all_tables;
SQL> select table_name from all_tables where table_name like ‘u’;
TABLE_NAME———————————————default_auditing_options

查看表结构:desc all_tables;

创建用户并赋予权限
###----------------------------创建用户并赋予权限------------------------------------####-
create user mpss
  identified by "mpss12"
  default tablespace TS_MPSS_DATA
  temporary tablespace TEMP;
 
  给用户赋予权限
grant connect to mpss;
grant resource,create session to mpss;  开发角色
grant create procedure to dbuser; #这些权限足够用于开发及生产环境



给用户授权
grant dba to spms;--授予DBA权限
grant unlimited tablespace to lxg;--授予不限制的表空间
grant select any table to lxg;--授予查询任何表
grant select any dictionary to lxg;--授予 查询 任何字典

删除用户
drop user mpss cascade;


建表空间
###---------------------------------建表空间------------------------------------####-
================建立表空间============================

CREATE TABLESPACE "TS_MPSS_DATA"
    LOGGING
    DATAFILE '/mpss/data/ts_mpss_data.dbf' SIZE 1024M
    EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT  AUTO
=====================================================
=================建立临时表空间============================

CREATE
    TEMPORARY TABLESPACE "SWVIP" TEMPFILE '/app/oracle/oradata/
    sworacle/SWVIP.dbf' SIZE 5M EXTENT MANAGEMENT LOCAL UNIFORM
    SIZE 1M
=====================================================


create tablespace TS_MPSS_DATA datafile '/mpss/data/ts_mpss_data.bdf ' size 1024m autoextend on ;      ###autoextend on 自动扩展

###------------------------------------------------------------------------------------####-


查看表空间
###----------------------------查看表空间大小------------------------------------####-
SELECT D.TABLESPACE_NAME "Name",
TO_CHAR(((((A.BYTES - DECODE(F.BYTES, NULL, 0, F.BYTES)) / 1024 / 1024)) /(A.BYTES / 1024 / 1024))*100,'99,990.9')  "used(%)",
TO_CHAR((DECODE(F.BYTES, NULL, 0, F.BYTES) / 1024 / 1024),'999,990.9') "Free (M)"
FROM SYS.DBA_TABLESPACES D, SYS.SM$TS_AVAIL A, SYS.SM$TS_FREE F
WHERE D.TABLESPACE_NAME = A.TABLESPACE_NAME
AND F.TABLESPACE_NAME (+) = D.TABLESPACE_NAME;
###--------------------------------------------------------------------------------------####-

SELECT D.TABLESPACE_NAME,SPACE "SUM_SPACE(M)",BLOCKS SUM_BLOCKS,SPACE-NVL(FREE_SPACE,0) "USED_SPACE(M)",
ROUND((1-NVL(FREE_SPACE,0)/SPACE)*100,2) "USED_RATE(%)",FREE_SPACE "FREE_SPACE(M)"
FROM
(SELECT TABLESPACE_NAME,ROUND(SUM(BYTES)/(1024*1024),2) SPACE,SUM(BLOCKS) BLOCKS
FROM DBA_DATA_FILES
GROUP BY TABLESPACE_NAME) D,
(SELECT TABLESPACE_NAME,ROUND(SUM(BYTES)/(1024*1024),2) FREE_SPACE
FROM DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)
UNION ALL --if have tempfile
SELECT D.TABLESPACE_NAME,SPACE "SUM_SPACE(M)",BLOCKS SUM_BLOCKS,
USED_SPACE "USED_SPACE(M)",ROUND(NVL(USED_SPACE,0)/SPACE*100,2) "USED_RATE(%)",
NVL(FREE_SPACE,0) "FREE_SPACE(M)"
FROM
(SELECT TABLESPACE_NAME,ROUND(SUM(BYTES)/(1024*1024),2) SPACE,SUM(BLOCKS) BLOCKS
FROM DBA_TEMP_FILES
GROUP BY TABLESPACE_NAME) D,
(SELECT TABLESPACE_NAME,ROUND(SUM(BYTES_USED)/(1024*1024),2) USED_SPACE,
ROUND(SUM(BYTES_FREE)/(1024*1024),2) FREE_SPACE
FROM V$TEMP_SPACE_HEADER
GROUP BY TABLESPACE_NAME) F
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+) ;



  查看表空间物理文件的名称及大小;
 
###--------------------表空间物理文件的名称及大小------------------------####-
  select tablespace_name, file_id, file_name,
    round(bytes/(1024*1024),0) total_space
    from dba_data_files
    order by tablespace_name;
###------------------------------------------------------------------------------------####-  
   

查看数据文件放置的路径
###------------------------------------------------------------------------------------####-  

SQL> col file_name format a50
SQL> select tablespace_name,file_id,bytes/1024/1024,file_name from dba_data_files order by file_id;
###------------------------------------------------------------------------------------####-  


    查看数据库库对象

    select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status;


用系统管理员,查看当前数据库有几个用户连接:

SQL> select username,sid,serial# from v$session;


扩表空间
###------------------------------------------------------------------------------------####-
alter tablespace G000 add datafile '/dev/vgbilling/rg000_lv03' SIZE 7500m;
给表G000增加一个7500m的逻辑卷'/dev/vgbilling/rg000_lv03'
###------------------------------------------------------------------------------------####-

检查被长时间锁的对象
###------------------------------------------------------------------------------------####-

SQL>select a.session_id,a.process,a.locked_mode,b.object_name,b.object_type,b.status from v$locked_object a,dba_objects b where a.object_id=b.object_id;
###------------------------------------------------------------------------------------####-





文章出处:http://www.diybl.com/course/7_databases/oracle/Oracleshl/200899/141376.html


sys用户登陆
创建表空间:
SQL> create tablespace lmsstemp datafile 'F:\ORADATA\LMSS\LMSSTEMP01.DBF' SIZE 1
024M extent management local;

给表空间增加数据文件
alter tablespace lmsstemp add datafile 'F:\ORADATA\LMSS\LMSSTEMP02.DBF' SIZE 1
024M;

更改表空间为自动扩展
SQL> alter database datafile 'F:\ORADATA\LMSS\LMSSTEMP01.DBF' autoextend on;

查看表空间信息
SQL> select file_name,tablespace_name,autoextensible from dba_data_files;

授权:
    create any table to leon




-- Create the user (用sys执行)
create user xx  identified by xx123 default tablespace lmss temporary tablespace TEMP profile DEFAULT;

-- Grant/Revoke role privileges (用sys执行)
grant connect to xx;

--创建视图给hy用户(用leon用户)

create or replace view view_tableName as
select column。。。 from table;

-- Grant/Revoke object privileges
grant select, update on RES_XIM_CARD to hy;

--创建同义词
create synonym RES_XIM_CARD for YY.RES_XIM_CARD ;

 

 

 

 

================================================================

在Oracle中查询表的大小和表空间的大小

有两种含义的表大小。
1. 一种是分配给一个表的物理空间数量,而不管空间是否被使用。可以这样查询获得字节数:
select segment_name, bytes
from user_segments
where segment_type = 'TABLE'  order by bytes desc;
 
或者
 Select Segment_Name,Sum(bytes)/1024/1024 From User_Extents Group By Segment_Name order by sum(bytes) desc;

另一种表实际使用的空间。这样查询:
analyze table T_TICKET compute statistics;
select (num_rows * avg_row_len)/1024/1024
from user_tables
where table_name = 'T_TICKET';

1、查看表空间的名称及大小

  select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size

  from dba_tablespaces t, dba_data_files d

  where t.tablespace_name = d.tablespace_name

  group by t.tablespace_name;
  

  2、查看表空间物理文件的名称及大小

  select tablespace_name, file_id, file_name,

  round(bytes/(1024*1024),0) total_space

  from dba_data_files

  order by tablespace_name;

  

  3、查看回滚段名称及大小

  select segment_name, tablespace_name, r.status,

  (initial_extent/1024) InitialExtent,(next_extent/1024) NextExtent,

  max_extents, v.curext CurExtent

  From dba_rollback_segs r, v$rollstat v

  Where r.segment_id = v.usn(+)

  order by segment_name ;

  

  4、查看控制文件

  select name from v$controlfile;

  

  5、查看日志文件

  select member from v$logfile;

  

  6、查看表空间的使用情况

  select sum(bytes)/(1024*1024) as free_space,tablespace_name

  from dba_free_space

  group by tablespace_name;

  

  SELECT A.TABLESPACE_NAME,A.BYTES TOTAL,B.BYTES USED, C.BYTES FREE,

  (B.BYTES*100)/A.BYTES "% USED",(C.BYTES*100)/A.BYTES "% FREE"

  FROM SYS.SM$TS_AVAIL A,SYS.SM$TS_USED B,SYS.SM$TS_FREE C

  WHERE A.TABLESPACE_NAME=B.TABLESPACE_NAME AND A.TABLESPACE_NAME=C.TABLESPACE_NAME;

  

  7、查看数据库库对象

  select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status;

  

  8、查看数据库的版本 

  Select version FROM Product_component_version

  Where SUBSTR(PRODUCT,1,6)='Oracle';

 

 

9、查看数据库的创建日期和归档方式

  Select Created, Log_Mode, Log_Mode From V$Database;

  

  10、捕捉运行很久的SQL

  column username format a12

  column opname format a16

  column progress format a8

  

  select username,sid,opname,

  round(sofar*100 / totalwork,0)    '%' as progress,

  time_remaining,sql_text

  from v$session_longops , v$sql

  where time_remaining <> 0

  and sql_address = address

  and sql_hash_value = hash_value

  /

  

  11、查看数据表的参数信息

  SELECT partition_name, high_value, high_value_length, tablespace_name,

  pct_free, pct_used, ini_trans, max_trans, initial_extent,

  next_extent, min_extent, max_extent, pct_increase, FREELISTS,

  freelist_groups, LOGGING, BUFFER_POOL, num_rows, blocks,

  empty_blocks, avg_space, chain_cnt, avg_row_len, sample_size,

  last_analyzed

  FROM dba_tab_partitions

  --WHERE table_name = :tname AND table_owner = :towner

  ORDER BY partition_position

  

  12、查看还没提交的事务

  select * from v$locked_object;

  select * from v$transaction;

  

  13、查找object为哪些进程所用

  select

  p.spid,

  s.sid,

  s.serial# serial_num,

  s.username user_name,

  a.type object_type,

  s.osuser os_user_name,

  a.owner,

  a.object object_name,

  decode(sign(48 - command),

  1,

  to_char(command), 'Action Code #'    to_char(command) ) action,

  p.program oracle_process,

  s.terminal terminal,

  s.program program,

  s.status session_status

  from v$session s, v$access a, v$process p

  where s.paddr = p.addr and

  s.type = 'USER' and

  a.sid = s.sid and

  a.object='SUBSCRIBER_ATTR'

  order by s.username, s.osuser
  

  14、回滚段查看

  select rownum, sys.dba_rollback_segs.segment_name Name, v$rollstat.extents

  Extents, v$rollstat.rssize Size_in_Bytes, v$rollstat.xacts XActs,

  v$rollstat.gets Gets, v$rollstat.waits Waits, v$rollstat.writes Writes,

  sys.dba_rollback_segs.status status from v$rollstat, sys.dba_rollback_segs,

  v$rollname where v$rollname.name(+) = sys.dba_rollback_segs.segment_name and

  v$rollstat.usn (+) = v$rollname.usn order by rownum

  

  15、耗资源的进程(top session)

  select s.schemaname schema_name, decode(sign(48 - command), 1,

  to_char(command), 'Action Code #'    to_char(command) ) action, status

  session_status, s.osuser os_user_name, s.sid, p.spid , s.serial# serial_num,

  nvl(s.username, '[Oracle process]') user_name, s.terminal terminal,

  s.program program, st.value criteria_value from v$sesstat st, v$session s , v$processp

  where st.sid = s.sid and st.statistic# = to_number('38') and ('ALL' = 'ALL'

  or s.status = 'ALL') and p.addr = s.paddr order by st.value desc, p.spid asc, s.username asc, s.osuser asc

 

select se.username,se.machine,sq.cpu_time,sq.sql_text from
v$process p,v$session se,v$sqlarea sq
where p.addr=se.paddr and se.sql_hashvalue=sq.hash_value and p.addr='&pid';

 

SELECT p.pid,p.spid,s.sid,p.username,s.TYPE,
         s.SERIAL#,s.SCHEMANAME,s.OSUSER,s.MACHINE,
         s.PROCESS,p.PROGRAM,s.MODULE,s.STATUS,
         s.terminal,logon_time
FROM v$process p, v$session s
WHERE p.addr=s.paddr
and spid =
'1712258'; 

 

下面的句子列出cpu_time占用top 10sql

select cpu_time,sql_text
from (select sql_text,cpu_time,
 rank() over (order by cpu_time desc) exec_rank
 from v$sql
      )
where exec_rank <=10;

 

执行次数最多的top 10

select sql_text,executions
from (select sql_text,executions,
   rank() over
    (order by executions desc) exec_rank
   from v$sql)
where exec_rank <=10;

 

分享到:
评论

相关推荐

    Oracle维护常用SQL语句.pdf

    oracle维护常用SQL语句.pdf

    Oracle维护常用SQL语句汇总

    ### Oracle维护常用SQL语句汇总 #### 一、概述 在Oracle数据库的日常维护工作中,熟练掌握常用的SQL语句对于提高工作效率、确保数据库稳定运行至关重要。本文将对一系列Oracle维护中常用的SQL语句进行总结,并详细...

    Oracle维护常用SQL语句.doc

    在日常维护工作中,了解和掌握一些常用的SQL语句对于数据库管理员来说至关重要。以下是一些关键的Oracle维护SQL语句及其解释: 1. **查看表空间名称及大小**: ```sql select t.tablespace_name, round(sum(bytes...

    Oracle维护常用SQL语句(查询系统表和视图)

    根据给定的信息,我们可以整理出一系列关于Oracle数据库维护中常用的SQL查询语句,这些语句主要涉及查询系统表和视图来获取有关数据库的各种信息。接下来将详细解释这些查询语句的意义及其应用场景。 ### 1. 查询表...

    ORACLE常用维护sql语句

    ORACLE常用维护sql语句ORACLE常用维护sql语句ORACLE常用维护sql语句ORACLE常用维护sql语句

    Oracle常用的SQL语句维护

    本文将深入探讨Oracle中常用的SQL语句以及与数据库表空间相关的维护知识。 首先,让我们了解如何在Oracle中创建表空间。表空间是Oracle数据库中存储数据的逻辑单位,它由一个或多个数据文件组成。创建表空间的SQL...

    ORACLE常用SQL语句大全.pdf

    Oracle 常用 SQL 语句大全 本文档总结了 Oracle 中常用的 SQL 语句,包括数据库的创建、删除、备份、表的创建、删除、修改、索引的创建、视图的创建等基本操作,以及一些高级查询运算符的使用。 一、数据库操作 ...

    oracle常用SQL语句(汇总版).docx

    Oracle 常用 SQL 语句汇总 Oracle 是一个功能强大且复杂的关系数据库管理系统,它提供了多种 SQL 语句来管理和操作数据库。在本文中,我们将详细介绍 Oracle 中常用的 SQL 语句,包括数据控制语句(DML)、数据定义...

    Oracle Sql语句转换成Mysql Sql语句

    本项目提供了一个Java源码工具,能够帮助用户便捷地将Oracle SQL语句转换为MySQL SQL语句。 Oracle SQL与MySQL SQL的主要差异在于以下几个方面: 1. **数据类型**:Oracle支持的数据类型如NUMBER、LONG、RAW等在...

    oracle监听执行sql语句

    在Oracle数据库管理与维护过程中,有时候我们需要了解应用程序正在执行哪些SQL语句,这不仅有助于性能优化,还可以帮助我们诊断潜在的问题。通过监听执行SQL语句的方式,我们可以获取到当前会话正在执行的具体SQL...

    Oracle数据库维护常用SQL语句集合

    进行Oracle数据库维护,掌握一些常用的SQL语句是必不可少的。本文将深入探讨Oracle数据库维护中的核心SQL语句,帮助你更好地管理和优化数据库。 1. **数据查询(SELECT语句)**:SELECT语句是SQL中最基本的语句,...

    Oracle数据库维护常用SQL语句集合(3)-PLSQL

    Oracle数据库维护常用SQL语句集合(3)-PLSQL

Global site tag (gtag.js) - Google Analytics