Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
37
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

Defining Object Types

For more information, see "Defining Object Constructors" on page 12-13.

Changing Attributes and Methods of an Existing Object Type (Type Evolution)

You can use the ALTER TYPE statement to add, modify, or drop attributes, and add or drop methods of an existing object type:

CREATE TYPE Person_typ AS OBJECT ( name CHAR(20),

ssn CHAR(12),

address VARCHAR2(100));

CREATE TYPE Person_nt IS TABLE OF Person_typ; CREATE TYPE dept_typ AS OBJECT

( mgr Person_typ, emps Person_nt);

CREATE TABLE dept OF dept_typ;

--Add new attributes to Person_typ and propagate the change

--to Person_nt and dept_typ

ALTER TYPE Person_typ ADD ATTRIBUTE (picture BLOB, dob DATE)

CASCADE NOT INCLUDING TABLE DATA;

CREATE TYPE mytype AS OBJECT (attr1 NUMBER, attr2 NUMBER);

ALTER TYPE mytype ADD ATTRIBUTE (attr3 NUMBER),

DROP ATTRIBUTE attr2,

ADD ATTRIBUTE attr4 NUMBER CASCADE;

When a procedure is compiled, it always uses the current version of any object types it references. Existing procedures on the server that reference an object type are invalidated when the type is altered, and are automatically recompiled the next time the procedure is called. You must manually recompile any procedures on the client side that reference types that are altered.

If you drop a method from a supertype, you might have to make changes to subtypes that override that method. You can find if any subtypes are affected by using the CASCADE option of ALTER TYPE; the statement is rolled back if any subtypes override the method. To successfully drop the method from the supertype, you can:

Drop the method permanently from the subtype first.

Drop the method in the subtype, then add it back later using ALTER TYPE without the OVERRIDING keyword.

For more information about the ALTER TYPE statement, see Oracle Database SQL Reference. For guidelines about using type evolution in your applications, and options for changing other types and data that rely on those types, see Oracle Database Application Developer's Guide - Object-Relational Features.

Defining Object Types

An object type can represent any real-world entity. For example, an object type can represent a student, bank account, computer screen, rational number, or data structure such as a queue, stack, or list. This section gives several complete examples, which teach you a lot about the design of object types and prepare you to start writing your own.

Currently, you cannot define object types in a PL/SQL block, subprogram, or package. You can define them interactively in SQL*Plus using the SQL statement CREATE TYPE.

Using PL/SQL Object Types 12-9

Defining Object Types

Overview of PL/SQL Type Inheritance

PL/SQL supports a single-inheritance model. You can define subtypes of object types. These subtypes contain all the attributes and methods of the parent type (or supertype). The subtypes can also contain additional attributes and additional methods, and can override methods from the supertype.

You can define whether or not subtypes can be derived from a particular type. You can also define types and methods that cannot be instantiated directly, only by declaring subtypes that instantiate them.

Some of the type properties can be changed dynamically with the ALTER TYPE statement. When changes are made to the supertype, either through ALTER TYPE or by redefining the supertype, the subtypes automatically reflect those changes.

You can use the TREAT operator to return only those objects that are of a specified subtype.

The values from the REF and DEREF functions can represent either the declared type of the table or view, or one or more of its subtypes.

See the Oracle Database Application Developer's Guide - Object-Relational Features for more detail on all these object-relational features.

Examples of PL/SQL Type Inheritance

-- Create a supertype from which several subtypes will be derived. CREATE TYPE Person_typ AS OBJECT ( ssn NUMBER, name VARCHAR2(30), address VARCHAR2(100)) NOT FINAL;

--Derive a subtype that has all the attributes of the supertype,

--plus some additional attributes.

CREATE TYPE Student_typ UNDER Person_typ ( deptid NUMBER, major VARCHAR2(30)) NOT FINAL;

--Because Student_typ is declared NOT FINAL, you can derive

--further subtypes from it.

CREATE TYPE PartTimeStudent_typ UNDER Student_typ( numhours NUMBER);

--Derive another subtype. Because it has the default attribute

--FINAL, you cannot use Employee_typ as a supertype and derive

--subtypes from it.

CREATE TYPE Employee_typ UNDER Person_typ( empid NUMBER, mgr VARCHAR2(30));

--Define an object type that can be a supertype. Because the

--member function is FINAL, it cannot be overridden in any

--subtypes.

CREATE TYPE T AS OBJECT (..., MEMBER PROCEDURE Print(), FINAL MEMBER

FUNCTION foo(x NUMBER)...) NOT FINAL;

--We never want to create an object of this supertype. By using

--NOT INSTANTIABLE, we force all objects to use one of the subtypes

--instead, with specific implementations for the member functions. CREATE TYPE Address_typ AS OBJECT(...) NOT INSTANTIABLE NOT FINAL;

--These subtypes can provide their own implementations of

--member functions, such as for validating phone numbers and

--postal codes. Because there is no "generic" way of doing these

--things, only objects of these subtypes can be instantiated. CREATE TYPE USAddress_typ UNDER Address_typ(...);

12-10 PL/SQL User's Guide and Reference

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

Соседние файлы в папке Oracle 10g