Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
3Activity
0 of .
Results for:
No results containing your search query
P. 1
what is an Oracle Nested Table

what is an Oracle Nested Table

Ratings: (0)|Views: 250|Likes:
Published by SHAHID FAROOQ
what is an Oracle Nested Table?
Let’s take some of the confusion out of Oracle nested tables. This article takes a step by step approach to create a table with a nested table and then query that table while showing some key DBA views we can use to describe the structures. I think we have all come to grips with what a database table is these days. However, when we start extending the very nature of a simple table we tend to get into trouble. I liken this to taking simple SQL statements and exte
what is an Oracle Nested Table?
Let’s take some of the confusion out of Oracle nested tables. This article takes a step by step approach to create a table with a nested table and then query that table while showing some key DBA views we can use to describe the structures. I think we have all come to grips with what a database table is these days. However, when we start extending the very nature of a simple table we tend to get into trouble. I liken this to taking simple SQL statements and exte

More info:

Published by: SHAHID FAROOQ on Aug 29, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

11/28/2010

pdf

text

original

 
what is an Oracle Nested Table?
Let’s take some of the confusion out of Oracle nested tables. This article takes a step by stepapproach to create a table with a nested table and then query that table while showing some key DBA views we can use to describe the structures.
I think we have all come to grips with what a database table is these days. However, when we startextending the very nature of a simple table we tend to get into trouble. I liken this to taking simpleSQL statements and extending into correlated subqueries where some of us just start to get lost andunable to relate. Nested tables are much like this, they take us to a form of abstraction we just don’tlike thinking about. After all, when talking about something like nested tables, we are sort of going outof bounds and creating a relationship within a table that could easily be done with two distinct andseparate tables.In the purest sense, a nested table is nothing more than a normal table that has a column whosedatatype is another table. Call it a table within a table, a column that is of type table, or a column thathas for its values another table. Confused yet? Let’s step through the process.First, we must create our own object type called ADDRESS_TYPE. Then we create a table of ADDRESS_TYPE called ADDRESS_TABLE_TYPE. The table ADDRESS_TABLE_TYPE is what wewill use as our nested table column type. All of this sounds quite confusing. Just try to remember thatevery attribute of a table needs some form of data type. We are creating our own data type herecalled ADDRESS_TABLE_TYPE that is a table of the type address_type. Below are the two DDLstatements with a DESCribe of each of them to show you that ADDRESS_TYPE andADDRESS_TABLE_TYPE really have the same attributes. It’s just that ADDRESS_TABLE_TYPE is aTABLE of ADDRESS_TYPE—denoting that we will be able to store multiple addresses for every
rowin the table we will be defining.CREATE TYPE address_type AS OBJECT(ADDR_LINE VARCHAR2(50),CITY VARCHAR2(50),STATE VARCHAR2(02),ZIP VARCHAR2(05));/CREATE TYPE address_table_type AS TABLE OF address_type;/ SQL> DESC address_typeName Type------------------ --------------ADDR_LINE VARCHAR2(50)CITY VARCHAR2(50)STATE VARCHAR2(2)ZIP VARCHAR2(5) SQL> DESC address_table_typeaddress_table_type TABLE OF ADDRESS_TYPEName Type------------------ --------------
 
ADDR_LINE VARCHAR2(50)CITY VARCHAR2(50)STATE VARCHAR2(2)ZIP VARCHAR2(5)
Since many of us like to use the DBA internal views to look at our objects, here is the SQL to look atour two newly created types. Nothing too exciting here but just note that they are of OBJECT_TYPEequal to TYPE—denoting that they are user-defined types. Also, note that in the DBA_TYPES viewwhile the ADDRESS_TYPE is an OBJECT the ADDRESS_TABLE_TYPE is a collection.
SQL> select object_name,object_typefrom dba_objectswhere object_name like 'ADDRESS_%';OBJECT_NAME OBJECT_TYPE-------------------- --------------ADDRESS_TYPE TYPEADDRESS_TABLE_TYPE TYPESQL> select TYPE_NAME,TYPECODE,ATTRIBUTESfrom dba_typeswhere type_name like 'ADDRESS_%';TYPE_NAME TYPECODE ATTRIBUTES------------------- ---------------- ----------ADDRESS_TYPE OBJECT 4ADDRESS_TABLE_TYPE COLLECTION 0
You can also extract what the DDL sort of looked like from the DBA_SOURCE view if you ever wantto investigate what someone else might have created. Sometimes this helps you get your headaround some interesting naming conventions that will throw the best of us off.
SQL> select textfrom dba_sourcewhere name = 'ADDRESS_TYPE';TEXT-------------------------------------------TYPE address_type AS OBJECT(ADDR_LINE VARCHAR2(50),CITY VARCHAR2(50),STATE VARCHAR2(02),ZIP VARCHAR2(05));7 rows selected.SQL> select textfrom dba_sourcewhere name = 'ADDRESS_TABLE_TYPE';TEXT
 
-------------------------------------------------------------------TYPE address_table_type AS TABLE OF address_type;
So now, in order to create a table, we just need to specify a column name (ADDRESSES in this case)and our newly created TYPE (ADDRESS_TABLE_TYPE). Quite simple. Below is the DDL to createthe table along with some SQL on how we might want to look at this newly created table. When welook at the DBA_TAB_COLS view, we can see the data type ADDRESS_TABLE_TYPE and then,additionally, we can look at the nested DBA view DBA_NESTED_TABLES andDBA_NESTED_TABLE_COLS.
CREATE TABLE employee (Name VARCHAR2(20),addresses ADDRESS_TABLE_TYPE)NESTED TABLE addresses STORE AS addresses_table;SQL> DESC employeeName Type-------------------- -----------------NAME VARCHAR2(20)ADDRESSES ADDRESS_TABLE_TYPESQL> DESC addresses_tableName Type------------------- -------------ADDR_LINE VARCHAR2(50)CITY VARCHAR2(50)STATE VARCHAR2(2)ZIP VARCHAR2(5)SELECT table_name, column_name, data_type, data_lengthFROM dba_tab_colsWHERE table_name = 'EMPLOYEE';TABLE_NAME COLUMN_NAME DATA_TYPE DATA_LENGTH---------- ------------------- ------------------- -----------EMPLOYEE ADDRESSES ADDRESS_TABLE_TYPE 16EMPLOYEE NAME VARCHAR2 20EMPLOYEE SYS_NC0000200003$ RAW 16SQL> SELECT table_name, table_type_name,parent_table_columnFROM dba_nested_tablesWHERE table_name = 'ADDRESSES_TABLE';TABLE_NAME TABLE_TYPE_NAME PARENT_TABLE_COLUMN---------------- -------------------------- -----------------------ADDRESSES_TABLE ADDRESS_TABLE_TYPE ADDRESSESSQL> select table_name,column_name,data_typefrom dba_nested_table_colswhere table_name = 'ADDRESSES_TABLE';TABLE_NAME COLUMN_NAME DATA_TYPE

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->