OBJECT TYPES
An object type differs from native SQL datatypes in that it is user-defined, and it specifies both the underlying persistent data (attributes) and the related behaviors (methods). Object types are abstractions of the real-world entities, for example, purchase orders. Object types store structured business data in its natural form and allow applications to retrieve it that way.
Object types and related object-oriented features, such as variable-length arrays and nested tables, provide higher-level ways to organize and access data in the database. Underneath the object layer, data is still stored in columns and tables, but you can work with the data in terms of the real-world entities--customers and purchase orders, for example--that make the data meaningful. Instead of thinking in terms of columns and tables when you query the database, you can simply select a customer.
Object types are abstractions of the real-world entities—for example, purchase orders—that application programs deal with. An object type is a schema object with three kinds of components:
• A name, which serves to identify the object type uniquely within that schema
• Attributes, which model the structure and state of the real-world entity. Attributes are built-in types or other user-defined types.
• Methods, which are functions or procedures written in PL/SQL or Java and stored in the database, or written in a language such as C and stored externally. Methods implement operations the application can perform on the real-world entity.
An object type is a template. A structured data unit that matches the template is called an object. An example object types and object instances:
Object Attributes and Methods
Attributes hold the data about an object's features of interest. For example, a student object type might have name, major, and graduation date attributes. An attribute has a declared datatype which can in turn be another object type. Taken together, the attributes of an object instance contain that object's data.
Methods are procedures or functions provided to enable applications to perform useful operations on the attributes of the object type. Methods are an optional element of an object type. They define the behavior of objects of that type and determine what (if anything) that type of object can do.
Types of Methods
Methods of an object type model the behavior of objects. The methods of an object type broadly fall into these categories:
• A Member method is a function or a procedure that always has an implicit SELF parameter as its first parameter, whose type is the containing object type.
• A Static method is a function or a procedure that does not have an implicit SELF parameter. Such methods can be invoked by qualifying the method with the type name, as in TYPE_NAME.METHOD. Static methods are useful for specifying user-defined constructors or cast methods.
• Comparison methods are used for comparing instances of objects.
Oracle supports the choice of implementing type methods in PL/SQL, Java, and C. Every object type also has one implicitly defined method that is not tied to specific objects, the object type's constructor method.
Object Type Constructor Methods
Every object type has a system-defined constructor method; that is, a method that makes a new object according to the object type's specification. The name of the constructor method is the name of the object type. Its parameters have the names and types of the object type's attributes. The constructor method is a function. It returns the new object as its value. You can also define your own constructor functions to use in place of the constructor functions that the system implicitly defines for every object type.
Comparison Methods
Methods play a role in comparing objects. Oracle has facilities for comparing two data items of a given built-in type (for example, two numbers), and determining whether one is greater than, equal to, or less than the other. Oracle cannot, however, compare two items of an arbitrary user-defined type without further guidance from the definer. Oracle provides two ways to define an order relationship among objects of a given object type: map methods and order methods.
Map methods use Oracle's ability to compare built-in types. Suppose that you have defined an object type called rectangle, with attributes height and width. You can define a map method area that returns a number, namely the product of the rectangle's height and width attributes. Oracle can then compare two rectangles by comparing their areas.
Order methods are more general. An order method uses its own internal logic to compare two objects of a given object type. It returns a value that encodes the order relationship. For example, it could return -1 if the first is smaller, 0 if they are equal, and 1 if the first is larger.
COLLECTION TYPES
Each collection type describes a data unit made up of an indefinite number of elements, all of the same datatype.
For modeling multi-valued attributes and many to many relationships, Oracle supports two collection datatypes: varrays and nested tables. Collection types can be used anywhere other datatypes can be used. You can have object attributes of a collection type in addition to columns of a collection type. For example, you might give a purchase order object type a nested table attribute to hold the collection of line items for a given purchase order. You use the CREATE TYPE statement to define collection types.
VARRAYs
An array is an ordered set of data elements. All elements of a given array are of the same datatype. Each element has an index, which is a number corresponding to the element's position in the array.
The number of elements in an array is the size of the array. Oracle allows arrays to be of variable size, which is why they are called VARRAYs. You must specify a maximum size when you declare the array type.
Creating an array type does not allocate space. It defines a datatype, which you can use as:
• The datatype of a column of a relational table
• An object type attribute
• A PL/SQL variable, parameter, or function return type.
A VARRAY is normally stored in line; that is, in the same tablespace as the other data in its row. If it is sufficiently large, however, Oracle stores it as a BLOB.
Nested Tables
A nested table is an unordered set of data elements, all of the same datatype. It has a single column, and the type of that column is a built-in type or an object type. If an object type, the table can also be viewed as a multicolumn table, with a column for each attribute of the object type. If compatibility is set to Oracle9i or higher, nested tables can contain other nested tables.
A table type definition does not allocate space. It defines a type, which you can use as:
• The datatype of a column of a relational table
• An object type attribute
• A PL/SQL variable, parameter, or function return type
When a table type appears as the type of a column in a relational table or as an attribute of the underlying object type of an object table, Oracle stores all of the nested table data in a single table, which it associates with the enclosing relational or object table.
A convenient way to access the elements of a nested table individually is to use a nested cursor.
Increasing the Size and Precision of VARRAYs and Nested Tables
When the element type of a VARRAY type is a variable character or RAW type or a numeric type, you can increase the size of the variable character or RAW type or increase the precision of the numeric type. A new type version is generated for the VARRAY type. The same changes can be applied to nested table types.
Options like INVALIDATE and CASCADE are provided to either invalidate all dependent objects or propagate the change to its type and table dependents.
Increasing VARRAY Limit Size
The ALTER TYPE ... MODIFY LIMIT syntax allows increasing the number of elements of a VARRAY type. If the number of elements of the VARRAY type is increased, a new type version is generated for the VARRAY type and this is maintained as part of the history of the type changes.
Options like INVALIDATE and CASCADE are provided to either invalidate all dependent objects or propagate the change to its type and table dependents.
Overview of Type Inheritance
An object type can be created as a subtype of an existing object type. A single inheritance model is supported: the subtype can be derived from only one parent type. A type inherits all the attributes and methods of its direct supertype. It can add new attributes and methods, and it can override any of the inherited methods.
Furthermore, a subtype can itself be refined by defining another subtype under it, thus building up type hierarchies.
Example picture illustrates two subtypes, Student_t and Employee_t, created under Person_t.
|