您的位置:首页 > 数据库 > Oracle

[Oracle] DBA 常用脚本一

2009-09-22 20:51 477 查看
1: 查看回滚段中活动的事务 Active Transactions in Rollback Segments

column rr heading 'RB Segment' format a18
column us heading 'Username' format a15
column os heading 'OS User' format a10
column te heading 'Terminal' format a10

SELECT r.name rr,
nvl(s.username,'no transaction') us,
s.osuser os,
s.terminal te
FROM
v$lock l,
v$session s,
v$rollname r
WHERE
l.sid = s.sid(+) AND
trunc(l.id1/65536) = r.usn AND
l.type = 'TX' AND
l.lmode = 6
ORDER BY r.name
/

2: 查看包体源码 Show Package Extract package and package body source from the database

COL SORT1 NOPRINT
COL SORT2 NOPRINT
COL SORT3 NOPRINT
COL SORT4 NOPRINT
BREAK ON SORT1 SKIP 1
set linesize 120
SET HEADING OFF
SET ECHO OFF
SET FEEDBACK OFF
SET PAGESIZE 0
SPOOL bldpack.sql

select 'set echo on ' from dual;
select 'spool bldpack.lst' from dual;
select 'Remark Build package definitions' from dual;

SELECT NAME SORT1, LINE SORT2,
'Create or Replace ' || TEXT
FROM USER_SOURCE
WHERE TYPE = 'PACKAGE' AND LINE = 1
UNION
SELECT NAME SORT1, LINE SORT2,TEXT
FROM USER_SOURCE
WHERE TYPE = 'PACKAGE' AND LINE > 1
UNION
SELECT NAME SORT1, 999999 SORT2, '/'
FROM USER_SOURCE
WHERE TYPE = 'PACKAGE' AND LINE =1
ORDER BY 1,2;

spool off
SPOOL bldpbdy.sql

select 'set echo on ' from dual;
select 'spool bldpbdy.lst' from dual;
select 'Remark Build package body definitions' from dual;

SELECT NAME SORT1, LINE SORT2,'Createor
Replace '||TEXT
FROM USER_SOURCE
WHERE TYPE = 'PACKAGE BODY' AND LINE = 1
UNION
SELECT NAME SORT1, LINE SORT2,TEXT
FROM USER_SOURCE
WHERE TYPE = 'PACKAGE BODY' AND LINE > 1
UNION
SELECT NAME SORT1, 999999 SORT2,'/'
FROM USER_SOURCE
WHERE TYPE = 'PACKAGE BODY' AND LINE = 1
ORDER BY 1,2;
spool off
SPOOL bldproc.sql

select 'set echo on ' from dual;
select 'spool bldproc.lst' from dual;
select 'Remark Build procedure definitions' from
dual;

SELECT NAME SORT1, LINE SORT2,
'Create or Replace ' || TEXT
FROM USER_SOURCE
WHERE TYPE = 'PROCEDURE' AND LINE = 1
UNION
SELECT NAME SORT1, LINE SORT2, TEXT
FROM USER_SOURCE
WHERE TYPE = 'PROCEDURE' AND LINE>1
UNION
SELECT NAME SORT1, 999999 SORT2,'/'
FROM USER_SOURCE
WHERE TYPE = 'PROCEDURE' AND LINE = 1
ORDER BY 1,2;

select 'Remark Build function definitions' from
dual;

SELECT NAME SORT1, LINE SORT2,
'Create or Replace ' || TEXT
FROM USER_SOURCE
WHERE TYPE = 'FUNCTION' AND LINE = 1
UNION
SELECT NAME SORT1, LINE SORT2, TEXT
FROM USER_SOURCE
WHERE TYPE = 'FUNCTION' AND LINE > 1
UNION
SELECT NAME SORT1, 999999 SORT2,'/'
FROM USER_SOURCE
WHERE TYPE = 'FUNCTION' AND LINE = 1
ORDER BY 1,2;
spool off

3: 查看视图源码 Show views, Extract the code for views from the database

SET HEADING OFF
SET ECHO OFF
SET FEEDBACK OFF
SET PAGESIZE 0
set numwidth 10

SELECT uv.view_name SORT1, 0 SORT2, 0 SORT3,
0 SORT4,
'create or replace view '||uv.view_name ||' ('
from dba_views uv
WHERE uv.owner = upper('&2')
and uv.view_name = upper('&1')
union all
SELECT utc.view_name SORT1, utc.column_id
SORT2, 0 SORT3, 0 SORT4,utc.column_name
from dba_tab_columns
WHERE utc.owner = upper('&2')
and utc.table_name = upper('&1')
and utc.column_id = 1
union all
SELECT utc.view_name SORT1, utc.column_id
SORT2, 0 SORT3, 0 SORT4,
' , '||utc.column_name
from dba_tab_columns
WHERE utc.owner = upper('&2')
and utc.table_name = upper('&1')
and utc.column_id <> 1
SELECT uv.view_name SORT1,999 SORT2,0
SORT3,0 SORT4,' )'
from dba_views uv
WHERE uv.owner = upper('&2')
and uv.view_name = upper('&1')
ORDER BY 1, 2, 3, 4;
SELECT uv.text
from dba_views uv
WHERE uv.owner = upper('&2')
and uv.view_name = upper('&1');
SELECT uv.view_name SORT1,999 SORT2,0
SORT3,0 SORT4,' ;'
from dba_views uv
WHERE uv.owner = upper('&2')
and uv.view_name = upper('&1');

4: 监控文件IO性能 Monitor File I/O

This script takes a snapshot of v$filestats at the current time and saves it. It then waits 10 seconds and takes another snapshot and reports on the delta.

Code:

col name for a50
set linesize 132
set pages 666
-- drop temporary table
drop table jh$filestats;
create table jh$filestats as
select file#, PHYBLKRD, PHYBLKWRT
from v$filestat;
prompt Waiting......
exec dbms_lock.sleep(10);

prompt NOTE: Only the top 10 files...
select * from (
select df.name, fs.phyblkrd - t.phyblkrd "Reads",fs.PHYBLKWRT - t.PHYBLKWRT "Writes",(fs.PHYBLKRD+fs.PHYBLKWRT) - (t.PHYBLKRD+t.PHYBLKWRT) "Total IO"
from v$filestat fs, v$datafile df, jh$filestats t
where df.file# = fs.file# and t.file# = fs.file#
and (fs.PHYBLKRD+fs.PHYBLKWRT) - (t.PHYBLKRD+t.PHYBLKWRT) > 0
order by "Total IO" desc )
where rownum <= 10
/

5: 获取表中2个最大值 Finds the two highest salaries from table

(1). Finds the two highest salaries from table "emp".

Code:
select a.empno,a.sal
from emp a
where 2>
(
select count(*)
from emp
where sal>a.sal
);

(2). Finds the two lowest salaries from table "emp".

Code:
select a.empno,a.sal
from emp a
where 2>
(
select count(*)
from emp
where sal<a.sal);

6: 给定父表查询出它的子表 Finds Child Tables for a given parent table

set echo off
set verify off
accept xTable prompt 'Enter Table Name: '
TTITLE LEFT 'Child Tables for the table: '&xTABLE
break on TABLE_NAME
SELECT B.TABLE_NAME, C.COLUMN_NAME,
C.POSITION
FROM USER_CONSTRAINTS A,
USER_CONSTRAINTS B,
USER_CONS_COLUMNS C
WHERE
A.CONSTRAINT_NAME=
B.R_CONSTRAINT_NAME
AND
A.TABLE_NAME = C.TABLE_NAME
AND
A.CONSTRAINT_NAME = C.CONSTRAINT_NAME
AND
A.TABLE_NAME = UPPER('&xTable')
ORDER BY B.TABLE_NAME, C.POSITION;

TTITLE LEFT 'Parent tables for the table: '&xTable

SELECT A.TABLE_NAME, C.COLUMN_NAME,
C.POSITION
FROM USER_CONSTRAINTS A,
USER_CONSTRAINTS B,
USER_CONS_COLUMNS C
WHERE
A.CONSTRAINT_NAME=B.R_CONSTRAINT_NAME
AND
B.TABLE_NAME = C.TABLE_NAME
AND
B.CONSTRAINT_NAME = C.CONSTRAINT_NAME
AND
B.TABLE_NAME = UPPER('&xTable')
ORDER BY A.TABLE_NAME, C.POSITION;

7: 查看约束的相关信息 Returns information about the constraint

Script prompts for a constraint name then it returns information about the constraint.

Code:
-- displays constraint info
-- created by Noah Monsey
-- 02/02/2000
set lines 120
set serveroutput on size 100000
set scan on
clear buffer
set verify off
set feedback off
declare

l_constraint_name varchar2(30);
l_constraint_type varchar2(1);
l_table_name varchar2(30);
l_search_condition long;
l_related_constraint varchar2(30);
l_related_table varchar2(30);
l_rcolumn varchar2(30);
l_column varchar2(30);
l_query varchar2(400);
l_column_count integer;

cursor lc_constraint (p_constraint_name varchar2) is
select *
from dba_constraints
where constraint_name = p_constraint_name;

cursor lc_related_constraint(p_r_constraint_name varchar2)
is
select table_name from dba_constraints
where constraint_name = p_r_constraint_name;

cursor lc_columns (p_constraint_name varchar2) is
select column_name from dba_cons_columns
where constraint_name = p_constraint_name;
begin
dbms_output.enable(1000000);
for rec_constraint in
lc_constraint(upper('&constraint_name'))
loop
l_constraint_type := null;
l_table_name := null;
l_related_constraint := null;
l_search_condition := null;
l_constraint_type :=
rec_constraint.constraint_type;
l_table_name :=
rtrim(rec_constraint.table_name);
l_related_constraint :=
rec_constraint.r_constraint_name;
l_search_condition :=
rec_constraint.search_condition;
l_constraint_name :=
rec_constraint.constraint_name;
l_column_count := 0;
dbms_output.put_line(
);
if l_constraint_type = 'P' then
dbms_output.put_line(l_constraint_name || ' is a primary key constraint on table ' || l_table_name);
open lc_columns(l_constraint_name);
fetch lc_columns into l_column;
dbms_output.put_line('Column ' ||
l_column);
close lc_columns;
end if;
if l_constraint_type = 'U' then
dbms_output.put_line(l_constraint_name || ' is a unique constraint on table ' || l_table_name);
for rec_columns in
lc_columns(l_constraint_name)
loop
dbms_output.put_line('Column ' ||
rec_columns.column_name);
end loop;
end if;
if l_constraint_type = 'C' then
dbms_output.put_line(l_constraint_na
me || ' is a check constraint ' ||
rtrim(l_table_name) || ' where '||
l_search_condition);
end if;
if l_constraint_type = 'R' then
open
lc_related_constraint(l_related_constr
aint);
fetch lc_related_constraint into
l_related_table;
dbms_output.put_line('Table
'||l_table_name ||' referential
constraint '|| l_related_constraint ||'
failed because ');
l_query := ' select ' ;
l_column_count := 0;
open lc_columns(l_constraint_name);
fetch lc_columns into l_column;
l_query := l_query || ' ' || rtrim(l_column);
if l_column_count > 1 then
l_query := l_query || ',';
end if;
l_column_count := l_column_count +1;
dbms_output.put_line('Column'||l_column);
close lc_columns;
l_query := l_query || 'from'|| l_table_name || ' minus select ' ;
l_column_count := 0;
dbms_output.put_line('Table'|| l_related_table||' is missing related values ');
open
lc_columns(l_related_constraint);
fetch lc_columns into l_rcolumn;
l_query := l_query || ' ' ||
rtrim(l_rcolumn);
if l_column_count > 1 then
l_query := l_query || ',';
end if;
l_column_count := l_column_count +1;
dbms_output.put_line('Column'||l_rcolumn);
close lc_columns;
l_query := l_query || ' from ' || rtrim(l_related_table) ||';';
dbms_output.put_line(l_query);
close lc_related_constraint;
end if;
if l_constraint_type not in ('C','P','U','R') then
dbms_output.put_line(l_table_name||'
has a unknown constraint type');
end if;
end loop;
end;
/
set feedback on

8: 找出表中重复的记录 Find out the duplicate records in a Table

Code:
SELECT primary_key FROM table_name
MINUS
(
SELECT DISTINCT primary_key
FROM table_name
)

You can also use * instead of the primary_key.
18. Description: This Script will help you to find out which Database you are presently
Connected to? If You are working on multiple Databases on different Servers using SQL*Net or Net8.
Code:

select distinct A.name DATABASE,
B.machine SERVER
from V$database A, V$session B
where b.username is null
/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: