about object types

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# .  Y ou use the CREATE TYPE '+ statement to de&ne object types. -xample ! shows how to creat e 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 reuired. /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"#,

Upload: estherrosetta

Post on 12-Oct-2015

1 views

Category:

Documents


0 download

DESCRIPTION

Oracle Objects

TRANSCRIPT

  • 5/21/2018 About Object Types

    1/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 typessuch as NUMBERor VARCHAR2. For example, you can specify an object type as the data type of a column i

    relational table, and you can declare variables of an object type. The value is a variable or an instance othat 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 CREATETYPE'+ 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 CREATETYPE'+ statement and on the

    CREATETYPEBODY'+ statement, see Oracle Database PL/SQL Language Reference.

    Note:Running Examples:*any examples in this guide can be run using the HRsample schema. (omments a

    the beginning of most examples indicate if any previous example code is reuired.

    /efer to Oracle Database Sample Schemasfor information on how these schemas were created and howyou can use them yourself

    Example 1-1 Creating the person_typ Object Type

    CREATETYPEperson_typASOBJECT( idno NUMBER, first_n!e VARCHAR2(2"#, $st_n!e VARCHAR2(2%#, e!i$ VARCHAR2(2%#, p&one VARCHAR2(2"#,

    http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CIHCDEGEhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/img_text/adobj036.htmhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjmng.htm#g1012556http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDFBGIIhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28370/create_type.htm#LNPLS01375http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28370/create_type.htm#LNPLS01375http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/server.111/b28328/toc.htmhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/img_text/adobj036.htmhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjmng.htm#g1012556http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDFBGIIhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28370/create_type.htm#LNPLS01375http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/server.111/b28328/toc.htmhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CIHCDEGE
  • 5/21/2018 About Object Types

    2/8

    MAP MEMBER 'UNCTON )et_idno RETURN NUMBER,MEMBER PROCEDURE disp$y_deti$s ( *E+' N OUT NOCOPY person_typ ##

    -

    CREATE TYPE BODY person_typ A* MAP MEMBER 'UNCTON )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 p1)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# ENDEND-

    $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 b

    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, i

    turn, be other object types.

    o *ethods are procedures or functions that applications can use to perform operations on th

    attributes of the object type. *ethods are optional. They de&ne the behavior of objects ofthat 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 itattributes and call its methods.

    "e&ning an object type does not allocate any storage. After they are de&ned, object types can be used i'+ statements in most of the same places you use types such as NUMBERor VARCHAR2. 'torage is

    allocated once you create an instance of the object type.

    -xample !2shows how to create object instances of the person_typcreated in -xample !, and de&n

    relational table to keep track of these instances as contacts.

    Example 1- Creating the contacts Table "ith an Object Type Column

    http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDFBGIIhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDFBGIIhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CIHBHACJhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/img_text/adobj035.htmhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CIHCBDHGhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDFBGIIhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDFBGIIhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CIHBHACJhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/img_text/adobj035.htmhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CIHCBDHGhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDFBGII
  • 5/21/2018 About Object Types

    3/8

    // re60ires e7istin) person_typ fr3 E7 8/8CREATE TAB+E 1ont1ts ( contact person_typ, 1ont1t_dte DATE #

    N*ERT NTO contactsVA+UE* ( person_typ (9%, 5Vern5, 5Mi$$s5, 5:!i$$s;e7!p$e31o!5, 58/9%"/%%%/"82%5#,52< =0n 2"">5 #

    The 1ont1tstable is a relational table with an object type as the data type of its contact column. $bjecthat occupy columns of relational tables are called column objects. 'ee #4ow $bjects are 'tored in Table

    About Object Methods

    $bject methods are functions or procedures that you can declare in an object type de&nition to implemebehavior 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&neoperations that an application performs on the data. To perform an operation, the application callthe appropriate method on the appropriate object.

    'tatic *ethods

    'tatic methods compare object instances and perform operations that do not use the data of anyparticular 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 w

    a user!de&ned constructor. A constructor method is called on a type to construct or create an objinstance of the type.

    -xample !6show the )et_idno(#method, created in -xample !,to display the %d number of persons

    the 1ont1tstable1

    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 #(reatand 5sing $bject Tables#

    /elational tables1 store objects with other table data

    http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/glossary.htm#CHDGJBJHhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#i458240http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CIHFJFDJhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDFBGIIhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDFBGIIhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjbas.htm#i463694http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/glossary.htm#CHDCGBABhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDHIEFHhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDHIEFHhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/glossary.htm#CHDGJBJHhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#i458240http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CIHFJFDJhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDFBGIIhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjbas.htm#i463694http://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/glossary.htm#CHDCGBABhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDHIEFHhttp://www.filibeto.org/sun/lib/nonsun/oracle/11.1.0.6.0/B28359_01/appdev.111/b28371/adobjint.htm#CHDHIEFH
  • 5/21/2018 About Object Types

    4/8

    $bjects that are stored as columns of a relational table, or are attributes of other objects, are callcolumn objects. -xample !2shows the 1ont1tstable which stores an instance of the person_tobject.

    $bjects that have meaning outside of the relational database in which they are contained, or objects thaare shared among more than one relational database object, should be made referenceable as row objec

    That is, such objects should be stored as a row object in an object table instead of in a column of arelational table.

    See Also:#'toring $bjects as (olumns or /ows#

    "!eating and #sing Object Tables

    -xample !7shows a CREATETAB+Estatement that creates an object table for person_typobjects.

    Example 1-% Creating the person_obj_table Object Table

    // re60ires E73 8/8CREATE 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_typobject, allowing you to perform obje

    oriented operations.

    As a multi!column table, in which each attribute of the object type person_typsuch as idno,

    first_n!e, $st_n!e, and so on, occupies a column, allowing you to perform relational

    operations.

    -xample !8illustrates several operations on an object table.

    Example 1-& Operations on the person_obj_table Object Table

    // re60ires E73 8/8 nd 8/