Replacing an Oracle Object Type Column with a REF to a Table of that Type

The following example illustrates an alternative to using an Oracle Object datatype as a column in a table. In a table definition, you can replace a line that identifies a column as an Oracle Object datatype with a REF to a table that is of the Oracle Object datatype. That is, assume you have a table definition with a column that is an Oracle Object Type. Create a second table with the same name and type as the Oracle Object. You can then redefine the column in the original table as a REF to the second table. This powerful technique gives you certain advantages:

The following example demonstrates the SQL code needed to make use of this technique. The example begins with the definition of the address_objtyp as an Oracle Object type. It then creates a table named address_objtab of type address_objtyp. The definition of the table people_reltab2 has already been rewritten to replace the line:

address  address_objtyp

with the REF statement:

address_ref REF address_objtyp
 SCOPE IS address_objtab ) -- REF specified

The SQL code concludes with an example of inserting data into the tables.

CREATE TYPE address_objtyp AS OBJECT (
  street VARCHAR2(200),
  city VARCHAR2(200),
  state CHAR(2),
  zipcode VARCHAR2(20))
  /
CREATE TABLE address_objtab OF address_objtyp ;
/
CREATE TABLE people_reltab2 (
 id NUMBER(4)
 CONSTRAINT pk_people_reltab2 PRIMARY KEY,
 name VARCHAR(15),
 Phone Number(6),
 address_ref REF address_objtyp
 SCOPE IS address_objtab ) -- REF specified
  /
Insert into address_objtab values(address_objtyp
 ('St1','FosterCity', 'CA', '94491'))
 Insert into address_objtab values
 (address_objtyp('St2','Redwood', 'CA', '94331'))
  insert into people_reltab2 values( 1, 'Sanjay',911,
  (SELECT REF(p) FROM
  address_objtab p WHERE street = 'St1'));
  insert into people_reltab2 values( 2, 'KING',1241,
  (SELECT REF(p) FROM
  address_objtab p WHERE street = 'St2'));


Related topics
Ways to Represent Oracle Object Types in Entity Objects
Representing an Oracle Object Type with a User-defined Domain
Representing a VARRAY that Contains Oracle Object Types
About Generating Entity Objects, Associations, and Database Tables
What Is an Entity Object?
What Is an Entity Attribute?
Business Component Data Types