📜  PL / SQL-数据类型

📅  最后修改于: 2020-11-26 05:49:31             🧑  作者: Mango


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

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标量数据类型和子类型

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

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程序中使用子类型,以使数据类型与其他程序中的数据类型兼容,同时将PL / SQL代码嵌入到其他程序(例如Java程序)中。

PL / SQL数值数据类型和子类型

下表列出了PL / SQL预定义数字数据类型及其子类型-

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)

以下是有效的声明-

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

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

PL/SQL procedure successfully completed 

PL / SQL字符数据类型和子类型

以下是PL / SQL预定义字符数据类型及其子类型的详细信息-

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数据类型存储逻辑运算中使用的逻辑值。逻辑值是布尔值TRUEFALSE以及值NULL

但是,SQL没有等效于BOOLEAN的数据类型。因此,布尔值不能用于-

  • SQL语句
  • 内置SQL函数(例如TO_CHAR )
  • 从SQL语句调用的PL / SQL函数

PL / SQL日期时间和间隔类型

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

默认日期格式由Oracle初始化参数NLS_DATE_FORMAT设置。例如,默认值可能是“ DD-MON-YY”,其中包括月份中的两位数字,月份名称的缩写以及年份的后两位。例如01-OCT-12。

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

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大对象(LOB)数据类型

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

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用户定义的子类型

子类型是另一种数据类型的子集,称为其基本类型。子类型与其基本类型具有相同的有效操作,但仅是其有效值的子集。

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

SUBTYPE CHARACTER IS CHAR; 
SUBTYPE INTEGER IS NUMBER(38,0);

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

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提示符下执行时,将产生以下结果-

Hello Reader Welcome to the World of PL/SQL 
 
PL/SQL procedure successfully completed. 

PL / SQL中的NULL

PL / SQL NULL值表示丢失未知的数据,它们不是整数,字符或任何其他特定数据类型。请注意, NULL与空数据字符串或空字符值‘\ 0’不同。可以分配null,但不能将其等同于任何事物,包括自身。