|
001 |
This chapter discusses the Oracle built-in datatypes, their properties,
and how they map to non-Oracle datatypes.
|
本章讨论 Oracle 内置的数据类型及其特性,以及内置数据类型与非 Oracle 数据类型间的映射关系。
|
|
002 |
This chapter contains the following topics:
|
本章包含以下主题:
|
|
003 |
Introduction to
Oracle Datatypes
|
26.1 Oracle 数据类型简介
|
|
004 |
Each column value and constant in a SQL statement has a datatype,
which is associated with a specific storage format, constraints, and a
valid range of values. When you create a table, you must specify a
datatype for each of its columns.
|
SQL 语句所引用的每个列值[column value]及常量[constant]都属于某个数据类型[datatype],数据类型决定了数据的存储格式,约束,及有效值范围。当用户创建数据表时,必须为每列设定数据类型。
|
|
005 |
Oracle provides the following categories of built-in datatypes:
|
Oracle 提供了以下内置数据类型:
|
|
006 |
Note:
PL/SQL has additional datatypes for constants and variables, which
include BOOLEAN, reference types,
composite types (collections and records), and user-defined
subtypes.
|
提示:
在
PL/SQL 中提供了额外的数据类型供常量及变量使用,这些类型包括 BOOLEAN,引用类型[reference type],复合类型[composite type](集合[collection]与记录[record]),及用户定义子类型[user-defined
subtype]。
|
|
007 |
See Also:
|
另见:
|
|
008 |
The following sections that describe each of the built-in datatypes in
more detail.
|
以下各节将分别详细描述每种内置数据类型。
|
|
009 |
Overview of Character Datatypes
|
26.2 字符数据类型概述
|
|
010 |
The character datatypes store character (alphanumeric) data in strings,
with byte values corresponding to the character encoding scheme,
generally called a character set or code page.
|
字符数据类型[character
datatype]用于存储字符串[string]中的字符(包括文字及数字)数据,其占用的字节数[byte value]是由字符编码方案[character
encoding scheme]决定的,字符编码方案也被称为字符集[character set]或编码页[code page]。
|
|
011 |
The database's character set is established when you create the
database. Examples of character sets are 7-bit ASCII (American Standard
Code for Information Interchange), EBCDIC (Extended Binary Coded Decimal
Interchange Code), Code Page 500, Japan Extended UNIX, and Unicode
UTF-8. Oracle supports both single-byte and multibyte encoding schemes.
|
Oracle 数据库的字符集是在创建数据库时设定的。常见的字符集包括 7-bit ASCII(American Standard Code
for Information Interchange[美国标准信息交换编码]),EBCDIC(Extended Binary Coded
Decimal Interchange Code[扩展二进制编码的十进制交换编码]),Code Page 500,Japan Extended
UNIX,及 Unicode UTF-8。Oracle 支持单字节[single-byte]及多字节[multibyte]编码方案。
|
|
012 |
See Also:
|
另见:
|
|
013 |
CHAR Datatype
|
26.2.1 CHAR 数据类型
|
|
014 |
The CHAR datatype stores fixed-length
character strings. When you create a table with a
CHAR column, you must specify a string length (in bytes or
characters) between 1 and 2000 bytes for the CHAR
column width. The default is 1 byte. Oracle then guarantees that:
- When you insert or update a row in the
table, the value for the CHAR column
has the fixed length.
- If you give a shorter value, then the
value is blank-padded to the fixed length.
- If a value is too large, Oracle
returns an error.
|
CHAR 数据类型用于存储固定长度的字符串。如果用户创建数据表时定义了
CHAR 类型的列,则必须为其设定一个介于 1 到 2000
之间的字符串长度(单位为字节[byte]或字符[character])以表明此列的宽度。CHAR
数据类型的默认长度为 1 字节。Oracle 能够确保:
- 当用户插入或更新表的数据行时,CHAR
列数据值为固定长度。
- 如果用户输入的数据值的长度小于定义,Oracle
以空格填充使之达到固定长度。
- 如果用户输入的数据值的长度大于定义,Oracle 返回错误消息。
|
|
015 |
Oracle compares CHAR values using
blank-padded comparison semantics.
|
Oracle 在比较 CHAR
类型的数据时采用空格填充比较语义[blank-padded comparison semantics]。
|
|
016 |
See Also:
Oracle Database SQL Reference for details about blank-padded
comparison semantics
|
另见:
Oracle Database SQL Reference
了解空格填充比较语义
|
|
017 |
VARCHAR2 and VARCHAR Datatypes
|
26.2.2 VARCHAR2 及 VARCHAR 数据类型
|
|
018 |
The VARCHAR2 datatype stores
variable-length character strings. When you create a table with a
VARCHAR2 column, you specify a maximum
string length (in bytes or characters) between 1 and 4000 bytes for the
VARCHAR2 column. For each row, Oracle
stores each value in the column as a variable-length field unless a
value exceeds the column's maximum length, in which case Oracle returns
an error. Using VARCHAR2 and
VARCHAR saves on space used by the table.
|
VARCHAR2 数据类型用于存储变长的字符串。如果用户创建数据表时定义了 VARCHAR2
类型的列,则必须为其设定一个介于 1 到 4000
之间的字符串长度(单位为字节[byte]或字符[character])以表明此列的宽度。Oracle 为每行的 VARCHAR2
列存储数据所占用的长度是可变的,如果输入的数据长度超出列的定义宽度 Oracle 将返回错误消息。使用 VARCHAR2
及
VARCHAR 类型能够节约数据表所占用的存储空间。
|
|
019 |
For example, assume you declare a column VARCHAR2
with a maximum size of 50 characters. In a single-byte character set, if
only 10 characters are given for the VARCHAR2
column value in a particular row, the column in the row's row piece
stores only the 10 characters (10 bytes), not 50.
|
例如,用户定义一个 VARCHAR2
列的最大宽度为 50 字符。当系统采用单字节字符集时,如果输入的数据行此列值的长度为 10 字符,则在此行对应的行片断[row
piece]中,此列只占用 10 字符(10 字节)的存储空间,而非 50 字节。
|
|
020 |
Oracle compares VARCHAR2 values using
nonpadded comparison semantics.
|
Oracle 在比较 VARCHAR2
类型的数据时采用非填充比较语义[nonpadded comparison semantics]。
|
|
021 |
See Also:
Oracle Database SQL Reference for details about nonpadded
comparison semantics
|
另见:
Oracle Database SQL Reference
了解非填充比较语义
|
|
022 |
VARCHAR Datatype
|
26.2.2.1 VARCHAR 数据类型
|
|
023 |
The VARCHAR datatype is synonymous with the
VARCHAR2 datatype. To avoid possible
changes in behavior, always use the VARCHAR2
datatype to store variable-length character strings.
|
VARCHAR 数据类型是
VARCHAR2
数据类型的同义词[synonymous]。为了避免对代码产生影响,开发者应使用
VARCHAR2 数据类型存储变长字符串。
|
|
024 |
Length Semantics for Character Datatypes
|
26.2.3 字符数据类型的长度语义
|
|
025 |
Globalization support allows the use of various character sets for the
character datatypes. Globalization support lets you process single-byte
and multibyte character data and convert between character sets. Client
sessions can use client character sets that are different from the
database character set.
|
Oracle 的全球化支持[globalization
support]特性使字符数据类型能够使用多种字符集。全球化支持特性可以处理单字节及多字节字符数据并在两者间进行转换。客户端会话使用的客户字符集可以与数据库字符集不同。
|
|
026 |
Consider the size of characters when you specify the column length for
character datatypes. You must consider this issue when estimating space
for tables with columns that contain character data.
|
当用户为字符数据类型设定列长度时需要考虑字符的大小。用户在预估数据表所占用的空间时,必须考虑字符数据列对空间的影响。
|
|
027 |
The length semantics of character datatypes can be measured in bytes or
characters.
- Byte semantics treat strings as
a sequence of bytes. This is the default for character datatypes.
- Character semantics treat
strings as a sequence of characters. A character is technically a
codepoint of the database character set.
|
字符数据类型的长度语义[length semantic]有两种:
- 字节语义[byte semantic]把字符串看作字节数据的序列。字符数据类型默认使用此种语义。
- 字符语义[character semantic]把字符串看作字符数据的序列。每个字符
均为数据库字符集中的一个元素。
|
|
028 |
For single byte character sets, columns defined in character semantics
are basically the same as those defined in byte semantics. Character
semantics are useful for defining varying-width multibyte strings; it
reduces the complexity when defining the actual length requirements for
data storage. For example, in a Unicode database (UTF8),
you need to define a VARCHAR2 column that
can store up to five Chinese characters together with five English
characters. In byte semantics, this would require (5*3 bytes) + (1*5
bytes) = 20 bytes; in character semantics, the column would require 10
characters.
|
对于单字节的字符集,采用字符语义定义的列与采用字节语义定义的列基本相同。当定义变长的多字节字符串时,使用字符语义更为简便,用户不必计算数据存储所需的实际长度。例如,在采用
Unicode(UTF8)的数据库中,用户需要定义一个 VARCHAR2
列用于存储最多 5 个中文字符及 5 个英文字符。使用字节语义时,此列需要(5*3 字节)+(5*1 字节)= 20
字节;而使用字符语义时,只需设定此列为 10 字符。
|
|
029 |
VARCHAR2(20 BYTE) and
SUBSTRB(<string>, 1,
20) use byte semantics.
VARCHAR2(10 CHAR) and
SUBSTR(<string>, 1,
10) use character semantics.
|
VARCHAR2(20 BYTE) 及
SUBSTRB(<string>, 1,
20) 采用的是字节语义。而
VARCHAR2(10 CHAR) 及
SUBSTR(<string>, 1,
10) 采用的是字符语义。
|
|
030 |
The parameter NLS_LENGTH_SEMANTICS
decides whether a new column of character datatype uses byte or
character semantics. The default length semantic is byte. If all
character datatype columns in a database use byte semantics (or all
use character semantics) then users do not have to worry about which
columns use which semantics. The BYTE
and CHAR qualifiers shown earlier
should be avoided when possible, because they lead to
mixed-semantics databases. Instead, the
NLS_LENGTH_SEMANTICS initialization parameter should be set
appropriately in the server parameter file (SPFILE) or
initialization parameter file, and columns should use the default
semantics.
|
NLS_LENGTH_SEMANTICS
初始化参数决定了一个字符数据类型列采用字符语义或字节语义。Oracle
默认的长度语义是字节语义。如果一个数据库中所有字符数据类型列均采用字节语义(或均采用字符语义),用户在使用过程中就不会产生混淆。用户应尽量避免
使用前例中的
BYTE 及 CHAR
限定符,因为这将导致同一数据库中存在多种长度语义。用户应在服务端参数文件[server parameter
file,SPFILE]或初始化参数文件[initialization parameter file]中设置 NLS_LENGTH_SEMANTICS
初始化参数,并在定义列时采用初始化参数指定的默认长度语义。
|
|
031 |
See Also:
|
另见:
|
|
032 |
NCHAR and NVARCHAR2 Datatypes
|
26.2.4 NCHAR 及 NVARCHAR2 数据类型
|
|
033 |
NCHAR and NVARCHAR2
are Unicode datatypes that store Unicode character data. The character
set of NCHAR and
NVARCHAR2 datatypes can only be either
AL16UTF16 or UTF8 and is specified
at database creation time as the national character set.
AL16UTF16 and UTF8
are both Unicode encoding.
- The NCHAR
datatype stores fixed-length character strings that correspond to
the national character set.
- The NVARCHAR2
datatype stores variable length character strings.
|
NCHAR 与 NVARCHAR2
均为 Unicode 数据类型,用于存储以 Unicode 编码的字符数据。NCHAR
与 NVARCHAR2 使用的字符集只能为
AL16UTF16 或 UTF8,此字符集为创建数据库时设定的国家字符集[national
character set]。AL16UTF16 及 UTF8
均采用 Unicode 编码。
- NCHAR
数据类型用于存储以国家字符集编码的固定长度字符串。
- NVARCHAR2
数据类型用于存储变长字符串。
|
|
034 |
When you create a table with an NCHAR or
NVARCHAR2 column, the maximum size
specified is always in character length semantics. Character length
semantics is the default and only length semantics for
NCHAR or NVARCHAR2.
|
当用户创建的表包含 NCHAR 或
NVARCHAR2 列时,只能使用字符长度语义设定列的最大长度。字符长度语义是 NCHAR
及
NVARCHAR2 数据类型默认且唯一的长度语义。
|
|
035 |
For example, if national character set is UTF8,
then the following statement defines the maximum byte length of 90
bytes:
|
例如,国家字符集为 UTF8,以下语句所定义的列的最大长度为 90 字节:
|
|
036 |
CREATE TABLE tab1 (col1 NCHAR(30));
|
CREATE TABLE tab1 (col1 NCHAR(30));
|
|
037 |
This statement creates a column with maximum character length of 30. The
maximum byte length is the multiple of the maximum character length and
the maximum number of bytes in each character.
|
上述语句所创建的列的最大长度为 30 字符。列的最大字节长度为列的最大字符长度乘每个字符的最大字节数。
|
|
038 |
NCHAR
|
26.2.4.1 NCHAR
|
|
039 |
The maximum length of an NCHAR column is
2000 bytes. It can hold up to 2000 characters. The actual data is
subject to the maximum byte limit of 2000. The two size constraints must
be satisfied simultaneously at run time.
|
NCHAR 列的最大长度为 2000 字节。NCHAR
列最多容纳 2000 字符。而其中实际存储的数据必须低于 2000 字节的限制。这两个容量限制必须同时满足。
|
|
040 |
NVARCHAR2
|
26.2.4.2 NVARCHAR2
|
|
041 |
The maximum length of an NVARCHAR2 column
is 4000 bytes. It can hold up to 4000 characters. The actual data is
subject to the maximum byte limit of 4000. The two size constraints must
be satisfied simultaneously at run time.
|
NVARCHAR2 列的最大长度为 4000 字节。NVARCHAR2
列最多容纳 4000 字符。而其中实际存储的数据必须低于 4000 字节的限制。这两个容量限制必须同时满足。
|
|
042 |
See Also:
Oracle Database Globalization Support Guide for more information
about the NCHAR and
NVARCHAR2 datatypes
|
另见:
Oracle Database Globalization Support Guide
了解关于 NCHAR 及 NVARCHAR2
数据类型的详细信息
|
|
043 |
Use of Unicode Data in an Oracle Database
|
26.2.5 在 Oracle 数据库中使用 Unicode 数据
|
|
044 |
Unicode is an effort to have a unified encoding of every character in
every language known to man. It also provides a way to represent
privately-defined characters. A database column that stores Unicode can
store text written in any language.
|
Unicode 的目标是将所有人类语言使用的字符统一编码。同时 Unicode 还提供了自定义字符的方式。一个能够存储 Unicode
的数据库列能够存储任何语言的文本数据。
|
|
045 |
Oracle users deploying globalized applications have a strong need to
store Unicode data in Oracle databases. They need a datatype which is
guaranteed to be Unicode regardless of the database character set.
|
如果用户需要部署全球化的应用程序,一定需要在 Oracle 数据库中存储 Unicode 数据。用户需要一种保证采用 Unicode
编码的数据类型(无论数据库字符集采用何种编码方式)。
|
|
046 |
Oracle supports a reliable Unicode datatype through
NCHAR, NVARCHAR2,
and NCLOB. These datatypes are guaranteed
to be Unicode encoding and always use character length semantics. The
character sets used by NCHAR/NVARCHAR2 can
be either UTF8 or
AL16UTF16, depending on the setting of the national character set
when the database is created. These datatypes allow character data in
Unicode to be stored in a database that may or may not use Unicode as
database character set.
|
Oracle 通过
NCHAR,NVARCHAR2,及 NCLOB
数据类型提供了可靠的 Unicode 支持。Oracle 确保这些数据类型采用 Unicode
编码,并使用字符长度语义。这些数据类型使用的字符集为 UTF8 或
AL16UTF16,由创建数据库时设定的国家字符集决定。通过这些数据类型,用户可以在未使用 Unicode
数据库字符集的数据库中存储以 Unicode 编码的数据。
|
|
047 |
Implicit Type Conversion
|
26.2.5.1 隐式类型转换
|
|
048 |
In addition to all the implicit conversions for
CHAR/VARCHAR2, Oracle also supports implicit conversion for
NCHAR/NVARCHAR2. Implicit conversion
between CHAR/VARCHAR2 and
NCHAR/NVARCHAR2 is also supported.
|
Oracle 支持
CHAR/VARCHAR2 数据类型的隐式转换,也支持
NCHAR/NVARCHAR2 数据类型的隐式转换。此外,Oracle 还支持
CHAR/VARCHAR2 及
NCHAR/NVARCHAR2 间的隐式转换。
|
|
049 |
LOB Character Datatypes
|
26.2.6 LOB 字符数据类型
|
|
050 |
The LOB datatypes for character data are CLOB
and NCLOB. They can store up to 8 terabytes
of character data (CLOB) or national
character set data (NCLOB).
|
CLOB
与 NCLOB 是用于存储字符数据的 LOB 数据类型。这两种数据类型分别可存储最大
8 TB(terabyte)的字符数据(CLOB)及国家字符集数据(NCLOB)。
|
|
051 |
See Also:
"Overview of LOB Datatypes"
|
另见:
“LOB 数据类型概述”
|
|
052 |
LONG Datatype
|
26.2.7 LONG 数据类型
|
|
053 |
Note:
Do not create tables with LONG columns.
Use LOB columns (CLOB,
NCLOB) instead.
LONG columns are supported only for backward compatibility.
Oracle also recommends that you convert existing
LONG columns to LOB columns. LOB
columns are subject to far fewer restrictions than
LONG columns. Further, LOB
functionality is enhanced in every release, whereas
LONG functionality has been static for
several releases.
|
提示:
不要在新创建的表中使用数据类型为 LONG
的数据列,而应使用 LOB 数据类型(CLOB,NCLOB)作为替代。LONG
数据类型只用于向后兼容[backward compatibility]。
Oracle 建议用户将现有的 LONG 数据列转换为 LOB
类型。LOB
数据类型所受的限制远少于
LONG 类型。此外,Oracle 各个版本中均对 LOB
功能有所加强,而关于
LONG 的功能已经不再更新。
|
|
054 |
Columns defined as LONG can store
variable-length character data containing up to 2 gigabytes of
information. LONG data is text data that is
to be appropriately converted when moving among different systems.
|
定义为 LONG 类型的列可存储最大 2 GB 的变长数据。LONG
类型的数据实际为文本数据,经过适当转换后可以在不同系统间进行迁移。
|
|
055 |
LONG datatype columns are used in the data dictionary to store the text
of view definitions. You can use LONG
columns in SELECT lists,
SET clauses of UPDATE
statements, and VALUES clauses of
INSERT statements.
|
Oracle
数据字典中使用 LONG 数据类型的列存储视图定义的文本。用户可以在
SELECT 列表,UPDATE
语句的 SET 子句,及 INSERT
语句的 VALUES 子句中使用 LONG
类型的列。
|
|
056 |
See Also:
|
另见:
|
|
057 |
Overview of
Numeric Datatypes
|
26.3 数字数据类型概述
|
|
058 |
The numeric datatypes store positive and negative fixed and
floating-point numbers, zero, infinity, and values that are the
undefined result of an operation (that is, is "not a number" or NAN).
|
数字数据类型[numeric datatype]用于存储正负整数、浮点数,零,无穷数,及一个操作返回的未定义结果值(即“非数字”或 NAN)。
|
|
059 |
NUMBER Datatype
|
26.3.1 NUMBER 数据类型
|
|
060 |
The NUMBER datatype stores fixed and
floating-point numbers. Numbers of virtually any magnitude can be stored
and are guaranteed portable among different systems operating Oracle, up
to 38 digits of precision.
|
NUMBER 数据类型用于存储固定及浮点数字。NUMBER
数据类型可以存储任意数量级的数字,NUMBER
数据能够在运行 Oracle 的不同操作系统间迁移,数字的精度[precision]最高可达 38 位。
|
|
061 |
The following numbers can be stored in a NUMBER column:
- Positive numbers in the range 1 x 10-130
to 9.99...9 x 10125 with up to 38 significant digits
- Negative numbers from -1 x 10-130
to 9.99...99 x 10125 with up to 38 significant digits
- Zero
- Positive and negative infinity
(generated only by importing from an Oracle Version 5 database)
|
以下数字数据均可以存储在 NUMBER 类型的数据列中:
- 范围在 1 x 10-130
到 9.99...9 x 10125 之间的正数,最高 38 位有效数字
- 范围在 -1 x 10-130
到 9.99...99 x 10125 之间的负数,最高 38 位有效数字
- 零
- 正负无穷(只存在于从 Oracle 数据库版本 5 导入的数据中)
|
|
062 |
For numeric columns, you can specify the column as:
|
用户可以按以下方式定义数字列:
|
|
063 |
column_name NUMBER
|
column_name NUMBER
|
|
064 |
Optionally, you can also specify a precision (total number of
digits) and scale (number of digits to the right of the decimal
point):
|
此外,用户还可以设定 NUMBER 数据类型的精度[precision](数字位数)和小数位[scale](小数点右侧的数据位数):
|
|
065 |
column_name NUMBER (precision, scale)
|
column_name NUMBER (precision, scale)
|
|
066 |
If a precision is not specified, the column stores values as given. If
no scale is specified, the scale is zero.
|
如果用户没有设定精度,则此列直接存储输入的值。如果用户没有设定小数位,则小数位默认为 0。
|
|
067 |
Oracle guarantees portability of numbers with a precision equal to or
less than 38 digits. You can specify a scale and no precision:
|
Oracle 在迁移 NUMBER 类型的数据时,能够确保其精度小于等于 38
位。用户定义 NUMBER 类型时可以只设定小数位而不设定精度:
|
|
068 |
column_name NUMBER (*, scale)
|
column_name NUMBER (*, scale)
|
|
069 |
In this case, the precision is 38, and the specified scale is
maintained.
|
上述例子中,数据列的精度为 38,小数位为用户设定的值。
|
|
070 |
When you specify numeric fields, it is a good idea to specify the
precision and scale. This provides extra integrity checking on input.
|
在定义数字数据类型的数据列时应尽量设定精度及小数位。Oracle 能够根据定义对输入的数据进行完整性检查。
|
|
071 |
Table 26-1 shows examples of how data would be stored using different
scale factors.
|
表 26-1 显示了不同小数位设定下数据是如何存储的。
|
|
072 |
Table 26-1 How Scale Factors
Affect Numeric Data Storage
|
表 26-1 小数位如何影响数字数据的存储
|
|
073 |
|
Input
Data
|
Specified
As
|
Stored As
|
|
7,456,123.89
|
NUMBER
|
7456123.89
|
7,456,123.89
|
NUMBER(*,1)
|
7456123.9
|
7,456,123.89
|
NUMBER(9)
|
7456124
|
7,456,123.89
|
NUMBER(9,2)
|
7456123.89
|
7,456,123.89
|
NUMBER(9,1)
|
7456123.9
|
7,456,123.89
|
NUMBER(6)
|
(not accepted, exceeds
precision)
|
7,456,123.89
|
NUMBER(7,-2)
|
7456100
|
|
|
Input
Data
|
Specified
As
|
Stored As
|
|
7,456,123.89
|
NUMBER
|
7456123.89
|
7,456,123.89
|
NUMBER(*,1)
|
7456123.9
|
7,456,123.89
|
NUMBER(9)
|
7456124
|
7,456,123.89
|
NUMBER(9,2)
|
7456123.89
|
7,456,123.89
|
NUMBER(9,1)
|
7456123.9
|
7,456,123.89
|
NUMBER(6)
|
(不接受,超过设定的精度值)
|
7,456,123.89
|
NUMBER(7,-2)
|
7456100
|
|
|
074 |
If you specify a negative scale, then Oracle rounds the actual data to
the specified number of places to the left of the decimal point. For
example, specifying (7,-2) means Oracle rounds to the nearest
hundredths, as shown in Table 26-1.
|
如果用户设定的小数位为负值,Oracle 在小数点左侧的相应位置对输入数据四舍五入。例如,设定 (7,-2) 表示 Oracle
将在十位对数据进行四舍五入,如
表 26-1 所示。
|
|
075 |
For input and output of numbers, the standard Oracle default decimal
character is a period, as in the number 1234.56. The decimal is the
character that separates the integer and decimal parts of a number.
You can change the default decimal character with the initialization
parameter NLS_NUMERIC_CHARACTERS. You
can also change it for the duration of a session with the
ALTER SESSION statement. To enter
numbers that do not use the current default decimal character, use
the TO_NUMBER function.
|
对于输入及输出的数据,Oracle 默认的小数点字符[decimal character]为点号,如数字 1234.56
所示。小数点字符是分隔数字整数及小数部分的字符。用户可以通过初始化参数 NLS_NUMERIC_CHARACTERS
修改默认的小数点字符。用户也可以使用
ALTER SESSION
语句修改一个会话的默认小数点字符。如需输入没有采用默认小数点字符的数据,可以使用 TO_NUMBER
函数进行转换。
|
|
076 |
Internal Numeric Format
|
26.3.1.1 数字数据存储格式
|
|
077 |
Oracle stores numeric data in variable-length format. Each value is
stored in scientific notation, with 1 byte used to store the exponent
and up to 20 bytes to store the mantissa. The resulting value is limited
to 38 digits of precision. Oracle does not store leading and trailing
zeros. For example, the number 412 is stored in a format similar to 4.12
x 102, with 1 byte used to store the exponent(2)
and 2 bytes used to store the three significant digits of the mantissa(4,1,2).
Negative numbers include the sign in their length.
|
Oracle 以变长格式存储数字数据。所有数字数据均以科学计数法存储[scientific notation],使用 1 字节存储指数[exponent],最多
20 字节存储尾数[mantissa]。最终存储的数字数据受限于 38 位精度限制。且 Oracle 不会存储数字首端及末端的 0。举例来说,数字 412
在存储时的格式可以理解为 4.12 x 102,Oracle 使用 1 字节存储指数(2),使用
2 字节存储尾数的三位有效数字(4,1,2)。存储负数时,符号也需要占用存储空间。
|
|
078 |
Taking this into account, the column size in bytes for a particular
numeric data value NUMBER(p), where
p is the precision of a given value, can be
calculated using the following formula:
|
综合上述存储特性,定义为 NUMBER(p)(p
表示数字值的精度)的数字数据值所占用的字节数可以通过以下公式计算:
|
|
079 |
ROUND((length(p)+s)/2))+1
|
ROUND((length(p)+s)/2))+1
|
|
080 |
where s equals zero if the number is
positive, and s equals 1 if the number is
negative.
|
如果数字为正数则 s 值为 0,如果数字为负值则
s 值为 1。
|
|
081 |
Zero and positive and negative infinity (only generated on import from
Version 5 Oracle databases) are stored using unique representations.
Zero and negative infinity each require 1 byte; positive infinity
requires 2 bytes.
|
零与正负无穷(只存在于从 Oracle 数据库版本 5 导入的数据中)采用特殊格式存储。零及负无穷需要占用 1 字节,正无穷需要占用 2 字节。
|
|
082 |
Floating-Point Numbers
|
26.3.2 浮点数字
|
|
083 |
Oracle provides two numeric datatypes exclusively for floating-point
numbers: BINARY_FLOAT and
BINARY_DOUBLE. They support all of the
basic functionality provided by the NUMBER
datatype. However, while NUMBER uses
decimal precision, BINARY_FLOAT and
BINARY_DOUBLE use binary precision. This
enables faster arithmetic calculations and usually reduces storage
requirements.
|
Oracle 为存储浮点数提供了两种专用的数据类型:BINARY_FLOAT 与
BINARY_DOUBLE。这两种数据类型均支持
NUMBER 数据类型所提供的基本功能。但 BINARY_FLOAT
及
BINARY_DOUBLE 采用二进制精度,而
NUMBER
采用十进制精度。因此这两种数据类型能够提供更快的数学运算速度,且能减少占用的存储空间。
|
|
084 |
BINARY_FLOAT and
BINARY_DOUBLE are approximate numeric datatypes. They store
approximate representations of decimal values, rather than exact
representations. For example, the value 0.1 cannot be exactly
represented by either BINARY_DOUBLE or
BINARY_FLOAT. They are frequently used for
scientific computations. Their behavior is similar to the datatypes
FLOAT and DOUBLE
in Java and XMLSchema.
|
BINARY_FLOAT 与
BINARY_DOUBLE 均为近似数字类型。她们存储数字的近似值而非准确值。例如,BINARY_FLOAT
或
BINARY_DOUBLE 只能存储 0.1 的近似值。此种数据类型常用于科学计算。此种数据类型上可执行的操作与 Java 及
XMLSchema 中的
FLOAT 和 DOUBLE
数据类型类似。
|
|
085 |
BINARY_FLOAT Datatype
|
26.3.2.1 BINARY_FLOAT 数据类型
|
|
086 |
BINARY_FLOAT is a 32-bit, single-precision
floating-point number datatype. Each BINARY_FLOAT
value requires 5 bytes, including a length byte.
|
BINARY_FLOAT 是一种 32 位,单精度浮点数字数据类型。每个 BINARY_FLOAT
值需要 5 字节存储空间,其中 1 字节用于存储数据值的长度。
|
|
087 |
BINARY_DOUBLE Datatype
|
26.3.2.2 BINARY_DOUBLE 数据类型
|
|
088 |
BINARY_DOUBLE is a 64-bit, double-precision
floating-point number datatype. Each BINARY_DOUBLE
value requires 9 bytes, including a length byte.
|
BINARY_DOUBLE 是一种 64 位,双精度浮点数字数据类型。每个 BINARY_DOUBLE
值需要 9 字节存储空间,其中 1 字节用于存储数据值的长度。
|
|
089 |
Note:
BINARY_DOUBLE and
BINARY_FLOAT implement most of the Institute of Electrical
and Electronics Engineers (IEEE) Standard for Binary Floating-Point
Arithmetic, IEEE Standard 754-1985 (IEEE754). For a full description
of the Oracle implementation of floating-point numbers and its
differences from IEEE754, see the
Oracle Database SQL Reference
|
提示:
BINARY_DOUBLE 与
BINARY_FLOAT 类型基本符合 IEEE
制定的二进制浮点数字标准,IEEE 标准 754-1985(IEEE754)。用户可参考
Oracle Database SQL Reference
了解 Oracle 浮点数据类型实现的详细描述,及其与 IEEE754 标准的区别。
|
|
090 |
Overview of DATE
Datatype
|
26.4 DATE 数据类型概述
|
|
091 |
The DATE datatype stores point-in-time
values (dates and times) in a table. The DATE
datatype stores the year (including the century), the month, the day,
the hours, the minutes, and the seconds
(after midnight).
|
DATE 数据类型用于存储时刻点[point-in-time]数据(包括日期及时间)。DATE
数据类型中包含年(包括世纪),月,日,小时,分钟,及秒。
|
|
092 |
Oracle can store dates in the Julian era, ranging from January 1, 4712
BCE through December 31, 4712 CE (Common Era, or 'AD'). Unless BCE ('BC'
in the format mask) is specifically used, CE date entries are the
default.
|
Oracle 能够存储儒略历[Julian era]形式的日期数据,范围自公元前 4712 年 1 月 1 日起,至公元 4712 年 12 月
31 日止。CE(公元后,也写为“AD”)是 DATE
数据的默认属性,除非用户设定使用 BCE(在格式掩码[format mask]中以“BC”表示)。
|
|
093 |
Oracle uses its own internal format to store dates. Date data is stored
in fixed-length fields of seven bytes each, corresponding to century,
year, month, day, hour, minute, and second.
|
Oracle 实际存储日期数据时使用一种内部格式。日期数据的存储长度固定为 7 字节,各个字节依次为世纪,年,月,日,小时,分钟,及秒。
|
|
094 |
For input and output of dates, the standard Oracle date format is
DD-MON-YY, as follows:
|
在输入输出日期数据时,Oracle 使用的标准日期格式为
DD-MON-YY,如下所示:
|
|
095 |
'13-NOV-92'
|
'13-NOV-92'
|
|
096 |
You can change this default date format for an instance with the
parameter NLS_DATE_FORMAT. You can also
change it during a user session with the ALTER
SESSION statement. To enter dates that are not in standard Oracle
date format, use the TO_DATE function with
a format mask:
|
用户可以通过 NLS_DATE_FORMAT 参数修改实例的日期格式。用户也可以使用 ALTER
SESSION 语句修改会话期间的日期格式。如需输入非 Oracle 标准格式的日期数据,可以使用 TO_DATE
函数及格式掩码进行转换:
|
|
097 |
TO_DATE ('November 13, 1992', 'MONTH DD, YYYY')
|
TO_DATE ('November 13, 1992', 'MONTH DD, YYYY')
|
|
098 |
Oracle stores time in 24-hour format—HH:MI:SS.
By default, the time in a date field is 00:00:00
A.M. (midnight) if no time portion is entered. In a time-only
entry, the date portion defaults to the first day of the current month.
To enter the time portion of a date, use the
TO_DATE function with a format mask indicating the time portion,
as in:
|
Oracle 以 24 小时的格式存储时间数据——HH:MI:SS。如用户没有输入日期数据中的时间部分,默认的时间值为 00:00:00
A.M(午夜)。对于只具有时间的日期数据,日期部分的默认值为当前月的第一天。在输入日期数据的时间部分时,可以使用 TO_DATE
函数及格式掩码:
|
|
099 |
INSERT INTO birthdays (bname, bday) VALUES
('ANDY',TO_DATE('13-AUG-66 12:56 A.M.','DD-MON-YY HH:MI A.M.'));
|
INSERT INTO birthdays (bname, bday) VALUES ('ANDY',TO_DATE('13-AUG-66 12:56 A.M.','DD-MON-YY HH:MI A.M.'));
|
|
100 |
Use of Julian Dates
|
26.4.1 使用儒略历日期数据
|
|
101 |
Julian dates allow continuous dating by the number of days from a common
reference. (The reference is 01-01-4712 years BCE, so current dates are
somewhere in the 2.4 million range.) A Julian date is nominally a
noninteger, the fractional part being a portion of a day. Oracle uses a
simplified approach that results in integer values.
Julian dates can be
calculated and interpreted differently. The calculation method used by
Oracle results in a seven-digit number (for dates most often used), such
as 2449086 for 08-APR-93.
|
对于儒略历日期数据,可以在一个基准日期的基础上通过天数累加来进行日期计算。(基准日期为 01-01-公元前 4712,因此现在的时间大概位于
240 万天前后的位置上。)儒略历日期数据理论上是一个非整型数,数据的小数部分代表一天中的部分时间。Oracle 将儒略历日期数据简化为整数值。儒略历日期数据有多种
计算及解释方法。Oracle
使用的计算方法能够将(大多数常用的)日期数据转换为一个 7 位的数字,例如 2449086 对应 08-APR-93。
|
|
102 |
Note:
Oracle Julian dates might not be compatible with Julian dates
generated by other date algorithms.
|
提示:
Oracle 儒略历日期数据可能与其他日期算法生成的儒略历日期数据不兼容。
|
|
103 |
The format mask 'J' can be used with date
functions (TO_DATE or
TO_CHAR) to convert date data into Julian dates. For example, the
following query returns all dates in Julian date format:
|
在日期函数(TO_DATE 或
TO_CHAR)中可以使用格式掩码“J”将日期数据转换为儒略历日期数据。例如,以下查询将所有日期数据转换为儒略历日期格式:
|
|
104 |
SELECT TO_CHAR (hire_date, 'J') FROM employees;
|
SELECT TO_CHAR (hire_date, 'J') FROM employees;
|
|
105 |
You must use the TO_NUMBER function if you
want to use Julian dates in calculations. You can use the
TO_DATE function to enter Julian dates:
|
如果用户需要对儒略历日期数据进行计算,必须使用 TO_NUMBER 函数。用户可以使用
TO_DATE 函数输入儒略历日期数据:
|
|
106 |
INSERT INTO employees (hire_date) VALUES (TO_DATE(2448921, 'J'));
|
INSERT INTO employees (hire_date) VALUES (TO_DATE(2448921, 'J'));
|
|
107 |
Date Arithmetic
|
26.4.2 日期数据运算
|
|
108 |
Oracle date arithmetic takes into account the anomalies of the calendars
used throughout history. For example, the switch from the Julian to the
Gregorian calendar, 15-10-1582, eliminated the previous 10 days
(05-10-1582 through 14-10-1582). The year 0 does not exist.
|
Oracle 进行日期数据运算时能够考虑历史上存在异常的日期。例如,将儒略历日期数据转换为格利高里历日期数据时,不计算
05-10-1582 至 14-10-1582 之间的 10 天。此外,Oracle 中不存在 0 年。
|
|
109 |
You can enter missing dates into the database, but they are ignored in
date arithmetic and treated as the next "real" date. For example, the
next day after 04-10-1582 is 15-10-1582, and the day following
05-10-1582 is also 15-10-1582.
|
用户可以向数据库中输入上述 10 天中的日期,但在日期算法中这些日期将被忽略,其值以下一个有效日期替代。例如,在 Oracle
中,04-10-1582 之后的一天为 15-10-1582,05-10-1582 之后的一天也为 15-10-1582。
|
|
110 |
Note:
This discussion of date arithmetic might not apply to all countries'
date standards (such as those in Asia).
|
提示:
本节讨论的日期算法可能不适合所有国家(例如某些亚洲国家)的日期标准。
|
|
111 |
Centuries and the Year 2000
|
26.4.3 世纪及 2000 年
|
|
112 |
Oracle stores year data with the century information. For example, the
Oracle database stores 1996 or 2001, and not simply 96 or 01. The
DATE datatype always stores a four-digit
year internally, and all other dates stored internally in the database
have four digit years. Oracle utilities such as import, export, and
recovery also deal with four-digit years.
|
Oracle 在存储年份数据时包含了世纪信息。例如,Oracle 数据库中存储的年份为 1996 或 2001,而非 96 或 01。DATE
数据类型在数据库内部存储时使用 4 位数字表示年份,其他日期类型在数据库内部存储也使用 4 位数字表示年份。用于数据导入,导出及数据恢复的
Oracle 工具也使用 4 位数字表示年份。
|
|
113 |
Daylight Savings Support
|
26.4.4 夏令时支持
|
|
114 |
Oracle Database provides daylight savings support for
DATETIME datatypes in the server. You can
insert and query DATETIME values based on
local time in a specific region. The DATETIME
datatypes TIMESTAMP WITH TIME ZONE and
TIMESTAMP WITH LOCAL TIME ZONE are
time-zone aware.
|
Oracle 数据库处理
DATETIME 数据类型时支持夏令时[daylight
saving]。用户在插入或查询
DATETIME 类型的数据时可以使用特定区域的当地时间。DATETIME
数据类型中的 TIMESTAMP WITH TIME ZONE 及
TIMESTAMP WITH LOCAL TIME ZONE 类型能够处理时区。
|
|
115 |
See Also:
|
另见:
|
|
116 |
Time Zones
|
26.4.5 时区
|
|
117 |
You can include the time zone in your date/time data and provides
support for fractional seconds. Three new datatypes are added to
DATE, with the following differences:
|
在日期/时间数据中可以加入时区数据,日期/时间数据也支持分数秒[fractional second]。除
DATE 数据类型外还有三种日期数据类型,这些类型的区别如下所示:
|
|
118 |
|
Datatype
|
Time Zone
|
Fractional Seconds
|
|
DATE
|
No
|
No
|
TIMESTAMP
|
No
|
Yes
|
TIMESTAMP WITH TIME ZONE
|
Explicit
|
Yes
|
TIMESTAMP WITH LOCAL TIME ZONE
|
Relative
|
Yes
|
|
|
日期类型
|
时区
|
分数秒
|
|
DATE
|
否
|
否
|
TIMESTAMP
|
否
|
是
|
TIMESTAMP WITH TIME ZONE
|
显式的
|
是
|
TIMESTAMP WITH LOCAL TIME ZONE
|
相对的
|
是
|
|
|
119 |
TIMESTAMP WITH LOCAL TIME ZONE is stored in
the database time zone. When a user selects the data, the value is
adjusted to the user's session time zone.
|
TIMESTAMP WITH LOCAL TIME ZONE
数据是根据数据库时区设置存储的。当用户查询数据时,数据值将被调整为用户会话所在时区的时间。
|
|
120 |
For example, a San Francisco database has system time zone = -8:00. When
a New York client (session time zone = -5:00) inserts into or selects
from the San Francisco database, TIMESTAMP WITH
LOCAL TIME ZONE data is adjusted as follows:
- The New York client inserts
TIMESTAMP'1998-1-23 6:00:00-5:00' into
a TIMESTAMP WITH LOCAL TIME ZONE column
in the San Francisco database. The inserted data is stored in San
Francisco as binary value 1998-1-23 3:00:00.
- When the New York client selects that
inserted data from the San Francisco database, the value displayed
in New York is '1998-1-23 6:00:00'.
- A San Francisco client, selecting the
same data, see the value '1998-1-23 3:00:00'.
|
例如,位于 San Francisco 的数据库的系统时区为 -8:00。一个位于 New York 的客户(会话时区为 -5:00)在 San Francisco
的数据库中进行查询或插入操作时,TIMESTAMP WITH
LOCAL TIME ZONE 数据的调整规则如下:
- 位于 New York 的客户将值为
'1998-1-23 6:00:00-5:00' 的
TIMESTAMP 数据插入到 San Francisco 数据库的 TIMESTAMP WITH LOCAL TIME ZONE
列。此数据在数据库中的二进制值为 1998-1-23 3:00:00。
- 当位于 New York 的客户从 San Francisco
的数据库中查询此数据时显示为 '1998-1-23 6:00:00'。
- 位于 San Francisco 的客户查询相同的数据则显示为 '1998-1-23 3:00:00'。
|
|
121 |
Note:
To avoid unexpected results in your DML operations on datatime data,
you can verify the database and session time zones by querying the
built-in SQL functions DBTIMEZONE and
SESSIONTIMEZONE. If the database time
zone or the session time zone has not been set manually, Oracle uses
the operating system time zone by default. If the operating system
time zone is not a valid Oracle time zone, then Oracle uses UTC as
the default value.
|
提示:
为了避免对日期时间数据进行 DML 操作时出现非预期的结果,用户可以通过内置的 SQL 函数
DBTIMEZONE 及 SESSIONTIMEZONE
确定数据库及会话的时区设置。如果用户没有手工设置数据库及会话的时区,Oracle
将默认使用操作系统的时区。如果操作系统的时区设置不是有效的 Oracle 时区,Oracle 将默认使用 UTC。
|
|
122 |
See Also:
Oracle Database SQL Reference for details about the syntax of
creating and entering data in time stamp columns
|
另见:
Oracle Database SQL Reference
了解创建及输入时间戳数据的语法
|
|
123 |
Overview of LOB
Datatypes
|
26.5 LOB 数据类型概述
|
|
124 |
The LOB datatypes BLOB,
CLOB, NCLOB,
and BFILE enable you to store and
manipulate large blocks of unstructured data (such as text, graphic
images, video clips, and sound waveforms) in binary or character format.
They provide efficient, random, piece-wise access to the data. Oracle
recommends that you always use LOB datatypes over
LONG datatypes. You can perform parallel queries (but not
parallel DML or DDL) on LOB columns.
|
BLOB,CLOB,NCLOB,及 BFILE
四种 LOB 数据类型可供用户存储或操作二进制格式或字符格式的大型非结构化数据(例如文本,图像,视频片断,及声音波形)。LOB
数据类型能够提供高效,随机,基于块[piece-wise]的数据访问。Oracle 建议用户使用 LOB 数据类型替代
LONG 数据类型。用户可以对 LOB 列执行并行查询(不支持并行 DML 或 DDL)。
|
|
125 |
LOB datatypes differ from LONG and
LONG RAW datatypes in several ways. For
example:
- A table can contain multiple LOB
columns but only one LONG column.
- A table containing one or more LOB
columns can be partitioned, but a table containing a
LONG column cannot be partitioned.
- The maximum size of a LOB is 8
terabytes, and the maximum size of a LONG
is only 2 gigabytes.
- LOBs support random access to data,
but
|