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

Overview of PL/SQL Subtypes

Operand 1

Operator

Operand 2

Result Type

 

 

 

 

interval

*

numeric

interval

numeric

*

interval

interval

interval

/

numeric

interval

 

 

 

 

You can also manipulate datetime values using various functions, such as EXTRACT. For a list of such functions, see Table 2–3, " Built-In Functions" on page 2-30.

For further information and examples of datetime arithmetic, see Oracle Database SQL Reference and Oracle Database Application Developer's Guide - Fundamentals.

Avoiding Truncation Problems Using Date and Time Subtypes

The default precisions for some of the date and time types are less than the maximum precision. For example, the default for DAY TO SECOND is DAY(2) TO SECOND(6), while the highest precision is DAY(9) TO SECOND(9). To avoid truncation when assigning variables and passing procedure parameters of these types, you can declare variables and procedure parameters of the following subtypes, which use the maximum values for precision:

TIMESTAMP_UNCONSTRAINED

TIMESTAMP_TZ_UNCONSTRAINED

TIMESTAMP_LTZ_UNCONSTRAINED

YMINTERVAL_UNCONSTRAINED

DSINTERVAL_UNCONSTRAINED

Overview of PL/SQL Subtypes

Each PL/SQL base type specifies a set of values and a set of operations applicable to items of that type. Subtypes specify the same set of operations as their base type, but only a subset of its values. A subtype does not introduce a new type; rather, it places an optional constraint on its base type.

Subtypes can increase reliability, provide compatibility with ANSI/ISO types, and improve readability by indicating the intended use of constants and variables. PL/SQL predefines several subtypes in package STANDARD. For example, PL/SQL predefines the subtypes CHARACTER and INTEGER as follows:

SUBTYPE CHARACTER IS CHAR;

SUBTYPE INTEGER IS NUMBER(38,0); -- allows only whole numbers

The subtype CHARACTER specifies the same set of values as its base type CHAR, so CHARACTER is an unconstrained subtype. But, the subtype INTEGER specifies only a subset of the values of its base type NUMBER, so INTEGER is a constrained subtype.

Defining Subtypes

You can define your own subtypes in the declarative part of any PL/SQL block, subprogram, or package using the syntax

SUBTYPE subtype_name IS base_type[(constraint)] [NOT NULL];

where subtype_name is a type specifier used in subsequent declarations, base_type is any scalar or user-defined PL/SQL datatype, and constraint applies only to base types that can specify precision and scale or a maximum size.

3-16 PL/SQL User's Guide and Reference

Overview of PL/SQL Subtypes

Some examples follow:

DECLARE

 

 

SUBTYPE BirthDate IS DATE NOT NULL;

-- based on DATE type

SUBTYPE Counter IS

NATURAL;

-- based on NATURAL subtype

TYPE NameList IS TABLE OF VARCHAR2(10);

SUBTYPE DutyRoster

IS NameList;

-- based on TABLE type

TYPE TimeRec IS RECORD (minutes INTEGER, hours INTEGER);

SUBTYPE FinishTime

IS TimeRec;

-- based on RECORD type

SUBTYPE ID_Num IS emp.empno%TYPE;

-- based on column type

You can use %TYPE or %ROWTYPE to specify the base type. When %TYPE provides the datatype of a database column, the subtype inherits the size constraint (if any) of the column. The subtype does not inherit other kinds of constraints such as NOT NULL.

Using Subtypes

Once you define a subtype, you can declare items of that type. In the example below, you declare a variable of type Counter. Notice how the subtype name indicates the intended use of the variable.

DECLARE

SUBTYPE Counter IS NATURAL; rows Counter;

You can constrain a user-defined subtype when declaring variables of that type:

DECLARE

SUBTYPE Accumulator IS NUMBER; total Accumulator(7,2);

Subtypes can increase reliability by detecting out-of-range values. In the example below, you restrict the subtype Numeral to storing integers in the range -9 .. 9. If your program tries to store a number outside that range in a Numeral variable, PL/SQL raises an exception.

DECLARE

SUBTYPE Numeral IS NUMBER(1,0);

x_axis Numeral; -- magnitude range is -9 .. 9 y_axis Numeral;

BEGIN

x_axis := 10; -- raises VALUE_ERROR

...

END;

Type Compatibility

An unconstrained subtype is interchangeable with its base type. For example, given the following declarations, the value of amount can be assigned to total without conversion:

DECLARE

SUBTYPE Accumulator IS NUMBER; amount NUMBER(7,2);

total Accumulator; BEGIN

...

total := amount;

...

END;

Different subtypes are interchangeable if they have the same base type:

PL/SQL Datatypes 3-17

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

Мы не исправляем ошибки в тексте (почему?), но будем благодарны, если вы все же напишите об ошибках.

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