Download PL/SQL User's Guide and Reference, Release 8.0

Transcript
Manipulating Objects
addr Address, -- not yet defined
phone VARCHAR2(15)
);
This allows you to defer the definition of object type Address. Moreover, the
incomplete type Customer can be made available to other application developers
for use in refs.
Manipulating Objects
You can use an object type in the CREATE TABLE statement to specify the datatype
of a column. Once the table is created, you can use SQL statements to insert an
object, select its attributes, call its methods, and update its state.
In the SQL*Plus script below, the INSERT statement calls the constructor for object
type Rational, then inserts the resulting object. The SELECT statement retrieves
the value of attribute num. The UPDATE statement calls member method
reciprocal, which returns a Rational value after swapping attributes num and
den. Notice that a table alias is required when you reference an attribute or
method. (For an explanation, see Appendix E.)
CREATE
/
INSERT
/
SELECT
/
UPDATE
/
TABLE numbers (rn Rational, ...)
INTO numbers (rn) VALUES (Rational(3, 62)) -- inserts 3/62
n.rn.num INTO my_num FROM numbers n WHERE ... -- returns 3
numbers n SET n.rn = n.rn.reciprocal WHERE ... -- yields 62/3
When you instantiate an object this way, it has no identity outside the database
table. However, the object type exists independently of any table, and can be used
to create objects in other ways.
In the next example, you create a table that stores objects of type Rational in its
rows. Such tables, having rows of objects, are called object tables. Each column in a
row corresponds to an attribute of the object type. Rows can have different column
values.
CREATE TABLE rational_nums OF Rational;
Each row in an object table has an object identifier, which uniquely identifies the
object stored in that row and serves as a reference to the object.
Object Types 9-31