Object Relational Databases

73
1 Object Relational Databases

description

Object Relational Databases. Object-Relational Data Model. A straightforward subset of ODM: only tuple types at the top level More precisely: Set of classes, where each class has a tuple type (the types of the tuple component can be anything) - PowerPoint PPT Presentation

Transcript of Object Relational Databases

Page 1: Object Relational Databases

1

Object Relational Databases

Page 2: Object Relational Databases

2

Object-Relational Data Model• A straightforward subset of ODM: only

tuple types at the top level

• More precisely:• Set of classes, where each class has a tuple type (the

types of the tuple component can be anything)

• Each tuple is an object of the form (oid, tuple-value)

• Pure relational data model:• Each class (relation) has a tuple type, but

• The types of tuple components must be primitive

• Oids are not explicitly part of the model – tuples are pure values

Page 3: Object Relational Databases

3

Objects in SQL:1999

• Object-relational extension of SQL-92• Includes the legacy relational model• SQL:1999 database = database = a finite set of relations• relationrelation = a set of tuples (extends legacy relations)

OROR

a set of objects (completely new)

• object = object = (oid, tuple-value)• tupletuple = tuple-value

• tuple-valuetuple-value = [Attr1: v1, …, Attrn: vn]

Page 4: Object Relational Databases

4

SQL:1999 Tuple Values

• Tuple valueTuple value: [Attr1: v1, …, Attrn: vn]

– AttrAttrii are all distinct attributesare all distinct attributes

– Each Each vi is one of these:is one of these:– Primitive value: a constant of type Primitive value: a constant of type CHAR(…), INTEGER, CHAR(…), INTEGER,

FLOATFLOAT, etc., etc.

– Reference value: an object IdReference value: an object Id

– Another tuple valueAnother tuple value

– A collection valueA collection value This one is a disappointment. SETOF and LISTOF are This one is a disappointment. SETOF and LISTOF are not not

supported.supported. Only the ARRAY construct is – a fixed size array

Page 5: Object Relational Databases

5

Row Types

• The same as the original (legacy) relational tuple type. However:– Row types can now be the types of the individual attributes in

a tuple

– In the legacy relational model, tuples could occur only as top-level types

CREATE TABLE PERSON (

Name CHAR(20),

Address ROW(Number INTEGER, Street CHAR(20), ZIP CHAR(5))

)

Page 6: Object Relational Databases

6

Row Types

• Use path expressions to refer to the components of row types:SELECT P.NameFROM Person PWHERE P.Address.ZIP = ‘11794’

• Update operations:INSERT INTO PERSON(Name, Address)VALUES (‘John Doe’, ROW(666, ‘Hollow Rd.’, ‘66666’))

UPDATE PERSONSET Address.ZIP = ‘66666’WHERE Address = ‘55555’

UPDATE PERSONSET Address = ROW(21, ‘Main St’, ‘12345’)WHERE Address = ROW(123, ‘Maple Dr.’, ‘54321’) AND Name = ‘J. Public’

Page 7: Object Relational Databases

7

User Defined Types (UDT)OR ABSTRACT DATA TYPE (ADT)

• UDTs allow specification of complex objects/tuples, methods, and their implementation

• Like ROW types, UDTs can be types of individual attributes in tuples

• UDTs can be much more complex than ROW types (even disregarding the methods): the components of UDTs do not need to be elementary types

Page 8: Object Relational Databases

8

A UDT ExampleCREATE TYPE PersonType AS (

Name CHAR(20),Address ROW(Number INTEGER, Street CHAR(20), ZIP

CHAR(5)) );

CREATE TYPE StudentType UNDER PersonType AS (Id INTEGER,Status CHAR(2)

)METHOD award_degree() RETURNS BOOLEAN;

CREATE METHOD award_degree() FOR StudentTypeLANGUAGE CEXTERNAL NAME ‘file:/home/admin/award_degree’;

File that holds the binary code

Page 9: Object Relational Databases

9

Using UDTs in CREATE TABLE

• As an attribute type:

CREATE TABLE TRANSCRIPT (Student StudentType,CrsCode CHAR(6),Semester CHAR(6),Grade CHAR(1)

)

• As a table type:

CREATE TABLE STUDENT OF StudentType;

Such a table is called typed table.typed table.

A previously defined UDT

Page 10: Object Relational Databases

10

Objects

• Only typed tables contain objects (ie, tuples with oids)

• Compare:CREATE TABLE STUDENT OF StudentType;

and

CREATE TABLE STUDENT1 (

Name CHAR(20),

Address ROW(Number INTEGER, Street CHAR(20), ZIP CHAR(5)),

Id INTEGER,

Status CHAR(2)

)

• Both contain tuples of exactly the same structure

• Only the tuples in STUDENT – not STUDENT1 – have oids

• Will see later how to reference objects, create them, etc.

Page 11: Object Relational Databases

11

Querying UDTs

• Nothing special – just use path expressions

SELECT T.Student.Name, T.Grade

FROM TRANSCRIPT T

WHERE T.Student.Address.Street = ‘Main St.’

Note: T.Student has the type StudentType. The attribute Name is not declared explicitly in StudentType, but is inherited from PersonType.

Page 12: Object Relational Databases

12

Updating User-Defined Types

• Inserting a record into TRANSCRIPT:INSERT INTO TRANSCRIPT(Student,Course,Semester,Grade)

VALUES (????, ‘CS308’, ‘2000’, ‘A’)

The type of the Student attribute is StudentType. How does one insert a value of this type (in place of ????)?

Further complication: the UDT StudentType is encapsulated,encapsulated, ie, it is accessible only through public methods, which we did not define

Do it through the observerobserver and mutatormutator methods provided

by the DBMS automatically

Page 13: Object Relational Databases

13

Observer Methods• For each attribute A of type T in a UDT, an SQL:1999 DBMS is supposed to

supply an observer methodobserver method, A: ( ) T, which returns the value of A (the notation “( )” means that the method takes no arguments)

• Observer methods for StudentType:• Id: ( ) INTEGER• Name: ( ) CHAR(20)• Status: ( ) CHAR(2)• Address: ( ) ROW(INTEGER, CHAR(20), CHAR(5))

• For example, inSELECT T.Student.Name, T.GradeFROM TRANSCRIPT TWHERE T.Student.Address.Street = ‘Main St.’

Name and Address are observer methods, since T.Student is of type StudentType

Note: Grade is not an observer, because TRANSCRIPT is not part of a UDT,but this is a conceptual distinction – syntactically there is no difference

Page 14: Object Relational Databases

14

Mutator Methods

• An SQL:1999 DBMS is supposed to supply, for each attribute A of type T in a UDT U, a mutator methodmutator method

AA:: T T U U

For any object o of type U, it takes a value t of type T and replaces the old value of o.A with t; it returns thenew value of the object. Thus, o.A(t) is an object of

type U• Mutators for StudentType:

• Id: INTEGER StudentType• Name: CHAR(20) StudentType• Address: ROW(INTEGER, CHAR(20), CHAR(5)) StudentType

Page 15: Object Relational Databases

15

Example: Inserting a UDT Value

INSERT INTO TRANSCRIPT(Student,Course,Semester,Grade)

VALUES (

NEW StudentType().Id(111111111).Status(‘G5’).Name(‘Joe Public’)

.Address(ROW(123,’Main St.’, ‘54321’)) ,

‘CS532’,

‘S2002’, ‘A’

)

‘CS532’, ‘S2002’, ‘A’ are primitive values for the attributes Course, Semester, Grade

Create a blank StudentType object

Add a value for Id

Add a value for Status

Add a value for the Address attribute

Page 16: Object Relational Databases

16

Example: Changing a UDT Value

UPDATE TRANSCRIPT

SET Student = Student.Address(ROW(21,’Maple St.’,’12345’)).Name(‘John Smith’),

Grade = ‘B’

WHERE Student.Id = 111111111 AND CrsCode = ‘CS532’ AND Semester = ‘S2002’

• Mutators are used to change the values of the attributes Address and Name

Change AddressChange Name

Page 17: Object Relational Databases

17

Referencing Objects

• Consider againCREATE TABLE TRANSCRIPT (

Student StudentType,CrsCode CHAR(6),Semester CHAR(6),Grade CHAR(1)

)

• Problem: TRANSCRIPT records for the same student refer to distinct values of type StudentType (even though the contents of these values may be the same) – a maintenance/consistency problem

• Solution: use self-referencing columnself-referencing column (next slide)– Bad design, which distinguishes objects from their references

– Not truly object-oriented

Page 18: Object Relational Databases

18

Self-Referencing Column

• Every typed table has a self-referencing columnself-referencing column– Normally invisible

– Contains explicit object Id for each tuple in the table

– Can be given an explicit name – the only way to enable referencing of objects

CREATE TABLE STUDENT2 OF StudentType

REF IS stud_oid;

Self-referencing columns can be used in queries just like regular columns

Their values cannot be changed, however

Self-referencing column

Page 19: Object Relational Databases

19

Reference Types and Self-Referencing Columns

• To reference objects, use self-referencing columns + reference reference typestypes: REF(some-UDT)

CREATE TABLE TRANSCRIPT1 ( Student REF(StudentType) SCOPE STUDENT2,

CrsCode CHAR(6), Semester CHAR(6), Grade CHAR(1) )

• Two issues:• How does one query the attributes of a reference type• How does one provide values for the attributes of type REF(…)

– Remember: you can’t manufacture these values out of thin air – they are oids!

Reference type

Typed table where the values are drawn from

Page 20: Object Relational Databases

20

Querying Reference Types

• Recall: Student REF(StudentType) SCOPE STUDENT2 in TRANSCRIPT1. How does one access, for example, student names?

• SQL:1999 has the same misfeature as C/C++ has (and which Java and OQL do not have): it distinguishes between objects and references to objects. To pass through a boundary of REF(…) use “” instead of “.”

SELECT T.StudentName, T.Grade

FROM TRANSCRIPT1 TWHERE T.StudentAddress.Street = “Main St.”

Crossing REF(…) boundary – use

Not crossing REF(…) boundary, use “.”

Page 21: Object Relational Databases

21

Inserting REF Values• How does one give values to REF attributes, like Student in

TRANSCRIPT1? • Need to use explicit self-referencing columns, like stud_oid in

STUDENT2

• Example: Creating a TRANSCRIPT1 record whose Student attribute has an object reference to an object in STUDENT2:

INSERT INTO TRANSCRIPT1(Student,Course,Semester,Grade)

SELECT S.stud_oid, ‘HIS666’, ‘F1462’, ‘D’

FROM STUDENT2 S

WHERE S.Id = ‘111111111’

Explicit self-referential

column of STUDENT2

Page 22: Object Relational Databases

22

Collection Data Types

• The lack of Set data type severely cripples the object-oriented capabilities of SQL:1999. However, sets will likely be added during the next update of SQL. Sets will look something like the following:

CREATE TYPE StudentType UNDER PersonType AS (

Id INTEGER,

Status CHAR(2),

Enrolled SETOF(REF(CourseType)) SCOPE COURSE

)

A bunch of references to objects in a typed table COURSE

Page 23: Object Relational Databases

23

Querying Collection Types

• For each student, list the Id, street, and the courses in which the student is enrolled:

SELECT S.Id, S.Address, C.Name

FROM STUDENT S, COURSE C

WHERE C.CrsCode IN

( SELECT E CrsCode

FROM S.Enrolled E )

• Note: E is bound to a set of object references, so E CrsCode is also a set

Page 24: Object Relational Databases

24

Oracle

Object Relational Features

Page 25: Object Relational Databases

25

Oracle’sGOAL:• Add object-oriented features on top of the existing

relational database.• Add support for complex data types.• Add support for encapsulation

ISSUES• Only a partial implementation.• No concept of inheritance.

– PL/SQL

• No polymorphism– Overloading– PL/SQL

Page 26: Object Relational Databases

26

Objects• Object types

– ADT

• Collection types– VARRAY– Nested table

Page 27: Object Relational Databases

27

Object TypesEach object has the following:

• Name - uniquely identifies it within a schema.

• Attributes – primitive data types or other complex objects.

• Methods – written in PL/SQL

Page 28: Object Relational Databases

28

Declaring a typeCREATE TYPE person_o AS OBJECT (

fname VARCHAR2(20),

lname VARCHAR2(20)

);

CREATE TYPE emp_o AS OBJECT (

emp person_o,

eid NUMBER

);

Page 29: Object Relational Databases

29

Object Table• Creates a table where each row represents

an object– CREATE TABLE person_table OF person_o;

– CREATE TABLE emp_table OF emp_o;

• Inserting data•INSERT INTO person_table VALUES (‘John’, ‘Doe’);

•INSERT INTO person_table VALUES (person_o(‘John’, ‘Doe’));

•INSERT INTO emp_table VALUES (person_o(‘John’, ‘Doe’), 1);

Page 30: Object Relational Databases

30

Querying the data• You can view the table in two fashions:

– As an object where a column represents an object•SELECT VALUE(p) from person_table p;

– As a relation where each object attributes is mapped to a column.•SELECT * from person_table;

Page 31: Object Relational Databases

31

Dot notationCREATE TYPE emp_o AS OBJECT (

emp person_o,

eid NUMBER

);

SELECT * FROM emp_table;

SELECT p.emp.fname, p.emp.lname, p.eid

FROM emp_table p;

Page 32: Object Relational Databases

32

Reference Pointers• REFs are used to point from one object to

another . The target of the reference must be an object table.

• REFS must be valid when they are stored

• REFS do not have to remain valid – dangling REF. ISDANGLING

• REFs are strongly typed, but they can be scoped or non-scoped

Page 33: Object Relational Databases

33

Reference Pointers (cont)• Non-scoped CREATE TABLE dept_table (dno NUMBER, manager REF emp_o);

• Scoped – constrained to a table CREATE TABLE dept_table (dno NUMBER, manager REF emp_o,

SCOPE FOR (manager) IS emp_table);

• Insertion– INSERT INTO dept_table VALUES (1,

(SELECT REF(p) FROM emp_table p WHERE fname = ‘john’ AND lname = ‘doe’));

Page 34: Object Relational Databases

34

Using REFs

SELECT * FROM dept_table;

SELECT d.dno, DEREF(d.manager) FROM dept_table d;

SELECT d.dno d.manager.lname FROM dept_table d;

Page 35: Object Relational Databases

35

Methods• Functions or procedures written in

PL/SQL or Java are stored in the database.

• Methods fall under three categories:1. Constructor

2. Comparison

3. Static

Page 36: Object Relational Databases

36

Constructor• Constructors are automatically created by

the system.

• You already saw this –person_o(‘john’, ‘doe’);

• NULL values– INSERT INTO person_table VALUES

(person_o(NULL,NULL))– INSERT INTO person_table VALUES

(NULL);

Page 37: Object Relational Databases

37

Comparison• Methods used to do compare object

instances. • ORDER – takes another instance and

compares it to the current instance

• MAP – Returns a number that is used to rank instances of object types

negative this < arg

zero this = arg

positive this > arg

Page 38: Object Relational Databases

38

MAP and ORDER• An object can have a single MAP method

or a single ORDER method.

• Which one should you use and why?

Page 39: Object Relational Databases

39

Map MethodCREATE TYPE foo AS OBJECT (

value NUMBER,

MAP MEMBER FUNCTION mp RETURN NUMBER);

/

CREATE OR REPLACE TYPE BODY foo AS

MAP MEMBER FUNCTION mp RETURN NUMBER IS

BEGIN

RETURN value;

END;

END;

Page 40: Object Relational Databases

40

Order MethodCREATE TYPE foo AS OBJECT (

value NUMBER,ORDER MEMBER FUNCTION ord (that foo) RETURN NUMBER);

/CREATE OR REPLACE TYPE BODY foo AS ORDER MEMBER FUNCTION ord(that foo) RETURN NUMBER IS

BEGIN IF (value < that.value) THEN RETURN -1; ELSIF (value > that.value) THEN RETURN +1; ELSE RETURN 0; END IF;

END; END;/

Page 41: Object Relational Databases

41

MethodCREATE TYPE person2_o AS OBJECT (

fname VARCHAR2(20),

lname VARCHAR2(20),

MEMBER FUNCTION fullname RETURN VARCHAR2,

PRAGMA RESTRICT_REFERENCES(fullname, WNDS));

/

CREATE OR REPLACE TYPE BODY person2_o AS

MEMBER FUNCTION fullname RETURN VARCHAR2 IS

BEGIN

RETURN fname || ' ' || lname;

END;

END;

/

Page 42: Object Relational Databases

42

Invoking the methodCREATE TABLE p2tab OF person2_o;

SELECT p.fullname()

FROM p2tab p

WHERE p.fullname() LIKE ‘john%’;

Page 43: Object Relational Databases

43

PRAGMA• Directives to the compiler that restrict what

can be done in a method.• Add line to table declarationPRAGMA RESTRICT_REFERENCES( fullname, WNDS, WNPS, RNDS, RNPS)

• WNDS is mandatory if you want to call the methods from SQL.

WNDS write no database state

WNPS write no package state

RNDS read no database state

RNPS read no package state

Page 44: Object Relational Databases

44

Collection Types• Oracle provides two techniques for

modeling one-to-many relationships.– VARRAY – stores a fixed number of repeating

attributes in a column.

– Nested Tables – table within a table.

• Collections can be columns in tables or attributes of object types.

Page 45: Object Relational Databases

45

VARRAY• Just like a C array.

– It has a fixed size– It contains objects of the same datatype.– Each element has an index

• VARRAYs can be used as columns in tables or as attributes in objects.

• Data stored is stored in the VARRAY as a raw or BLOB.

Page 46: Object Relational Databases

46

Declaring a VARRAYCREATE TYPE type_name AS VARRAY (limit) OF data_type;

CREATE TYPE tire_o AS OBJECT( PSI NUMBER, MFG VARCHAR2(20));/CREATE TYPE bike_tire_vtype AS VARRAY(2) OF tire_o;

/CREATE TABLE bike_table ( mfg VARCHAR2(20), wheels bike_tire_vtype);

Page 47: Object Relational Databases

47

Initializing VARRAY• Constructor

• Using the database

• Using direct assignment

Page 48: Object Relational Databases

48

VARRAY ConstructorINSERT INTO bike_table VALUES ('RF900R',

bike_tire_vtype(tire_o(32,'metzler'),

tire_o(35,'metzler')));

• You don’t have to set all of the valuesINSERT INTO bike_table VALUES ('RF900R',

bike_tire_vtype(tire_o(32,'metzler'),

NULL)); INSERT INTO bike_table VALUES ('RF900R',

bike_tire_vtype(tire_o(32,'metzler'));

Page 49: Object Relational Databases

49

Direct AssignmentDECLARE src bike_tire_vtype:= bike_tire_vtype (tire_o(32, 'cheap'),tire_o(35, 'cheap')); tgt bike_tire_vtype;BEGIN src(1) := tire_o(42, 'cheaper'); tgt := src; FOR cnt IN 1..tgt.COUNT LOOP DBMS_OUTPUT.PUT(tgt(cnt).psi||

tgt(cnt).mfg);DBMS_OUTPUT.NEW_LINE;

END LOOP;END;/

Page 50: Object Relational Databases

50

From a Database FetchCREATE TABLE sets ( code number primary key, pair bike_tire_vtype default bike_tire_vtype(tire_o(1,'generic'), tire_o(2, 'generic')));

/INSERT INTO sets(code) values (1);/

• SQLinsert into sets (SELECT 2, s.pair from sets s where code = '1')

Page 51: Object Relational Databases

51

From a Database Fetch (PL/SQL)CREATE OR REPLACE Function getpair(base in sets.code%type)RETURN sets.pair%type IS

--DECLARE

temp sets.pair%type;

BEGIN

SELECT s.pair INTO temp FROM sets s

WHERE s.code = base;

return temp;

END;

/

Page 52: Object Relational Databases

52

Nested Table• Table within another table.

• The tables don’t have a fixed maximum size, because the data is all out-of-line in the external table.

• The nesting has a depth of one.

• The tables are unordered.

• The tables can have triggers and indexes.

• The nested table can’t be directly queried.

Page 53: Object Relational Databases

53

Creating a simple nested table• Create the nested table datatypeCREATE TYPE fruit_ntypes AS TABLE OF varchar2(20);

/

• Nested table as a columnCREATE TABLE fruit_basket(

id NUMBER,

fruits fruit_ntypes)

nested table fruits store as fruit_selections;

Page 54: Object Relational Databases

54

Creating the Nested TableCREATE TYPE tire_ntype AS TABLE OF tire_o;

/

CREATE TYPE auto_table AS OBJECT (

mfg varchar2(20),

model varchar2(20),

tires tire_ntype );

/

CREATE TABLE auto_info OF auto_table (

primary key (model, mfg)

)

nested table tires store as tire_detail;

Page 55: Object Relational Databases

55

Inserting into the Object TablesINSERT INTO auto_info values (

'FORD',

'RANGER',

tire_ntype (tire_o(32, 'bridgestone'),

tire_o(32, 'bridgestone'),

tire_o(33, 'yokohama'),

tire_o(32, 'yokohama')))

/

Page 56: Object Relational Databases

56

Collection Functions

THE Flattens the nested table.

CAST Maps a collection of one type to another VARRAY Nested

MULTISET Maps a database to a collection

TABLE Maps a collection to a table

Page 57: Object Relational Databases

57

THE

• What is the average tire pressure of a Ford Ranger?

SELECT avg(p.psi) FROM

THE (SELECT tires FROM auto_info

WHERE mfg = 'FORD' AND

model = 'RANGER') p

Page 58: Object Relational Databases

58

THESELECT column_name [, column_name]…

FROM THE( SELECT nested_table_column

FROM parent_table_name

[WHERE condition parent table])

[WHERE condition_nested_table]

• You can work on at most one row of the parent table.

Page 59: Object Relational Databases

59

CAST• THE works on nested tables but doesn’t

work on VARRAYs.

• Use CAST to cast the VARRAY into a nested table.

Page 60: Object Relational Databases

60

THE update• Replace all of the firestone tires on the Ford

Ranger w/ yokohama tires.

UPDATE THE(SELECT tires

FROM auto_info

WHERE mfg = 'FORD' AND

model = 'RANGER')

SET mfg = 'yokohama'

WHERE mfg = 'firestone'

/

Page 61: Object Relational Databases

61

THE insert• Insert a new row representing the spare tire

in all Ford autos

INSERT INTO THE(SELECT tires

FROM auto_info

WHERE mfg = ‘FORD’)

VALUES (50, ‘GENERIC SPARE’);

Page 62: Object Relational Databases

62

THE complicated insert• Add a spare tire to the Ford Ranger if it has

more than two firestone tires.INSERT INTO THE( SELECT tires FROM auto_info WHERE mfg = 'FORD' AND model = 'RANGER') SELECT 50, 'GENERIC SPARE' FROM THE(SELECT tires FROM auto_info WHERE mfg = 'FORD' AND model = 'RANGER') WHERE mfg = 'firestone' HAVING count(*) >= 2;

Page 63: Object Relational Databases

63

THE delete• Remove all of the firestone tires from the

Ford Ranger.

DELETE THE( SELECT tires

FROM auto_info

WHERE mfg = 'FORD' AND

model = 'RANGER')

WHERE mfg = 'firestone';

Page 64: Object Relational Databases

64

THE CAST• THE does not work with VARRAYs.

• You use the CAST function to cast a VARRAY into a NESTED table.

• You can't access VARRAY

Page 65: Object Relational Databases

65

CAST and COLUMN_VALUECREATE TYPE my_byte_type AS VARRAY(8) OF VARCHAR2(1);

CREATE TABLE my_color ( color VARCHAR2(20), code my_byte_type);

SELECT COLUMN_VALUEFROM THE(SELECT CAST(code AS my_byte_type)

FROM my_color WHERE color = 'orange');

Page 66: Object Relational Databases

66

MULTISET• It converts a set of data into a collection

type.

• MULTISET works w/ CAST

SELECT CAST (MULTISET (SELECT column FROM table) AS collection_type)

FROM DUAL;

• When do you use it?

Page 67: Object Relational Databases

67

MULTISETCREATE TABLE data (

key NUMBER,

PRIMARY KEY (key));

CREATE TABLE data_point (

key NUMBER,

value NUMBER,

FOREIGN KEY (key) REFERENCES data (key));

Page 68: Object Relational Databases

68

MULTISETCREATE TYPE data_ntype AS TABLE OF NUMBER;

CREATE TABLE data_tab_o (

key NUMBER,

vals data_ntype)

NESTED TABLE vals STORE AS data_val_tabs;

Page 69: Object Relational Databases

69

MULTISET (PL/SQL) DECLARE

CURSOR data_c IS

SELECT key, CAST( MULTISET (

SELECT dp.value

FROM data_point dp

WHERE dp.key = d.key) AS

data_ntype) FROM data d;

tkey NUMBER;

tdata data_ntype;

--continued next slide

Page 70: Object Relational Databases

70

MULTISET (PL/SQL)--anonymous block continued

BEGIN

OPEN data_c;

LOOP

FETCH data_c INTO tkey, tdata;

EXIT WHEN data_c%NOTFOUND;

INSERT INTO data_tab_o

VALUES (tkey, tdata);

END LOOP;

CLOSE data_c;

END;

Page 71: Object Relational Databases

71

TABLE• Converts a collection column into a logical

table that you can select on.

TABLE(alias.column);

• Lets you determine if a row contains a collection that satisfies a criteria.

Page 72: Object Relational Databases

72

TABLE• Find the make and model of the autos that

have exactly two bridgestone tires.

SELECT mfg, model

FROM auto_info ai

WHERE (SELECT count(p.mfg)

FROM TABLE(ai.tires) p

WHERE mfg = 'bridgestone') = 2;

Page 73: Object Relational Databases

73

References• Oracle. Oracle 8 documentation

• Pribyl. B. & Dawes C. Oracle PL/SQL Language Pocket Reference. O’Reilly. 1999

• Sunderraman. R. Oracle 8 Programming Primer. Addison Wesley. 1999.