Paper 78
Paper 78
Techniques for
DBAs and
Developers
Ari Kaplan
www.arikaplan.com
Independent Consultant
Paper 78
EOUG Madrid 2000
Introduction
✸ Most books and documentation are set up
like reference materials
✸ Training courses focus on the fundamental
aspects of products in a limited time
✸ Hands-on experience is one of the best ways
to learn tricks to maximize your use of
Oracle
The Topics
✔ “Gotchas” of Dropping and Recreating Tables
alter PROCEDURE PROD.CM1279 compile;
alter FUNCTION PROD.DELETE_TRADE_DATE
compile;
alter FUNCTION PROD.POP_TEMP_MATCH compile;
alter VIEW TEST_ACCT.POST215_V compile;
alter FUNCTION TEST_ACCT.PROCESS215 compile;
alter FUNCTION TEST_ACCT.PROCESS216 compile;
alter PACKAGE TEST_ACCT.TEST_UTILITY_P
compile;
See Hidden INIT.ORA
Parameters
Regular INIT.ORA: V$PARAMETER
Name Null? Type
------------------------------------------------- ------------------------ ----------------------------------------
NUM NUMBER
NAME
VARCHAR2(64)
TYPE NUMBER
VALUE VARCHAR2(512)
ISDEFAULT VARCHAR2(9)
ISSES_MODIFIABLE VARCHAR2(5)
ISSYS_MODIFIABLE VARCHAR2(9)
ISMODIFIED VARCHAR2(10)
ISADJUSTED VARCHAR2(5)
See Hidden INIT.ORA Parameters
✸ To view the “regular” init.ora parameters, issue:
SQL> SELECT NAME, VALUE, ISDEFAULT
■ FROM V$PARAMETER
■ ORDER BY NAME;
$ORACLE_HOME/sqlplus/admin/glogin.sql
✼ Local Login Script:
$ORACLE_PATH/login.sql
set heading off
select 'Logged in as '
|| username from
user_users;
set heading on
Changing the SQL Prompt
In glogin.sql or login.sql:
set heading off
set prompt off
spool make_prompt.sql
SELECT ‘set sqlprompt ‘‘‘|| d.name ||‘@‘
||
substr(s.machine,1,decode
(instr(s.machine,‘.‘), 0,
length(s.machine),
instr(s.machine,‘.‘) 1)) ||
‘SQL> ‘‘‘
FROM V$SESSION s, V$DATABASE D
The prompt will look
WHERE s.SID=1; like:
spool off
@make_prompt.sql PHIS@survlpd-
set heading on SQL>
Resize the Redo Logs
SQL> SELECT * FROM V$LOG;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS CHANGE#
FIRST_TIM
1 1 422 52,428,800
1 NO INACTIVE 680764231 05APR00
2 1 423 262,144,000
1 NO INACTIVE 680764271 05APR00
3 1 424 262,144,000
1 NO CURRENT 680764463 05APR00
SQL> SELECT * FROM V$LOGFILE;
GROUP# STATUS MEMBER
/emc08/ORACLE/DHIS/redoQHIS01a.log 1
Resize the Redo Logs
1) Drop the redo log (or group of redo logs):
SQL> ALTER DATABASE DROP LOGFILE GROUP 1;
(If the active log group is 1, you will get the following error:)
ORA-01623: log 1 is current log for thread 1 – cannot drop
(If this occurs, switch the logfile group: “ALTER SYSTEM SWITCH
LOGFILE;” and reissue.)
Database altered.
2) Now there are two log groups remaining (#2, 3). Next, we will
physically remove the file:
SQL> !rm /emc08/ORACLE/DHIS/redoQHIS01a.log
3) Add the redo log back to group 1, with a 16M size:
SQL> ALTER DATABASE ADD LOGFILE GROUP 1
'/emc08/ORACLE/DHIS/redoQHIS01a.log' SIZE 16M
Database altered.
4) Switch the logfile until it points to the recently changed group (in
this example group 1).
SQL> ALTER SYSTEM SWITCH LOGFILE;
System altered.
The new redo log (from group 1) is now 16M in size.
Repeat #1-4 above until all three redo logs have been resized.
Using DECODE for Multiple Values in One
Decode Example:
Clause
SQL> SELECT DECODE(SEX, ’M’, ’Male’, ’F’, ’Female’,
’Unknown’)
2 FROM TABLE_NAME;
Table Example:
SQL> SELECT * FROM ACCOUNT_PHONES_T;
PHONE If the length of
5551212
5553333
phone > 7
SQL> SELECT PHONE, SUBSTR(PHONE,1,3)||
2 DECODE(SIGN(7LENGTH(PHONE)),1,
3 SUBSTR(PHONE,4,99), ''||SUBSTR(PHONE,4,99))
4 “PHONE_FORMATTED”
If the length of
5 FROM ACCOUNT_PHONES_T; phone not > 7
PHONE PHONE_FORMATTED
5551212 5551212
5553333 5553333
Using DECODE for < or >
Table Example: Comparisons
SQL> SELECT * FROM SALARIES;
HUSBAND_SALARY WIFE_SALARY
45000 60000
90000 39700
SQL Using DECODE and LEAST:
SQL> SELECT husband_salary, wife_salary,
DECODE (LEAST (husband_salary,wife_salary),
husband_salary,'The Husband has the
lower salary',
'The Wife has the lower salary')
"WHO_IS_LOWER"
FROM SALARIES
WHERE husband_salary <> wife_salary;
HUSBAND_SALARY WIFE_SALARY WHO_IS_LOWER
45000 60000 The
Husband has the lower salary
Using DECODE for < or >
Table Example, withComparisons
<, >, and = values:
SQL> SELECT * FROM SALARIES;
HUSBAND_SALARY WIFE_SALARY
45000 60000
90000 39700
55000 55000
SQL Using DECODE and LEAST:
SQL> SELECT husband_salary, wife_salary,
decode(husband_salary wife_salary, 0, 'The
salaries are the same',
decode(least(husband_salary,wife_salary),
husband_salary,'The Husband has the lower
salary',
‘The Wife has the lower salary'))
"WHO_IS_LOWER”
FROM SALARIES;
HUSBAND_SALARY WIFE_SALARY WHO_IS_LOWER
Identify and Delete Duplicate Records
Table Example, with duplicate values:
SQL> SELECT * FROM EMP;
EMP_ID OFFICE_ID EMPNAME
305 12 ELLISON, GEORGE
305 12 MERCURIO, JASON
128 17 SIMPSON, LINDA
305 22 JACKSON, DREW
To see duplicates, use the following SQL:
SQL> SELECT COUNT(*), EMP_ID, OFFICE_ID
FROM EMP
GROUP BY EMP_ID, OFFICE_ID
HAVING COUNT(*) > 1;
The result will be:
COUNT(*) EMP_ID OFFICE_ID
2 305 12
Identify and Delete Duplicate Records
Table Example, with duplicate values:
SQL> SELECT * FROM EMP;
EMP_ID OFFICE_ID EMPNAME
305 12 ELLISON, GEORGE
305 12 MERCURIO, JASON
128 17 SIMPSON, LINDA
305 22 JACKSON, DREW
To delete ALL duplicates (not leaving any of the set):
SQL> DELETE FROM EMP A WHERE
(EMP_ID, OFFICE_ID, 2) IN
(SELECT EMP_ID, OFFICE_ID,
decode(count(*),1,1,2)
FROM EMP B
WHERE A.EMP_ID=B.EMP_ID AND
A.OFFICE_ID = B.OFFICE_ID
Identify and Delete Duplicate Records
Table Example, with duplicate values:
SQL> SELECT * FROM EMP;
EMP_ID OFFICE_ID EMPNAME
305 12 ELLISON, GEORGE
305 12 MERCURIO, JASON
128 17 SIMPSON, LINDA
305 22 JACKSON, DREW
To delete all of the duplicates (leaving just one of the set):
SQL> DELETE FROM EMP A
WHERE ROWID > (
SELECT min(rowid) FROM EMP B
WHERE A.EMP_ID = B.EMP_ID AND
A.OFFICE_ID = B.OFFICE_ID);
OS Commands and Utilities
✷ oerr
> oerr ora 3200
03200, 00000, "the segment type specification is invalid"
// *Cause: segment type is not TABLE, INDEX, or CLUSTER
// *Action: use a correct segment type
comp.databases.oracle.server
comp.databases.oracle.tools
comp.databases.oracle.misc
There are over 370 tips and answers to questions that have been posed to
me over the years. This paper will be downloadable from the web page as
well.