Oracle Database Experts

By Charles Kim

July 26th, 2009

Generating SQL for database objects

You can use SQL to generate SQL for DDL for database objects. Here’s another alternative using the DBMS_METADATA package:


----------------------------------------------------------------------------------------------
--
SELECT dbms_metadata.get_dependent_ddl('OBJECT_GRANT', 'ORDERS', 'OE') from dual;
SELECT  DBMS_METADATA.GET_DEPENDENT_DDL('TRIGGER','<table_name>','<schema>') from dual;

----------------------------------------------------------------------------------------------
--
set pagesize 0
set long 90000
set feedback off
set echo off 

spool schema.sql 

SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name)
     FROM USER_TABLES u;

SELECT DBMS_METADATA.GET_DDL('INDEX',u.index_name)
     FROM USER_INDEXES u;

spool off;

----------------------------------------------------------------------------------------------
--
SELECT dbms_metadata.get_dependent_xml('OBJECT_GRANT', 'ORDERS', 'OE') from dual;

----------------------------------------------------------------------------------------------
--  View system grants for a user
SELECT dbms_metadata.get_granted_ddl('SYSTEM_GRANT', 'OE_USER') FROM dual

----------------------------------------------------------------------------------------------
--
SELECT dbms_metadata.get_granted_ddl('ROLE_GRANT', 'OE') from dual;

SELECT dbms_metadata.get_ddl('VIEW', 'MY_TABLES')
FROM dual;

Function
CREATE OR REPLACE FUNCTION whoami RETURN VARCHAR2 IS

BEGIN
   RETURN user;
END whoami;
/

SELECT dbms_metadata.get_ddl('FUNCTION', 'WHOAMI')
FROM dual; 

# --------------------------------------------------------------------------------------------
# For maximum flexibility, extract object definitions for tables, indexes, and constraints
exec dbms_metadata.set_transform_param( dbms_metadata.session_transform, 'PRETTY', true);

exec dbms_metadata.set_transform_param( dbms_metadata.session_transform, 'SEGMENT_ATTRIBUTES', true);
exec dbms_metadata.set_transform_param( dbms_metadata.session_transform, 'STORAGE', true);
exec dbms_metadata.set_transform_param( dbms_metadata.session_transform, 'TABLESPACE', true);

exec dbms_metadata.set_transform_param( dbms_metadata.session_transform, 'CONSTRAINTS', true);
exec dbms_metadata.set_transform_param( dbms_metadata.session_transform, 'REF_CONSTRAINTS', true);
exec dbms_metadata.set_transform_param( dbms_metadata.session_transform, 'CONSTRAINTS_AS_ALTER', true);

Posted by Charles Kim, Oracle ACE

July 22nd, 2009

Oracle Security – For auditing, move the aud$ to another tablespace

If you are performing any kind of auditing on the database, we recommend that you move the aud$ tablespace from the system tablespace to another tablespace. In this example, we are moving the aud$ tablespace to a tablespace called SYSAUDIT_D.

If you are on ASM, you would change the file location to an ASM diskgroup such as ‘+data’.

def DB='&1'
def FS='&2'

CREATE TABLESPACE SYSAUDIT_D DATAFILE
  '/data/oracle/&DB/&FS/sysaudit_d_01.dbf' SIZE 200m AUTOEXTEND OFF
LOGGING ONLINE PERMANENT
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;

create table audx tablespace SYSAUDIT_D as select * from aud$ where 1 = 2;
rename AUD$ to AUD$$;
rename audx to aud$;
drop index i_aud1;

create index i_aud1 on aud$(sessionid, ses$tid) tablespace SYSAUDIT_D;

set echo off

Posted by Charles Kim, Oracle ACE

July 9th, 2009

Cloning an Oracle Home after you tar the binaries from one server to another

First, let’s look at the inventory XML file on the target server before the clone:

db – oracle: vi inventory.xml

 

<?xml version="1.0" standalone="yes" ?>

<!– Copyright (c) 2005 Oracle Corporation. All rights Reserved –>

<!– Do not modify the contents of this file by hand. –>

<INVENTORY>

<VERSION_INFO>

<SAVED_WITH>10.2.0.3.0</SAVED_WITH>

<MINIMUM_VER>2.1.0.6.0</MINIMUM_VER>

</VERSION_INFO>

<HOME_LIST>

<HOME NAME="db10g" LOC="/apps/oracle/product/db10g" TYPE="O" IDX="1"/>

<HOME NAME="oms10g" LOC="/apps/oracle/product/oms10g" TYPE="O" IDX="2"/>

<HOME NAME="agent10g" LOC="/apps/oracle/product/agent10g" TYPE="O" IDX="3"/>

</HOME_LIST>

</INVENTORY>

 

Copy the binaries from one server to the other

$- oracle: tar cvf – db |ssh rac1.dbaexpert.com "cd /apps/oracle/product/10.2.0; tar xvf -"

Execute the runInstaller in silent mode

cd $ORACLE_HOME/oui/bin

db – oracle: ./runInstaller -silent -clone ORACLE_HOME="/apps/oracle/product/10.2.0/db" ORACLE_HOME_NAME="OraDb10g_home1"

Starting Oracle Universal Installer…

 

No pre-requisite checks found in oraparam.ini, no system pre-requisite checks will be executed.

Preparing to launch Oracle Universal Installer from /tmp/OraInstall2009-07-09_09-08-33AM. Please wait …rac1.dbaexpert.com:/apps/oracle/product/10.2.0/db/oui/bin

db – oracle: Oracle Universal Installer, Version 10.2.0.4.0 Production

Copyright (C) 1999, 2008, Oracle. All rights reserved.

 

You can find a log of this install session at:

/apps/oracle/oraInventory/logs/cloneActions2009-07-09_09-08-33AM.log

………………………………………………………………………………………. 100% Done.

 

 

 

Installation in progress (Thursday, July 9, 2009 9:08:45 AM EDT)

……………………………………………………………….. 74% Done.

Install successful

 

Linking in progress (Thursday, July 9, 2009 9:08:52 AM EDT)

Link successful

 

Setup in progress (Thursday, July 9, 2009 9:11:21 AM EDT)

Setup successful

 

End of install phases.(Thursday, July 9, 2009 9:11:25 AM EDT)

WARNING:

The following configuration scripts need to be executed as the "root" user.

#!/bin/sh

#Root script to run

/apps/oracle/product/10.2.0/db/root.sh

To execute the configuration scripts:

1. Open a terminal window

2. Log in as "root"

3. Run the scripts

 

The cloning of OraDb10g_home1 was successful.

Please check ‘/apps/oracle/oraInventory/logs/cloneActions2009-07-09_09-08-33AM.log’ for more details.

 

Inventory XML file AFTER the clone:

db – oracle: cat inventory.xml

<?xml version="1.0" standalone="yes" ?>

<!– Copyright (c) 2008 Oracle Corporation. All rights Reserved –>

<!– Do not modify the contents of this file by hand. –>

<INVENTORY>

<VERSION_INFO>

<SAVED_WITH>10.2.0.4.0</SAVED_WITH>

<MINIMUM_VER>2.1.0.6.0</MINIMUM_VER>

</VERSION_INFO>

<HOME_LIST>

<HOME NAME="db10g" LOC="/apps/oracle/product/db10g" TYPE="O" IDX="1"/>

<HOME NAME="oms10g" LOC="/apps/oracle/product/oms10g" TYPE="O" IDX="2"/>

<HOME NAME="agent10g" LOC="/apps/oracle/product/agent10g" TYPE="O" IDX="3"/>

<HOME NAME="OraDb10g_home1" LOC="/apps/oracle/product/10.2.0/db" TYPE="O" IDX="4"/>

</HOME_LIST>

</INVENTORY>

July 2nd, 2009

sqlplus lightweight session when all fails

What do you do when you can’t login via sqlplus / as sysdba?  You try to connect and it just hangs. You can login with the -prelim option. It is a lightweight session and No SQL is allowed in the connected session.

$ sqlplus -prelim / as sysdba

SQL*Plus: Release 10.2.0.4.0 - Production on Thu Jul 2 03:32:26 2009

Copyright (c) 1982, 2007, Oracle.&nbsp; All Rights Reserved.
SQL> shutdown abort;

ORACLE instance shut down.
SQL> exit
Disconnected from ORACLE
rac01.dbaexpert.com:/apps/oracle
DBATOOLS - oracle: sqlp

SQL*Plus: Release 10.2.0.4.0 - Production on Thu Jul 2 03:33:08 2009<

Copyright (c) 1982, 2007, Oracle.&nbsp; All Rights Reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area&nbsp; 419430400 bytes
Fixed Size       2084456 bytes
Variable Size    155189656 bytes
Database Buffers 255852544 bytes
Redo Buffers     6303744 bytes
Database mounted.
Database opened.

Posted by Charles Kim, Oracle ACE

|