Release 2 (8.1.6)
Part Number A76965-01
This chapter describes object views. It contains the following major sections:
Just as a view is a virtual table, an object view is a virtual object table.
Oracle provides object views as an extension of the basic relational view mechanism. By using object views, you can create virtual object tables from data--of either built-in or user-defined types--stored in the columns of relational or object tables in the database.
Object views provide the ability to offer specialized or restricted access to the data and objects in a database. For example, you might use an object view to provide a version of an employee object table that does not have attributes containing sensitive data and doesn't have a deletion method.
Object views allow the use of relational data in object-oriented applications. They let users:
Using object views can lead to better performance. Relational data that make up a row of an object view traverse the network as a unit, potentially saving many round trips.
You can fetch relational data into the client-side object cache and map it into C or C++ structures so 3GL applications can manipulate it just like native structures.
Object views provide a gradual migration path for legacy data.
Object views provide for co-existence of relational and object-oriented applications. They make it easier to introduce object-oriented applications to existing relational data without having to make a drastic change from one paradigm to another.
Object views provide the flexibility of looking at the same relational or object data in more than one way. Thus you can use different in-memory object representations for different applications without changing the way you store the data in the database.
Conceptually, the procedure for defining an object view is simple:
The object identifier corresponds to the unique object identifier that Oracle generates automatically for rows of object tables. In the case of object views, however, the declaration must specify something that is unique in the underlying data (for example, a primary key).
If the object view is based on a table or another object view and you do not specify an object identifier, Oracle uses the object identifier from the original table or object view.
If you want to be able to update a complex object view, you may have to take another step:
After these steps you can use an object view just like an object table.
For example, the following SQL statements define an object view:
CREATE TABLE emp_table ( empnum NUMBER (5), ename VARCHAR2 (20), salary NUMBER (9, 2), job VARCHAR2 (20) ); CREATE TYPE employee_t ( empno NUMBER (5), ename VARCHAR2 (20), salary NUMBER (9, 2), job VARCHAR2 (20) ); CREATE VIEW emp_view1 OF employee_t WITH OBJECT OID (empno) AS SELECT e.empnum, e.ename, e.salary, e.job FROM emp_table e WHERE job = 'Developer';
The object view looks to the user like an object table whose underlying type is
employee_t. Each row contains an object of type
employee_t. Each row has a unique object identifier.
Oracle constructs the object identifier based on the specified key. In most cases it is the primary key of the base table. If the query that defines the object view involves joins, however, you must provide a key across all tables involved in the joins, so that the key still uniquely identifies rows of the object view.
"Updating Object Views" for more information about writing an INSTEAD OF trigger
Data in the rows of an object view may come from more than one table, but the object still traverses the network in one operation. When the instance is in the client side object cache, it appears to the programmer as a C or C++ structure or as a PL/SQL object variable. You can manipulate it like any other native structure.
You can refer to object views in SQL statements the same way you refer to an object table. For example, object views can appear in a SELECT list, in an UPDATE SET clause, or in a WHERE clause.
You can also define object views on object views.
You can access object view data on the client side using the same OCI calls you use for objects from object tables. For example, you can use OCIObjectPin() for pinning a REF and OCIObjectFlush() for flushing an object to the server. When you update or flush to the server an object in an object view, Oracle updates the object view.
Oracle Call Interface Programmer's Guide for more information about OCI calls
You can update, insert, and delete the data in an object view using the same SQL DML you use for object tables. Oracle updates the base tables of the object view if there is no ambiguity.
A view is not updatable if its view query contains joins, set operators, aggregate functions, GROUP BY, or DISTINCT. If a view query contains pseudocolumns or expressions, the corresponding view columns are not updatable. Object views often involve joins.
To overcome these obstacles Oracle provides INSTEAD OF triggers. They are called INSTEAD OF triggers because Oracle executes the trigger body instead of the actual DML statement.
INSTEAD OF triggers provide a transparent way to update object views or relational views. You write the same SQL DML (INSERT, DELETE, and UPDATE) statements as for an object table. Oracle invokes the appropriate trigger instead of the SQL statement, and the actions specified in the trigger body take place.
A nested table can be modified by inserting new elements and updating or deleting existing elements. Nested table columns that are virtual or synthesized, as in a view, are not usually updatable. To overcome this, Oracle allows INSTEAD OF triggers to be created on these columns.
The INSTEAD OF trigger defined on a nested table column of a view is fired when the column is modified. Note that if the entire collection is replaced by an update of the parent row, then the INSTEAD OF trigger on the nested table column is not fired.
Oracle8i Application Developer's Guide - Fundamentals for a purchase order/line item example that uses an INSTEAD OF trigger on a nested table column