Plsql 简明教程

PL/SQL - Data Types

本章中,我们将讨论 PL/SQL 中的数据类型。PL/SQL 变量、常量和参数必须具有有效的数据类型,该数据类型指定了存储格式、约束和有效范围。本章中,我们将重点介绍 SCALARLOB 数据类型。其他两个数据类型将在其他章节中介绍。

In this chapter, we will discuss the Data Types in PL/SQL. The PL/SQL variables, constants and parameters must have a valid data type, which specifies a storage format, constraints, and a valid range of values. We will focus on the SCALAR and the LOB data types in this chapter. The other two data types will be covered in other chapters.

S.No

Category & Description

1

Scalar Single values with no internal components, such as a NUMBER, DATE, or BOOLEAN.

2

Large Object (LOB) Pointers to large objects that are stored separately from other data items, such as text, graphic images, video clips, and sound waveforms.

3

Composite Data items that have internal components that can be accessed individually. For example, collections and records.

4

Reference Pointers to other data items.

PL/SQL Scalar Data Types and Subtypes

PL/SQL 标量数据类型和子类型属于以下类别 −

PL/SQL Scalar Data Types and Subtypes come under the following categories −

S.No

Date Type & Description

1

Numeric Numeric values on which arithmetic operations are performed.

2

Character Alphanumeric values that represent single characters or strings of characters.

3

Boolean Logical values on which logical operations are performed.

4

Datetime Dates and times.

PL/SQL 提供数据类型的子类型。例如,数据类型 NUMBER 具有名为 INTEGER 的子类型。在将 PL/SQL 代码嵌入到其他程序(例如 Java 程序)时,可以在 PL/SQL 程序中使用子类型来使数据类型与其他程序中的数据类型兼容。

PL/SQL provides subtypes of data types. For example, the data type NUMBER has a subtype called INTEGER. You can use the subtypes in your PL/SQL program to make the data types compatible with data types in other programs while embedding the PL/SQL code in another program, such as a Java program.

PL/SQL Numeric Data Types and Subtypes

下表列出了 PL/SQL 预定义的数值数据类型及其子类型 −

Following table lists out the PL/SQL pre-defined numeric data types and their sub-types −

S.No

Data Type & Description

1

PLS_INTEGER Signed integer in range -2,147,483,648 through 2,147,483,647, represented in 32 bits

2

BINARY_INTEGER Signed integer in range -2,147,483,648 through 2,147,483,647, represented in 32 bits

3

BINARY_FLOAT Single-precision IEEE 754-format floating-point number

4

BINARY_DOUBLE Double-precision IEEE 754-format floating-point number

5

NUMBER(prec, scale) Fixed-point or floating-point number with absolute value in range 1E-130 to (but not including) 1.0E126. A NUMBER variable can also represent 0

6

DEC(prec, scale) ANSI specific fixed-point type with maximum precision of 38 decimal digits

7

DECIMAL(prec, scale) IBM specific fixed-point type with maximum precision of 38 decimal digits

8

NUMERIC(pre, secale) Floating type with maximum precision of 38 decimal digits

9

DOUBLE PRECISION ANSI specific floating-point type with maximum precision of 126 binary digits (approximately 38 decimal digits)

10

FLOAT ANSI and IBM specific floating-point type with maximum precision of 126 binary digits (approximately 38 decimal digits)

11

INT ANSI specific integer type with maximum precision of 38 decimal digits

12

INTEGER ANSI and IBM specific integer type with maximum precision of 38 decimal digits

13

SMALLINT ANSI and IBM specific integer type with maximum precision of 38 decimal digits

14

REAL Floating-point type with maximum precision of 63 binary digits (approximately 18 decimal digits)

以下是一个有效的声明 −

Following is a valid declaration −

DECLARE
   num1 INTEGER;
   num2 REAL;
   num3 DOUBLE PRECISION;
BEGIN
   null;
END;
/

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

PL/SQL procedure successfully completed

PL/SQL Character Data Types and Subtypes

以下是 PL/SQL 预定义字符数据类型及其子类型 −

Following is the detail of PL/SQL pre-defined character data types and their sub-types −

S.No

Data Type & Description

1

CHAR Fixed-length character string with maximum size of 32,767 bytes

2

VARCHAR2 Variable-length character string with maximum size of 32,767 bytes

3

RAW Variable-length binary or byte string with maximum size of 32,767 bytes, not interpreted by PL/SQL

4

NCHAR Fixed-length national character string with maximum size of 32,767 bytes

5

NVARCHAR2 Variable-length national character string with maximum size of 32,767 bytes

6

LONG Variable-length character string with maximum size of 32,760 bytes

7

LONG RAW Variable-length binary or byte string with maximum size of 32,760 bytes, not interpreted by PL/SQL

8

ROWID Physical row identifier, the address of a row in an ordinary table

9

UROWID Universal row identifier (physical, logical, or foreign row identifier)

PL/SQL Boolean Data Types

BOOLEAN 数据类型存储在逻辑运算中使用的逻辑值。逻辑值为布尔值 TRUEFALSE 以及值 NULL

The BOOLEAN data type stores logical values that are used in logical operations. The logical values are the Boolean values TRUE and FALSE and the value NULL.

然而,SQL 没有等同于 BOOLEAN 的数据类型。因此,布尔值不能在 − 中使用

However, SQL has no data type equivalent to BOOLEAN. Therefore, Boolean values cannot be used in −

  1. SQL statements

  2. Built-in SQL functions (such as TO_CHAR)

  3. PL/SQL functions invoked from SQL statements

PL/SQL Datetime and Interval Types

DATE 数据类型用于存储固定长度日期时间,其中包括自午夜以来的时间(以秒为单位)。有效的日期范围是从公元前 4712 年 1 月 1 日到公元 9999 年 12 月 31 日。

The DATE datatype is used to store fixed-length datetimes, which include the time of day in seconds since midnight. Valid dates range from January 1, 4712 BC to December 31, 9999 AD.

默认日期格式由 Oracle 初始化参数 NLS_DATE_FORMAT 设置。例如,默认可能为“DD-MON-YY”,其中包括一个用于表示当月日期的两位数字、月份名称的缩写以及该年的最后两位数字。例如,01-OCT-12。

The default date format is set by the Oracle initialization parameter NLS_DATE_FORMAT. For example, the default might be 'DD-MON-YY', which includes a two-digit number for the day of the month, an abbreviation of the month name, and the last two digits of the year. For example, 01-OCT-12.

每个 DATE 包含世纪、年、月、日、小时、分钟和秒。下表显示了每个字段的有效值 −

Each DATE includes the century, year, month, day, hour, minute, and second. The following table shows the valid values for each field −

Field Name

Valid Datetime Values

Valid Interval Values

YEAR

-4712 to 9999 (excluding year 0)

Any nonzero integer

MONTH

01 to 12

0 to 11

DAY

01 to 31 (limited by the values of MONTH and YEAR, according to the rules of the calendar for the locale)

Any nonzero integer

HOUR

00 to 23

0 to 23

MINUTE

00 to 59

0 to 59

SECOND

00 to 59.9(n), where 9(n) is the precision of time fractional seconds

0 to 59.9(n), where 9(n) is the precision of interval fractional seconds

TIMEZONE_HOUR

-12 to 14 (range accommodates daylight savings time changes)

Not applicable

TIMEZONE_MINUTE

00 to 59

Not applicable

TIMEZONE_REGION

Found in the dynamic performance view V$TIMEZONE_NAMES

Not applicable

TIMEZONE_ABBR

Found in the dynamic performance view V$TIMEZONE_NAMES

Not applicable

PL/SQL Large Object (LOB) Data Types

大型对象 (LOB) 数据类型是指大型数据项,例如文本、图形图像、视频剪辑和声音波形。LOB 数据类型允许高效、随机、分块访问这些数据。以下是预定义的 PL/SQL LOB 数据类型 −

Large Object (LOB) data types refer to large data items such as text, graphic images, video clips, and sound waveforms. LOB data types allow efficient, random, piecewise access to this data. Following are the predefined PL/SQL LOB data types −

Data Type

Description

Size

BFILE

Used to store large binary objects in operating system files outside the database.

System-dependent. Cannot exceed 4 gigabytes (GB).

BLOB

Used to store large binary objects in the database.

8 to 128 terabytes (TB)

CLOB

Used to store large blocks of character data in the database.

8 to 128 TB

NCLOB

Used to store large blocks of NCHAR data in the database.

8 to 128 TB

PL/SQL User-Defined Subtypes

子类型是其他数据类型的子集,后者称为其基础类型。子类型具有与其基础类型相同有效的操作,但其有效值仅为其子集。

A subtype is a subset of another data type, which is called its base type. A subtype has the same valid operations as its base type, but only a subset of its valid values.

PL/SQL 在包 STANDARD 中预定义了几个子类型。例如,PL/SQL 预定义了子类型 CHARACTERINTEGER ,如下所示:

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);

您可以定义和使用您自己的子类型。下述程序说明了如何定义和使用用户定义的子类型:

You can define and use your own subtypes. The following program illustrates defining and using a user-defined subtype −

DECLARE
   SUBTYPE name IS char(20);
   SUBTYPE message IS varchar2(100);
   salutation name;
   greetings message;
BEGIN
   salutation := 'Reader ';
   greetings := 'Welcome to the World of PL/SQL';
   dbms_output.put_line('Hello ' || salutation || greetings);
END;
/

当以上代码在 SQL 提示符下执行时,它会生成以下结果:

When the above code is executed at the SQL prompt, it produces the following result −

Hello Reader Welcome to the World of PL/SQL

PL/SQL procedure successfully completed.

NULLs in PL/SQL

PL/SQL NULL 值表示 missingunknown data ,它们不是整数、字符或任何其他特定数据类型。请注意, NULL 与空数据字符串或空字符值 '\0' 不同。可以赋值 NULL,但它不能与任何内容相等,包括它自身。

PL/SQL NULL values represent missing or unknown data and they are not an integer, a character, or any other specific data type. Note that NULL is not the same as an empty data string or the null character value '\0'. A null can be assigned but it cannot be equated with anything, including itself.