Você está na página 1de 8

About Object Types

An object type is a kind of data type. You can use it in the same ways that you use standard data types
such as NUMBER or VARCHAR2. For example, you can specify an object type as the data type of a column in a
relational table, and you can declare variables of an object type. The value is a variable or an instance of
that type. An object instance is also called an object.
Figure ! shows an object type, person_typ, and two instances of the object type.
Figure 1-1 An Object Type and Object Instances
"escription of #Figure ! An $bject Type and $bject %nstances#
$bject types serve as blueprints or templates that de&ne both structure and behavior. $bject types are
database schema objects, subject to the same kinds of administrative control as other schema objects.
Application code can retrieve and manipulate these objects. 'ee (hapter ), #*anaging $racle $bjects#.
You use the CREATE TYPE '+, statement to de&ne object types.
-xample ! shows how to create an object type named person_typ. %n the example, an object
speci&cation and object body are de&ned. For information on the CREATE TYPE '+, statement and on the
CREATE TYPE BODY '+, statement, see Oracle Database PL/SQL Language Reference.
Note:
Running Examples: *any examples in this guide can be run using the HR sample schema. (omments at
the beginning of most examples indicate if any previous example code is re.uired.
/efer to Oracle Database Sample Schemas for information on how these schemas were created and how
you can use them yourself
Example 1-1 Creating the person_typ Object Type
CREATE TYPE person_typ AS OBJECT (
idno NUMBER,
first_n!e VARCHAR2(2"#,
$st_n!e VARCHAR2(2%#,
e!i$ VARCHAR2(2%#,
p&one VARCHAR2(2"#,
MAP MEMBER 'UNCT(ON )et_idno RETURN NUMBER,
MEMBER PROCEDURE disp$y_deti$s ( *E+' (N OUT NOCOPY person_typ ##,
-
CREATE TYPE BODY person_typ A*
MAP MEMBER 'UNCT(ON )et_idno RETURN NUMBER (*
BE.(N
RETURN idno,
END,
MEMBER PROCEDURE disp$y_deti$s ( *E+' (N OUT NOCOPY person_typ # (*
BE.(N
// 0se t&e PUT_+(NE pro1ed0re of t&e DBM*_OUTPUT p12)e to disp$y deti$s
DBM*_OUTPUT3PUT_+(NE(TO_CHAR(idno# 44 5 5 44 first_n!e 44 5 5 44 $st_n!e#,
DBM*_OUTPUT3PUT_+(NE(e!i$ 44 5 5 44 p&one#,
END,
END,
-
$bject types di0er from the standard data types that are native to a relational database1
$racle "atabase does not supply prede&ned object types. You de&ne the object types you want by
combining built!in types with user!de&ned ones as shown in -xample !.
$bject types are composed of attributes and methods as illustrated in Figure !2.
o Attributes hold the data about an object. Attributes have declared data types which can, in
turn, be other object types.
o *ethods are procedures or functions that applications can use to perform operations on the
attributes of the object type. *ethods are optional. They de&ne the behavior of objects of
that type.
Figure 1- Object Attributes and !ethods
"escription of #Figure !2 $bject Attributes and *ethods#
About Object Instances
A variable of an object type is an instance of the type, or an object. An object has the attributes and
methods de&ned for its type. 3ecause an object instance is a concrete thing, you can assign values to its
attributes and call its methods.
"e&ning an object type does not allocate any storage. After they are de&ned, object types can be used in
'+, statements in most of the same places you use types such as NUMBER or VARCHAR2. 'torage is
allocated once you create an instance of the object type.
-xample !2 shows how to create object instances of the person_typ created in -xample !, and de&ne a
relational table to keep track of these instances as contacts.
Example 1- Creating the contacts Table "ith an Object Type Column
// re60ires e7istin) person_typ fr3 E7 8/8
CREATE TAB+E 1ont1ts (
contact person_typ,
1ont1t_dte DATE #,
(N*ERT (NTO contacts VA+UE* (
person_typ (9%, 5Vern5, 5Mi$$s5, 5:!i$$s;e7!p$e31o!5, 58/9%"/%%%/"82%5#,
52< =0n 2"">5 #,
The 1ont1ts table is a relational table with an object type as the data type of its contact column. $bjects
that occupy columns of relational tables are called column objects. 'ee #4ow $bjects are 'tored in Tables#.
About Object Methods
$bject methods are functions or procedures that you can declare in an object type de&nition to implement
behavior that you want objects of that type to perform.
The general kinds of methods that can be declared in a type de&nition are1
*ember *ethods
5sing member methods, you can provide access to the data of an object, and otherwise de&ne
operations that an application performs on the data. To perform an operation, the application calls
the appropriate method on the appropriate object.
'tatic *ethods
'tatic methods compare object instances and perform operations that do not use the data of any
particular object, but, instead, are global to an object type.
(onstructor *ethods
A default constructor method is implicitly de&ned for every object type, unless it is overwritten with
a user!de&ned constructor. A constructor method is called on a type to construct or create an object
instance of the type.
-xample !6 show the )et_idno(# method, created in -xample !, to display the %d number of persons in
the 1ont1ts table1
Example 1-# $sing the get_idno Object !ethod
// re60ires E7 8/8 nd E7 8/2
*E+ECT 131ont1t3)et_idno(# 'ROM 1ont1ts 1,
See Also:
#$bject *ethods# for detailed information
Ho Objects a!e Sto!ed in Tables
$bjects can be stored in two types of tables1
$bject tables1 store only objects
%n an object table, each row represents an object, which is referred to as a row object. 'ee #(reating
and 5sing $bject Tables#
/elational tables1 store objects with other table data
$bjects that are stored as columns of a relational table, or are attributes of other objects, are called
column objects. -xample !2 shows the 1ont1ts table which stores an instance of the person_typ
object.
$bjects that have meaning outside of the relational database in which they are contained, or objects that
are shared among more than one relational database object, should be made referenceable as row objects.
That is, such objects should be stored as a row object in an object table instead of in a column of a
relational table.
See Also:
#'toring $bjects as (olumns or /ows#
"!eating and #sing Object Tables
-xample !7 shows a CREATE TAB+E statement that creates an object table for person_typ objects.
Example 1-% Creating the person_obj_table Object Table
// re60ires E73 8/8
CREATE TAB+E person_o?@_t?$e O' person_typ,
You can view this table in two ways1
As a single!column table, in which each row is a person_typ object, allowing you to perform object!
oriented operations.
As a multi!column table, in which each attribute of the object type person_typ such as idno,
first_n!e, $st_n!e, and so on, occupies a column, allowing you to perform relational
operations.
-xample !8 illustrates several operations on an object table.
Example 1-& Operations on the person_obj_table Object Table
// re60ires E73 8/8 nd 8/<
(N*ERT (NTO person_o?@_t?$e VA+UE* (
person_typ(8"8, 5=o&n5, 5*!it&5, 5@s!it&;e7!p$e31o!5, 58/9%"/%%%/"8>%5# #,
*E+ECT VA+UE(p# 'ROM person_o?@_t?$e p
AHERE p3$st_n!e B 5*!it&5,
DEC+ARE
person person_typ,
BE.(N // P+-*C+ ?$o12 for se$e1tin) person nd disp$yin) deti$s
*E+ECT VA+UE(p# (NTO person 'ROM person_o?@_t?$e p AHERE p3idno B 8"8,
person3disp$y_deti$s(#,
END,
-
The (N*ERT (NTO '+, statement in -xample !8 inserts a person_typ object into person_o?@_t?$e,
treating person_o?@_t?$e as a multi!column table.
The *E+ECT '+, statement selects from person_o?@_t?$e as a single!column table, using the VA+UE
function to return rows as object instances. 'ee #9A,5-# for information on the VA+UE function.
The :,;'+, block selects a speci&c person and executes a member function of person_typ to display
details about the speci&ed person. For more information about using :,;'+, with objects, see (hapter 6,
#5sing :,;'+, <ith $bject Types#.
#sing Object Identi$e!s to Identi%y Ro Objects
$bject identi&ers =$%"s> uni.uely identify row objects in object tables. You cannot directly access object
identi&ers, but you can make references =RE's> to the object identi&ers and directly access the RE's, as
discussed in #5sing /eferences to /ow $bjects#.
There are two types of object identi&ers.
'ystem!?enerated $bject %denti&ers =default>
$racle automatically creates system!generated object identi&ers for row objects in object tables
unless you choose the primary!key based option.
:rimary!@ey 3ased $bject %denti&ers
You have the option to create primary!key based $%"s when you create the table using the CREATE
TAB+E statement.
Note:
(olumn objects are identi&ed by the primary key of the row, and, therefore, do not need a speci&c object
identi&er.
See Also:
#'pecifying $bject %denti&ers for $bject 9iews#
#'torage (onsiderations for $bject %denti&ers =$%"s>#
#$bject %denti&ers and /eferences#
#sing Re%e!ences to Ro Objects
A RE' is a logical pointer or reference to a row object that you can construct from an object identi&er =$%">.
You can use the RE' to obtain, examine, or update the object. You can change a RE' so that it points to a
di0erent object of the same object type hierarchy or assign it a null value.
RE's are $racle "atabase built!in data types. RE's and collections of RE's model associations among
objects, particularly many!to!one relationships, thus reducing the need for foreign keys. RE's provide an
easy mechanism for navigating between objects.
-xample !A illustrates a simple use of a RE'.
Example 1-' $sing a (EF to the emp_person_typ Object
CREATE TYPE e!p_person_typ A* OB=ECT (
n!e VARCHAR2(>"#,
!n)er REF emp_person_typ #,
-
CREATE TAB+E e!p_person_o?@_t?$e O' e!p_person_typ,
(N*ERT (NTO e!p_person_o?@_t?$e VA+UE* (
e!p_person_typ (5=o&n *!it&5, NU++##,

(N*ERT (NTO e!p_person_o?@_t?$e
*E+ECT e!p_person_typ (5Bo? =ones5, RE'(e##
'ROM e!p_person_o?@_t?$e e
AHERE e3n!e B 5=o&n *!it&5,
This example &rst creates the e!p_person_typ Bohn 'mith, with NU++ value for a manager. Then it adds
the e!p_person_typ 3ob Bones as Bohn 'mithCs supervisee.
The following .uery and its output show the e0ect1
CO+UMN n!e 'ORMAT A8"
CO+UMN !n)er 'ORMAT A%"
se$e1t D fro! e!p_person_o?@_t?$e e,
NAME MANA.ER
////////// //////////////////////////////////////////////////
=o&n *!it&
Bo? =ones """"22"2"E<2<EE"8"9FC2EABBE"<"%FECEF"A"F"F<2<EE"8"
9FC8EABBE"<"%FECEF"A"F"F
-xample !D shows how to dereference the object, so that Mn)er appears as a name rather than an
object identi&er.
'ee #/ules for /-F (olumns and Attributes# and #"esign (onsiderations for /-Fs#.
#sing Scoped RE&s
You can constrain a column type, collection element, or object type attribute to reference a speci&ed object
table by using the '+, constraint subclause *COPE (* when you declare the RE'. 'coped RE' types re.uire
less storage space and allow more eEcient access than unscoped RE' types.
-xample !) shows RE' column 1ont1t_ref scoped to person_o?@_t?$e which is an object table of type
person_typ.
Example 1-) Creating the contacts_re* Table $sing a +coped (EF
// re60ires E73 8/8, 8/<, nd 8/%
CREATE TAB+E 1ont1ts_ref (
1ont1t_ref RE' person_typ SCOPE IS person_o?@_t?$e,
1ont1t_dte DATE #,
To insert a row in the table, you could issue the following1
(N*ERT (NTO 1ont1ts_ref
*E+ECT RE'(p#, 529 =0n 2"">5
'ROM person_o?@_t?$e p
AHERE p3idno B 8"8,
A RE' can be scoped to an object table of the declared type =person_typ in the example> or of any subtype
of the declared type. %f a RE' is scoped to an object table of a subtype, the RE' column is e0ectively
constrained to hold only references to instances of the subtype =and its subtypes, if any> in the table. 'ee
#%nheritance in '+, $bject Types#.
"hec'ing %o! (angling RE&s
%t is possible for the object identi&ed by a RE' to become unavailable if the object has been deleted or
some necessary privilege has been deleted. This is a dangling RE'. You can use the $racle "atabase '+,
predicate (* DAN.+(N. to test RE's for this condition.
"angling RE's can be avoided by de&ning referential integrity constraints. 'ee #/ules for /-F (olumns and
Attributes#.
(e!e%e!encing RE&s
Accessing the object that the RE' refers to is called dereferencing the RE'. $racle "atabase provides the
DERE' operator to do this.
Example 1-, $sing -E(EF to -ere*erence a (EF
// re60ires E73 8/9
*E+ECT DERE'(e3!n)er# 'ROM e!p_person_o?@_t?$e e,
DERE'(E3MANA.ER#(NAME, MANA.ER#
////////////////////////////////////////////////////////////////
EMP_PER*ON_TYP(5=o&n *!it&5, NU++#
-xample !F shows that dereferencing a dangling RE' returns a null object.
Example 1-. -ere*erencing a -angling (e*
//re60ires E73 8/8, 8/<, 8/%, nd 8/F
// DE+ETE 1o!!nd needed to 10se dn)$in) refDE+ETE fro! person_o?@_t?$e AHERE idno B
8"8,-
*E+ECT DERE'(131ont1t_ref#, 131ont1t_dte 'ROM 1ont1ts_ref 1,
$racle "atabase also provides implicit dereferencing of RE's. For example, to access the managerCs name
for an employee, you can use a *E+ECT statement.
-xample !D follows the pointer from the personCs name and retrieves the managerCs name
e3!n)er3n!e.
Example 1-1/ Implicitly -e*erencing a (EF
// re60ires E73 8/9
*E+ECT e3n!e, e3!n)er3n!e 'ROM e!p_person_o?@_t?$e e
AHERE e3n!e B 5Bo? =ones5,
Following the RE' in this manner is allowed in '+,, but :,;'+, re.uires the DERE' keyword as in -xample
!G.
Obtaining a RE& to a Ro Object
You can obtain a RE' to a row object by selecting the object from its object table and applying the RE'
operator.
-xample ! shows how to obtain a RE' to the person with an idno e.ual to 8"8.
Example 1-11 Obtaining a (EF to a (o" Object
// re60ires E73 8/8, 8/<, nd 8/%
DEC+ARE
person_ref RE' person_typ,
person person_typ,
BE.(N

*E+ECT RE'(p# (NTO person_ref
'ROM person_o?@_t?$e p
AHERE p3idno B 8"8,

se$e1t deref(person_ref# into person fro! d0$,
person3disp$y_deti$s(#,

END,
-
The .uery returns exactly one row. 'ee #'torage 'iHe of /-Fs#.
"ompa!ing RE& )a!iables
Two RE' variables can be compared if, and only if, the targets that they reference are both of the same
declared type, or one is a subtype of the other.
#sing O!acle "ollections
For modeling multi!valued attributes and many!to!many relationships, $racle "atabase supports two
collection data types1 varrays and nested tables. You can use collection types anywhere other data types
are used. You can have object attributes of a collection type in addition to columns of a collection type. For
example, a purchase order object type might contain a nested table attribute that holds the collection of
line items for the purchase order.
To de&ne a collection type, use the CREATE TYPE 3 3 3 A* TAB+E O' statement.
-xample !2 shows CREATE TYPE statements that de&ne a collection and an object type.
Example 1-1 Creating the people_typ Collection -ata Type
// re60ires E73 8/8
CREATE TYPE peop$e_typ AS TABLE OF person_typ,
-
CREATE TYPE dept_persons_typ A* OB=ECT (
dept_no CHAR(%#,
dept_n!e CHAR(2"#,
dept_!)r person_typ,
dept_e!ps peop$e_typ#,
-
Note the %olloing about this example:
The collection type, peop$e_typ, is speci&cally a nested table type.
The dept_persons_typ object type has an attribute dept3e!ps of peop$e_typ. -ach row in the
dept3e!ps nested table is an object of type person_typ which was de&ned in -xample !.

Você também pode gostar