Mandalika's scratchpad [ Work blog @Oracle | Stock Market Notes | My Music Compositions ]

Old Posts: 09.04  10.04  11.04  12.04  01.05  02.05  03.05  04.05  05.05  06.05  07.05  08.05  09.05  10.05  11.05  12.05  01.06  02.06  03.06  04.06  05.06  06.06  07.06  08.06  09.06  10.06  11.06  12.06  01.07  02.07  03.07  04.07  05.07  06.07  08.07  09.07  10.07  11.07  12.07  01.08  02.08  03.08  04.08  05.08  06.08  07.08  08.08  09.08  10.08  11.08  12.08  01.09  02.09  03.09  04.09  05.09  06.09  07.09  08.09  09.09  10.09  11.09  12.09  01.10  02.10  03.10  04.10  05.10  06.10  07.10  08.10  09.10  10.10  11.10  12.10  01.11  02.11  03.11  04.11  05.11  07.11  08.11  09.11  10.11  11.11  12.11  01.12  02.12  03.12  04.12  05.12  06.12  07.12  08.12  09.12  10.12  11.12  12.12  01.13  02.13  03.13  04.13  05.13  06.13  07.13  08.13  09.13  10.13  11.13  12.13  01.14  02.14  03.14  04.14  05.14  06.14 


Sunday, May 27, 2007
 
Oracle HOW-TO: Get the Table/View Definition, Indexed Columns

Q#1: Given a view name, how do we get the definition of the view? i.e., how to get the corresponding SQL statement stored in the database in the system tablespace?

A: Query the TEXT column of table DBA_VIEWS.

Syntax:
SQL> set long 10000

SQL> select TEXT
2 FROM DBA_VIEWS
3 where OWNER = '<owner_name>'
4 and VIEW_NAME = '<view_name>';

Here is an example:
% sqlplus fs890/fs890@fs890

SQL> create table PERSON (
2 SSN VARCHAR2(12),
3 FIRST_NAME VARCHAR2(25),
4 LAST_NAME VARCHAR2(25),
5 STREET VARCHAR2(40),
6 CITY VARCHAR2(30),
7 STATE VARCHAR2(30),
8 ZIP VARCHAR2(15),
9 COUNTRY VARCHAR2(35));

Table created.

SQL> create view PERSON_VW as
2 select SSN, FIRST_NAME, LAST_NAME from PERSON;

View created.

SQL> set long 1000
SQL> select TEXT
2 from DBA_VIEWS
3 where OWNER = 'FS890'
4 and VIEW_NAME = 'PERSON_VW';

TEXT
--------------------------------------------------------------------------------
SELECT SSN, FIRST_NAME, LAST_NAME FROM PERSON

Q#2: How to get the current {session} user and current schema name?

A: Run the following query:

select sys_context('USERENV', 'SESSION_USER') SESSION_USER, sys_context('USERENV', 'CURRENT_SCHEMA') CURRENT_SCHEMA from dual;

Alternatively run select USER from DUAL; to find the current {session} user name.

sys_context() function returns the value of parameter associated with the context namespace. USERENV is an Oracle provided namespace that describes the current session. Check the table Predefined Parameters of Namespace USERENV for the list of parameters and the expected return values.

eg.,
SQL> column SESSION_USER format A15
SQL> column CURRENT_SEHEMA format A15

SQL> select sys_context('USERENV', 'SESSION_USER') SESSION_USER,
2 sys_context('USERENV', 'CURRENT_SCHEMA') CURRENT_SCHEMA from dual;

SESSION_USER CURRENT_SCHEMA
--------------- ---------------
FS890 FS890

SQL> column USER format A6

SQL> select USER from DUAL;

USER
------
FS890

Q#3: How to extract the table definition (DDL statements) from an Oracle database without having to go through a stack of dictionary views?

A: By calling the GET_DDL() function of metadata package DBMS_METADATA.

Syntax:
select DBMS_METADATA.GET_DDL('TABLE','<table_name>') from DUAL;

eg.,
SQL> set long 1000
SQL> set pagesize 0

SQL> select DBMS_METADATA.GET_DDL('TABLE','PERSON') from DUAL;

CREATE TABLE "FS890"."PERSON"
( "SSN" VARCHAR2(12),
"FIRST_NAME" VARCHAR2(25),
"LAST_NAME" VARCHAR2(25),
"STREET" VARCHAR2(40),
"CITY" VARCHAR2(30),
"STATE" VARCHAR2(30),
"ZIP" VARCHAR2(15),
"COUNTRY" VARCHAR2(35)
)
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENT
S 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT
)
TABLESPACE "PSDEFAULT"

Q#4: How to extract the index definition (DDL statements) from an Oracle database without having to go through a stack of dictionary views?

A: By calling the GET_DDL() function of metadata package DBMS_METADATA.

Syntax:
select DBMS_METADATA.GET_DDL('INDEX','<index_name>') from DUAL;

eg.,
SQL> create index PERSON_IDX on PERSON ( SSN ); 

Index created.

SQL> set long 1000
SQL> set pagesize 0

SQL> select DBMS_METADATA.GET_DDL('INDEX','PERSON_IDX') from DUAL;

CREATE INDEX "FS890"."PERSON_IDX" ON "FS890"."PERSON" ("SSN")
PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MA
XEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL
DEFAULT)
TABLESPACE "PSDEFAULT"

If the interest is only to get the indexed column names for an index, simply query COLUMN_NAME of table USER_IND_COLUMNS.

Syntax:
select COLUMN_NAME from USER_IND_COLUMNS where INDEX_NAME = '<index_name>';

eg.,
SQL> column COLUMN_NAME format A15
SQL> select COLUMN_NAME from USER_IND_COLUMNS where INDEX_NAME = 'PERSON_IDX';

COLUMN_NAME
---------------
SSN
________________
Technorati tags:
| | |


Comments:
Many thanks for the post. The information really saved me a lot of time.
 
Giri,

Thanks a lot for this post. Helped me a lot in my work since we keep getting requests to provide view definitions. saved us a lot of time and effort.
 
The post is really informative...
 
thanks so much! You saved me lots of time!!
 
Hi!
select from dba_views returns


select using Dbms_Metadata.Get_Ddl() returns

Do you know why?
 
:)
1)"Long"
2)"CLOB"
 
Great! Precisely what I need. Thanks for the good work. I need to find out database definitions for my maintenance work.
 
Thanks for the post. It helped me.
 
I googled and googled to find out how to get the definition of a view. FINALLY I found it here. I just wanted to say Thank You!
 
Thanks Very Much!
 
just do
select dbms_metadata.get_ddl('VIEW','VIEW_NAME','SCHEMA') from dual;

very simple son
 
This post is very helpful. Thanks a lot!
 
Post a Comment

Links to this post:

Create a Link



<< Home


2004-2014 

This page is powered by Blogger. Isn't yours?