| Lesson 2 | Understanding related object tables (the REF operator) |
| Objective | Describe the SQL enhancements that allow you to query related object tables. |
Within Oracle 8i, references can be obtained for row objects only. For every row object that is stored within an object table, Oracle
8i generates and associates an object identifier (OID) for it. However, objects that are stored within a column do not have such a
unique identifier.
In the following SlideShow, we create an object and reference it within another object by using the REF pointer:
In the following SlideShow, we create an object and reference it within another object by using the REF pointer:
To query a table that is referenced to an object, we can use standard SQL along with the VALUE, REF, and DEREF operators. Let's examine these operators in detail.
REF operator
Object Unique Identifier will return a REF
(reference) of the object, rather than the object itself. Like the VALUE operator, REF takes a correlation
variable as an argument. In this context, a correlation variable is simply a table alias. The following example illustrates
this:
Correlation variable: A correlation variable is a parameter, which is passed when a function is used, e.g. the VALUE finction requires the table alias to be passed as a parameter or correlation variable.
Correlation variable: A correlation variable is a parameter, which is passed when a function is used, e.g. the VALUE finction requires the table alias to be passed as a parameter or correlation variable.
SELECT REF(pc) FROM pet_care_log_obj_table pc WHERE to_char(pc.last_update_datetime, 'DD-MMM-YY') = '12-DEC-99';
REF(PC) ------------------------------------------------------ 0000280209DEE8F40AC4B911D39B07000000000000DEE8F3E3C4B9 11D39B07000000000000008003370002
Given a reference, DEREF will return the original object. We will learn more about DEREF later in this module. The following example illustrates this:
SELECT DEREF(pc.product_id) FROM pet_care_log_obj_table pc WHERE to_char(pc.last_update_datetime, 'DD-MMM-YY') = '12-DEC-99';
DEREF(PC.PRODUCT_ID)(PRODUCT_ID, PRODUCT_NAME, PACKAGE_ID, CURRENT_INVENTORY_COUNT, STORE_COST, SALE --------------------------------------------------- PRODUCT_TYPE(11, 'Fish', NULL, 150, 0, .99, '23-OCT-99', 'JANET', 'Y')
The VALUE operator returns an object, rather than a list of attributes. It takes a correlation variable as an argument. In
this context, a correlation variable is simply a table alias. The following example illustrates how the VALUE operator is
used:
SELECT VALUE(pc) FROM pet_care_log_obj_table pc WHERE to_char(pc.last_update_datetime, 'DD-MMM-YY') = '12-DEC-99';
VALUE(PC)(PRODUCT_ID, LOG_DATETIME, CREATED_BY_USER, LOG_TEXT, LAST_UPDATE_DATETIME) ---------------------------------------------------- PET_CARE_LOG_TYPE(0000220208DEE8F3F3C4B911D39B07000 000000000DEE8F3E2C4B911D39B07000000000000, '12-DE C-99', 'MARK', 'Fish that are sold must be written down on the FISH LOG sheet for proper funds if needed.', '12-DEC-99')