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
1Activity
0 of .
Results for:
No results containing your search query
P. 1
Oracle - varrays and nested tables

Oracle - varrays and nested tables

Ratings: (0)|Views: 8 |Likes:
Published by oracle412
oracle foreign key primary key constraints performance tuning MTS IOT 9i block size backup rman corrupted column drop rename recovery controlfile backup clone architecture database archives export dump dmp duplicate rows extents segments fragmentation hot cold blobs migration tablespace locally managed redo undo new features rollback ora-1555 shrink free space user password link TNS tnsnames.ora listener java
oracle foreign key primary key constraints performance tuning MTS IOT 9i block size backup rman corrupted column drop rename recovery controlfile backup clone architecture database archives export dump dmp duplicate rows extents segments fragmentation hot cold blobs migration tablespace locally managed redo undo new features rollback ora-1555 shrink free space user password link TNS tnsnames.ora listener java

More info:

Published by: oracle412 on Jun 17, 2011
Copyright:Attribution Non-commercial

Availability:

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

08/02/2014

pdf

text

original

 
Using VARRAYs and Nested Tables Administration TipsCopyright © Howard Rogers 2002 2/24/2002 Page
1 of 8
 
Using VARRAYs and Nested Tables
The VARRA
A VARRAY allows you to store multi-line items as part of a single row within a table. Forexample, you might want to store people’s addresses: usually, these are stored in a tableusing columns such as ‘Address Line 1’, ‘Address Line 2’ and so on. By using a VARRAY, wecan store multiple lines of an address as one entity –in fact as an “object” which is a“collection” of otherwise separate items… hence the term often used for them‘collection objects’.A VARRAY is so-called because it is a ‘variable array’ of items. It’s variable in the sensethat you get to define how many items it should be comprised of, and you can then use anynumber of items up to that number. But once you’ve reached that number, it suddenlygets a lot less variable: the upper limit is strictly fixed, and can’t be breached.A simple demo might help.
SQL>
CREATE TYPE ADDRESS AS VARRAY
(3)
OF VARCHAR 
2(25);2
 
/T
YPE CREATED
.
Notice that we create a “type”, with a user-given name, as an array of UP TO 3 items.Each one of those items (in this case) can be a varchar2 entry up to 25 characters long.
SQL>
CREATE TABLE EMPLOYEES
(2
NAME VARCHAR 
2(15),3
ADDR ADDRESS
);T
 ABLE CREATED
.
A perfectly ordinary create table statement –except that the data type for the secondcolumn is the address “type” I created just a moment before.
SQL>
INSERT INTO EMPLOYEES VALUES
(2
 
'H
OWARD
OGERS
',3
ADDRESS
('16
 
B
RADEY
 A 
 VENUE
','H
 AMMONDVILLE
','NSW
 
2170')4
 
);1
ROW CREATED
.
Notice here that to populate the table’s second column, I must reference not the columnname (‘Addr’), but the type name (‘Address’). Here, I’ve used all 3 of my available entries.
SQL>
COMMIT
;C
OMMIT COMPLETE
.
 
Using VARRAYs and Nested Tables Administration TipsCopyright © Howard Rogers 2002 2/24/2002 Page
2 of 8
 
SQL>
SELECT
*
FROM EMPLOYEES
; NAME
 
---------------
 
 ADDR 
 
H
OWARD
OGERS
 ADDRESS('16
 
B
RADEY
 A 
 VENUE
',
 
'H
 AMMONDVILLE
',
 
'NSW
 
2170')
 
And here we see that selecting from the table is a perfectly normal operation, with noparticular syntactical tricks. Note, though, that the display includes the type name [i.e.,“ADDRESS(…”], which makes it look almost as if you are applying a function to the data.That means you’ll have to develop ways of stripping the type name out of the returningdata if you’re going to make use of it in your application.What happens if you try to exceed the permitted array length? Well, this:
SQL>
INSERT INTO EMPLOYEES VALUES
(2
 
'H
OMER 
S
IMPSON
',3
ADDRESS
('3216
 
B
RADLEY
 A 
 VENUE
','S
OUTH
P
 ARK
','H
 AMMONDVILLE
','NSW
 
2170’)4
 
);
 ADDRESS
('3216
 
B
RADLEY
 A 
 VENUE
','S
OUTH
P
 ARK
',
 
'H
 AMMONDVILLE
','NSW
 
2170')*ERROR 
AT LINE
3:ORA-22909:
EXCEEDED MAXIMUM 
 VARRAY
LIMIT
Can you index the varray’d column? Let’s see:
SQL>
CREATE INDEX VARRAY
 _
EMPLOY ON EMPLOYEES
(
 ADDR 
);
CREATE INDEX VARRAY
 _
EMPLOY ON EMPLOYEES
(
 ADDR 
)*ERROR 
AT LINE
1:ORA-02327:
CANNOT CREATE INDEX ON EXPRESSION WITH DATATYPE
 NAMED
 
 ARRAY
 
TYPE
So the answer is no, which means that any access to the arrayed column is extremely likelyto provoke a full table scan. That in turn means that the use of VARRAYs is not somethingyou’d undertake lightly if you were hoping to win some performance awards for yourapplication.Finally, can you reference individual parts of the array? Well, the following ‘global’command works:
SQL>
SELECT ADDR FROM EMPLOYEES
; ADDR ------------------------------------------------------- ADDRESS('16
 
B
RADEY
 A 
 VENUE
',
 
'H
 AMMONDVILLE
',
 
'NSW
 
2170')
…so perhaps something like ‘select addr(1) from employees’ will select just the firstelement of the array?
 
Using VARRAYs and Nested Tables Administration TipsCopyright © Howard Rogers 2002 2/24/2002 Page
3 of 8
 
SQL>
SELECT ADDR 
(1)
FROM EMPLOYEES
;
SELECT ADDR 
(1)
FROM EMPLOYEES
 *ERROR 
AT LINE
1:ORA-00904:
INVALID COLUMN NAME
 
Unfortunately not. In fact, you can’t select a single part of the array –and what’s ratherworse, you can’t update a single part of the array, either. You have to update the entirething, or none of it. If I move house from 16 Bradey Avenue to 15 Bradey Avenue, thecommand has to look like this:
SQL>
UPDATE EMPLOYEES SET ADDR 
=2
ADDRESS
('15
 
B
RADEY
 A 
 VENUE
','H
 AMMONDVILLE
','NSW
 
2170')3
WHERE NAME
='H
OWARD
OGERS
';
In other words, you have to treat the entire thing as a single entity, and minor changes toparts of it nevertheless require you to update all of it. That can lead, of course, to rathera lot of redo and rollback being produced for what would have been, had the (in this case)address been stored as three separate columns, a fairly small update.Are VARRAYs useful, then? Not really. They make selecting just the data from the tabletricky (unless you particularly want to see the TYPE name appear with the data everytime). Updates are expensive. Selects are done via full table scans. And you lose anyability to work with just particular parts of the data… it’s all or nothing.
The Nested Tabl
A ‘nested table’ is, in effect, an infinitely variable VARRAY: there’s no pre-defined limit onthe number of elements that can be stored within it, so if you need to add an extraelement, you are free to do so. What’s more, the various components of the nested tablecan be updated and selected on separately (unlike, as we have just seen, the VARRAYwhich is treated as a monolithic whole).As an example, we can repeat our earlier demo, this time using a nested table:
SQL>
DROP TYPE ADDRESS
;T
YPE DROPPED
.SQL>
CREATE TYPE ADDRESS AS OBJECT
(
HOUSE VARCHAR 
2(25),
STREET VARCHAR 
2(25),
STATE VARCHAR 
2(25));2
 
/T
YPE CREATED
.
Having first gotten rid of our varray version of the address type, here we are creating atype ADDRESS. In practical effect, this version of the type is much the same as our earlier

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)//-->