Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
2
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

9.1.2 The pls_integer Type

The PLS_INTEGER datatype is available in PL/SQL Release 2.3 and above, and stores signed integers in the range -2,147,483,647 through 2,147,483,647. Values are stored using your hardware platform's native integer format.

Following is an example of some PLS_INTEGER declarations:

DECLARE

loop_counter PLS_INTEGER;

days_in_standard_year CONSTANT PLS_INTEGER := 365;

emp_vacation_days PLS_INTEGER DEFAULT 14;

The PLS_INTEGER datatype was designed for speed. When you perform arithmetic using PLS_INTEGER values, the Oracle software uses native machine arithmetic, whereas arithmetic with other numeric types requires Oracle's platform-independent arithmetic library. As a result, it's faster to manipulate PLS_INTEGER values than it is to manipulate NUMBER values. Because PLS_INTEGER values are integers, you generally won't run into any compatibility issues as you move from one hardware platform to the next.

For efficiency reasons, Oracle recommends that you use PLS_INTEGER for all integer calculations that do not fall outside its range. Bear in mind, however, that if your use of PLS_INTEGER results in frequent conversions to and from the NUMBER type, you may be better off using NUMBER to begin with. You'll gain the greatest efficiency when you use PLS_INTEGER for integer arithmetic (and for loop counters) in cases where you can avoid conversions back and forth to the NUMBER type.

9.1.3 The binary_integer Type

The BINARY_INTEGER datatype also allows you to store signed integers in a binary format. Unlike with PLS_INTEGER, native machine arithmetic is not used; BINARY_INTEGER arithmetic uses Oracle's platform-independent library code. BINARY_INTEGER was the first of PL/SQL's binary, integer datatypes. However, Oracle now recommends that you use PLS_INTEGER instead. Use BINARY_INTEGER only if you need to maintain compatibility with code written prior to PL/SQL Release 2.3.

The range of magnitude of a BINARY_INTEGER is -2,147,483,647 through 2,147,483,647. If you will be performing intensive calculations with integer values, you might see a performance improvement by declaring your variables as BINARY_INTEGER rather than NUMBER. But to be honest, in most situations this slight savings will not be noticeable.

9.1.4 Numeric Subtypes

Oracle also provides a number of numeric subtypes. Most of the time, these subtypes are simply alternate names for the three basic types we've just discussed. These alternate names offer compatibility with ANSI SQL, SQL/DS, and DB2 datatypes, and usually have the same range of legal values as their base type. Sometimes, subtypes offer additional functionality by restricting values to a subset of those supported by their base type. These subtypes are described in Table 9-4.

Table 9-4. Predefined numeric subtypes

Subtype

Compatibility

Corresponding Oracle datatype/notes

DEC (precision, scale)

ANSI

NUMBER (precision, scale)

DECIMAL (precision, scale)

IBM

NUMBER (precision, scale)

DOUBLE PRECISION

ANSI

NUMBER

FLOAT

ANSI, IBM

NUMBER

FLOAT (binary_precision)

ANSI, IBM

NUMBER, but NUMBERs can't be declared using binary precision

INT

ANSI

NUMBER

INTEGER

ANSI, IBM

NUMBER

NATURAL

N/A

BINARY_INTEGER, but allows only non-negative values (0 and higher)

NATURALN

N/A

Same as NATURAL, but with the additional restriction of never being NULL

NUMERIC (precision, scale)

ANSI

NUMBER (precision, scale)

POSITIVE

N/A

BINARY_INTEGER, but allows only positive values (1 and higher)

POSITIVEN

N/A

Same as POSITIVE, but with the additional restriction of never being NULL

REAL

ANSI

NUMBER

SIGNTYPE

N/A

BINARY_INTEGER, limited to the values -1, 0, and 1

SMALLINT

ANSI, IBM

NUMBER (38)

The NUMERIC, DECIMAL, and DEC datatypes can declare only fixed-point numbers. DOUBLE PRECISION and REAL are equivalent to NUMBER. FLOAT allows floating decimal points with binary precisions that range from 63 to 126 bits. We don't find it all that useful to define a number's precision in terms of bits rather than digits. We also don't find much use for the ANSI/IBM compatible subtypes, and we don't believe you will either.

The subtypes that we do sometimes find useful are the BINARY_INTEGER subtypes. NATURAL and POSITIVE are both subtypes of BINARY_INTEGER. These subtypes constrain the values you can store in a variable, and their use can make a program more self-documenting. For example, if you have a variable whose values must always be non-negative, you could declare that variable to be NATURAL (0 and higher) or POSITIVE (1 and higher), improving the self-documenting aspect of your code. Bear in mind, though, that you may get better performance out of PLS_INTEGER than out of BINARY_INTEGER and its subtypes.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]