5 Schema Objects
| 001 |
This chapter discusses the different types of database objects contained
in a user's schema. |
本章讨论存储在用户方案(schema)内的各类数据库对象。 |
| 002 |
This chapter contains the following topics:
|
本章包含以下主题: |
| 003 |
Introduction to Schema Objects |
5.1 方案对象简介 |
| 004 |
A schema is a collection of logical structures of data, or schema
objects. A schema is owned by a database user and has the same name as
that user. Each user owns a single schema. Schema objects can be created
and manipulated with SQL and include the following types of objects:
|
方案(schema)是一个逻辑数据结构(logical structures of data)(或称为方案对象(schema
object))的集合。每个数据库用户拥有一个与之同名的方案,且只有这一个方案。方案对象可以通过 SQL
语句创建和操作。可以包含于方案的对象类型有:
|
| 005 |
Other types of objects are also stored in the database and can be
created and manipulated with SQL but are not contained in a schema:
|
还有一些类型的对象也存储于数据库中,且可由 SQL
语句创建或操作,但是他们并不属于任何方案(schema):
|
| 006 |
Schema objects are logical data storage structures. Schema objects do
not have a one-to-one correspondence to physical files on disk that
store their information. However, Oracle stores a schema object
logically within a tablespace of the database. The data of each object
is physically contained in one or more of the tablespace's datafiles.
For some objects, such as tables, indexes, and clusters, you can specify
how much disk space Oracle allocates for the object within the
tablespace's datafiles. |
方案对象(schema object)是一种逻辑数据存储结构(logical data storage structure)。Oracle
在逻辑上将方案对象存储于数据库的表空间(tablespace)中,而方案对象的数据在物理上存储于此表空间的一个或多个数据文件(datafile)中。因此方案对象不一定与磁盘上存储其数据的物理文件(physical
file)一一对应。用户可以对表(table),索引(index),及簇(cluster)等对象的磁盘空间分配进行设定。 |
| 007 |
There is no relationship between schemas and tablespaces: a tablespace
can contain objects from different schemas, and the objects for a schema
can be contained in different tablespaces. |
方案(schema)与表空间(tablespace)之间没有必然联系:同一表空间可以包含属于不同方案的对象,而同一方案内的对象也可以存储在不同表空间中。 |
| 008 |
Figure 5-1 illustrates the relationship among
objects, tablespaces, and datafiles. |
图5-1
展示了方案对象(schema object),表空间(tablespace),及数据文件(datafile)之间的关系。 |
| 009 |
Figure 5-1 Schema Objects,
Tablespaces, and Datafiles |
图5-1 方案对象(schema
object),表空间(tablespace),及数据文件(datafile) |
| 010 | ||
| 011 |
|
图5-1 展示了一个 SYSTEM 表空间及一个用户数据表空间。SYSTEM 表空间内包含了数个表及索引对象,而用户数据表空间内则包含了表,索引,及簇等对象。 |
| 012 |
|
另见: |
| 013 |
Overview of Tables |
5.2 表概述 |
| 014 |
Tables are the basic unit of data storage in an Oracle database.
Data is stored in rows and columns. You define a table
with a table name (such as employees)
and set of columns. You give each column a column name (such as
employee_id,
last_name, and job_id), a
datatype (such as VARCHAR2,
DATE, or NUMBER),
and a width. The width can be predetermined by the datatype, as
in DATE. If columns are of the
NUMBER datatype, define precision
and scale instead of width. A row is a collection of column
information corresponding to a single record. |
表(table)是 Oracle 数据库中最基本的数据存储结构。数据在表中以行(row)和列(column)的形式存储。用户在定义表时,需要设定表名(table
name)(例如 employees 表),还要设定表内各列的列名(column name)(例如 employee_id,last_name,及 job_id
列),数据类型(datatype)(例如VARCHAR2,DATE,或 NUMBER),及宽度(width)。有些数据类型的宽度是固定的,例如 DATE
类型。而对于
NUMBER 类型的列来说,则需要定义精度(precision)及数值范围(scale)。
数据行是一条记录(single
record)各列信息的集合。 |
| 015 |
You can specify rules for each column of a table. These rules are called
integrity constraints. One example is a NOT
NULL integrity constraint. This constraint forces the column to
contain a value in every row. |
用户可以为一个表的各列数据的值设定规则。这些规则被称为完整性约束(integrity constraint)。例如 NOT
NULL 完整性约束,她要求各行的此列必须包含数据值。 |
| 016 |
You can also specify table columns for which data is encrypted before
being stored in the datafile. Encryption prevents users from
circumventing database access control mechanisms by looking inside
datafiles directly with operating system tools. |
用户可以设定表内某些列(column)的数据在存储到数据文件(datafile)之前首先进行加密(encryption)。加密可以防止未经授权的用户绕过数据库访问控制机制,使用操作系统工具直接察看数据文件的内容。 |
| 017 |
After you create a table, insert rows of data using SQL statements.
Table data can then be queried, deleted, or updated using SQL. |
用户创建(create)表后,就可以使用 SQL 语句向其中插入(insert)数据,或使用 SQL
语句查询(query),删除(delete),或更新(update)表内的数据。 |
| 018 |
Figure 5-2 shows a sample table. |
图5-2
展示了一个简单的表。 |
| 019 |
Figure 5-2 The EMP Table |
图5-2 EMP 表 |
| 020 | ||
| 021 |
|
图5-2 显示了 emp 表的一部分,包括列名及四行数据。各列的名称为 ENAME,JOB,MGR,HIREDATE,SAL,COMM,及 DEPTNO。 |
| 022 |
|
|
| 023 |
How Table Data Is Stored |
5.2.1 表数据如何存储 |
| 024 |
When you create a table, Oracle automatically allocates a data segment
in a tablespace to hold the table's future data. You can control the
allocation and use of space for a table's data segment in the following
ways:
|
当用户创建一个表时,Oracle 会自动地在相应的表空间内(tablespace)为此表分配数据段(data
segment)以容纳其数据。用户可以采用以下方式分别控制数据段的空间分配与使用:
|
| 025 |
Oracle stores data for a clustered table in the data segment created for
the cluster instead of in a data segment in a tablespace. Storage
parameters cannot be specified when a clustered table is created or
altered. The storage parameters set for the cluster always control the
storage of all tables in the cluster. |
Oracle 在存储簇表(clustered table)的数据时统一使用为其所属簇(cluster)创建的数据段(data
segment),而不是为每个簇表单独创建数据段。创建或修改簇表时不能为其设定存储参数(storage
parameter)。一个簇内的所有簇表都使用此簇的存储参数来控制其空间分配。 |
| 026 |
A table's data segment (or cluster data segment, when dealing with a
clustered table) is created in either the table owner's default
tablespace or in a tablespace specifically named in the
CREATE TABLE statement. |
表使用的数据段(data segment)(或簇表(clustered table)使用的簇数据段(cluster data
segment))既可以创建在该表所有者(owner)的默认表空间(tablespace)里,也可以创建在
CREATE TABLE 语句中指定的表空间里。 |
| 027 |
|
另见: |
| 028 |
Row Format and Size |
5.2.1.1 行的格式及容量 |
| 029 |
Oracle stores each row of a database table containing data for less than
256 columns as one or more row pieces. If an entire row can be inserted
into a single data block, then Oracle stores the row as one row piece.
However, if all of a row's data cannot be inserted into a single data
block or if an update to an existing row causes the row to outgrow its
data block, then Oracle stores the row using multiple row pieces. A data
block usually contains only one row piece for each row. When Oracle must
store a row in more than one row piece, it is chained across
multiple blocks. |
Oracle 使用一个或多个行片断(row piece)来存储表的每一行数据的前255列。当一个数据块(data
block)可以容纳一个完整的数据行时(且表的列数小于等于 256),那么此行就可以使用一个行片断来存储。当插入(insert)一个数据行,或更新(update)已有数据行时,数据行容量大于数据块容量,那么
Oracle 将使用多个行片断来存储此行。大多数情况下,每个数据行只存储于一个行片断中,且在同一数据块内。当 Oracle
必须使用多个行片断来存储同一数据行时(且每个行片断位于不同的数据块内),此行将在多个数据块间构成行链接(Row Chaining)。 |
| 030 |
When a table has more than 255 columns, rows that have data after the
255th column are likely to be chained within the same block. This is
called intra-block chaining. A chained row's pieces are chained
together using the rowids of the pieces. With intra-block chaining,
users receive all the data in the same block. If the row fits in the
block, users do not see an effect in I/O performance, because no extra
I/O operation is required to retrieve the rest of the row. |
当一个表超过 255 列时,每行第255列之后的数据将作为一个新的行片断(row piece)存储在相同的数据块(data
block)中,这被称为块内链接(intra-block chaining)。由多个行片断组成的行进行块内链接时,使用各行片断的 rowid
进行链接。当一个行为块内链接时,用户可以从同一数据块中访问此行的全部数据。如果一个数据行位于同一数据块内,那么访问此行不会影响 I/O
性能,因为访问此行不会带来额外的 I/O 开销。 |
| 031 |
Each row piece, chained or unchained, contains a row header and
data for all or some of the row's columns. Individual columns can also
span row pieces and, consequently, data blocks.
Figure 5-3 shows the format of a row piece: |
无论链接(chained)或非链接(unchained)的行片断,都包含一个行头(row header),及此行部分或全部的数据。
一行内某一列的数据也有可能跨多个行片断(row
piece),甚至跨多个数据块(data block)。图5-3
显示了行片断的格式。 |
| 032 |
Figure 5-3 The Format of a
Row Piece |
图5-3 行片断的格式 |
| 033 |
|
|
| 034 |
|
图5-3 显示了数据块(data block)中的一个行片断(row piece)。一个行片断由行头(row header)及列数据(column data)构成。列数据部分包含了各列的列长(column length)及列值(column value)。而行头内包含以下内容:行管理开销(row overhead),列数,簇键ID(cluster key ID)(如果是簇表),行片断链接(chained row pieces)的ROWID(如果有链接)。 |
| 035 |
The row header precedes the data and contains information about:
|
行头(row header)位于行数据之前,包含以下信息:
|
| 036 |
A row fully contained in one block has at least 3 bytes of row header.
After the row header information, each row contains column length and
data. The column length requires 1 byte for columns that store 250 bytes
or less, or 3 bytes for columns that store more than 250 bytes, and
precedes the column data. Space required for column data depends on the
datatype. If the datatype of a column is variable length, then the space
required to hold a value can grow and shrink with updates to the data. |
如果一行能被存储于一个数据块(data block)中,那么其行头(row header)所需容量将不少于 3
字节(byte)。在行头信息之后依次储存的是各列的列长(column length)及列值(column
value)。列长存储于列值之前,如列值不超过 250 字节,那么 Oracle 使用 1 字节存储其列长;如列值超过 250 字节,则使用 3
字节存储其列长。列数据(column
data)所需的存储空间取决于此列的数据类型(datatype)。如果某列的数据类型为变长(variable
length)的,那么存储此列值所需的空间可能会随着数据更新而增长或缩小。 |
| 037 |
To conserve space, a null in a column only stores the column length
(zero). Oracle does not store data for the null column. Also, for
trailing null columns, Oracle does not even store the column length. |
为了节约存储空间,如果某列值为空(null),那么数据库中只存储其列长(column length)(值为
0),而不存储任何数据。对位于一行末尾的空列值(trailing null column),数据库中将列长也忽略不予存储。 |
| 038 |
|
提示: |
| 039 |
Clustered rows contain the same information as nonclustered rows. In
addition, they contain information that references the cluster key to
which they belong. |
簇表(clustered)内的行需要存储与非簇表(nonclustered)行相同的信息。除此之外,簇表内各行还需要存储其所属的簇键(cluster key)。 |
| 040 |
|
|
| 041 |
Rowids of Row Pieces |
5.2.1.2 行片断的 rowid |
| 042 |
The rowid identifies each row piece by its location or address.
After they are assigned, a given row piece retains its rowid until the
corresponding row is deleted or exported and imported using Oracle
utilities. For clustered tables, if the cluster key values of a row
change, then the row keeps the same rowid but also gets an additional
pointer rowid for the new values. |
Oracle 使用 rowid 记录每个行片断(row piece)的存储位置和地址。每个行片断得到一个 rowid 之后,这个值将会保持不变,直到其所属行被删除(delete)或经过 Oracle 工具导出并再次导入。对于簇表(clustered table)来说,如果某行的簇键值(cluster key value)发生改变,那么此行除了保存原有的 rowid 之外,还将为新簇键值存储一个额外的 rowid 指针(pointer)。 |
| 043 |
Because rowids are constant for the lifetime of a row piece, it is
useful to reference rowids in SQL statements such as
SELECT, UPDATE,
and DELETE. |
由于行片断(row piece)在其生命周期内拥有固定的 rowid,因此在 SELECT,UPDATE,及 DELETE 等 SQL 语句中可以充分利用 rowid 的这个特性。 |
| 044 |
|
另见: |
| 045 |
Column Order |
5.2.1.3 列顺序 |
| 046 |
The column order is the same for all rows in a given table. Columns are
usually stored in the order in which they were listed in the
CREATE TABLE statement, but this is not
guaranteed. For example, if a table has a column of datatype
LONG, then Oracle always stores this column
last. Also, if a table is altered so that a new column is added, then
the new column becomes the last column stored. |
一个表内所有行的列顺序(column order)都是一致的。列的存储顺序通常和 CREATE TABLE 语句中定义的列顺序是一致的,但是也有例外情况。例如,如果一个表含有数据类型(datatype)为 LONG 的列,那么 Oracle 会将此列存储在行的末尾。当用户修改了表定义向其中添加了新的列,这些列也将存储在行的末尾。 |
| 047 |
In general, try to place columns that frequently contain nulls last so
that rows take less space. Note, though, that if the table you are
creating includes a LONG column as well,
then the benefits of placing frequently null columns last are lost. |
一般来说,应该将出现空值(null)几率较大的列放在最后,以便节约空间。但是当用户创建的表中包含数据类型为 LONG 的列时,上述方法将无法发挥节约空间的作用。 |
| 048 |
Table Compression |
5.2.2 表压缩 |
| 049 |
Oracle's table compression feature compresses data by eliminating
duplicate values in a database block. Compressed data stored in a
database block (also known as disk page) is self-contained. That is, all
the information needed to re-create the uncompressed data in a block is
available within that block. Duplicate values in all the rows and
columns in a block are stored once at the beginning of the block, in
what is called a symbol table for that block. All occurrences of such
values are replaced with a short reference to the symbol table. |
Oracle 的表压缩(table compression)功能可以压缩数据块(data block)内的重复值(duplicate value)。一个包含压缩数据的数据块内同时也存储了用于解压缩(uncompress)信息。[这避免了解压缩带来额外的 I/O 开销]数据块内的每个重复值被存储在在块头(data block header)的符号表(symbol table)内。而在该重复值实际发生的位置,只需存储一个指向符号表内对应位置的指针。 |
| 050 |
With the exception of a symbol table at the beginning, compressed
database blocks look very much like regular database blocks. All
database features and functions that work on regular database blocks
also work on compressed database blocks. |
除了使用符号表(symbol table)之外,压缩的数据块(data block)与普通数据块非常相似。普通数据块可以使用的所有数据库功能和函数,同样也适用于压缩的数据块。 |
| 051 |
Database objects that can be compressed include tables and materialized
views. For partitioned tables, you can choose to compress some or all
partitions. Compression attributes can be declared for a tablespace, a
table, or a partition of a table. If declared at the tablespace level,
then all tables created in that tablespace are compressed by default.
You can alter the compression attribute for a table (or a partition or
tablespace), and the change only applies to new data going into that
table. As a result, a single table or partition may contain some
compressed blocks and some regular blocks.
This guarantees that data
size will not increase as a result of compression; in cases where
compression could increase the size of a block, it is not applied to
that block. |
可以被压缩的数据库对象有表和物化视图(materialized view)。对于分区表(partitioned table),用户可以选择压缩部分或全部分区(partition)。表空间(tablespace),表,及分区表都可以被设定为压缩模式。如果在表空间级作了设定,那么在此表空间内创建的表默认都以压缩模式存储。用户也可以修改一个表(表空间,或分区)的压缩属性,在修改后插入的数据将按照新的模式存储。这样,一个表或分区可以同时包含压缩及常规的数据块(data block)。有时使用表压缩反而会导致数据块内数据容量增长,因此利用上述特性能避免这种压缩带来的容量增长。 |
| 052 |
Using Table Compression |
5.2.2.1 使用表压缩 |
| 053 |
Compression occurs while data is being bulk inserted or bulk loaded.
These operations include:
|
压缩发生在数据批量插入(bulk insert)或批量加载(bulk load)时。具体的操作有:
|
| 054 |
Existing data in the database can also be compressed by moving it into
compressed form through ALTER TABLE and
MOVE statements. This operation takes an
exclusive lock on the table, and therefore prevents any updates and
loads until it completes. If this is not acceptable, then Oracle's
online redefinition utility (DBMS_REDEFINITION
PL/SQL package) can be used. |
使用 ALTER TABLE ... MOVE 语句可以将数据库内已有的数据转换为压缩模式。这个操作将对表使用一个排他锁(exclusive lock),以阻止转换期间对此表的更新(update)和插入(insert)操作。如果用户不希望使用排他锁,还可以使用 Oracle 提供的联机重定义工具(online redefinition utility)(DBMS_REDEFINITION PL/SQL 包)。 |
| 055 |
Data compression works for all datatypes except for all variants of LOBs
and datatypes derived from LOBs, such as
VARRAYs
stored out of line or the XML datatype stored in a
CLOB. |
数据压缩(data compression)适用于各种数据类型,但对于 LOB 及基于 LOB 的数据类型无效。例如以 LOB 形式存储的 VARRAY 对象,或存储于 CLOB 中的 XML 数据都无法进行数据压缩。 |
| 056 |
Table compression is done as part of bulk loading data into the
database. The overhead associated with compression is most visible at
that time. This is the primary trade-off that needs to be taken into
account when considering compression. |
表压缩的工作主要在数据被批量加载(bulk load)到数据库时进行。表压缩带来的开销(overhead)在此时最为显著。因此批量加载的效率在是考虑是否使用表压缩时最需要权衡的问题。 |
| 057 |
Compressed tables or partitions can be modified the same as other Oracle
tables or partitions. For example, data can be modified using
INSERT, UPDATE,
and DELETE statements. However, data
modified without using bulk insertion or bulk loading techniques is not
compressed. Deleting compressed data is as fast as deleting uncompressed
data. Inserting new data is also as fast, because data is not compressed
in the case of conventional INSERT; it is
compressed only doing bulk load. Updating compressed data can be slower
in some cases. For these reasons, compression is more suitable for data
warehousing applications than OLTP applications. Data should be
organized such that read only or infrequently changing portions of the
data (for example, historical data) is kept compressed. |
压缩表(compressed table)或压缩分区(compressed partition)中的数据也可以如同普通的 Oracle 表或分区一样被修改。用户可以使用 INSERT,UPDATE,及 DELETE 语句对压缩的数据进行修改。但是没有使用批量插入(bulk insertion)或批量加载(bulk loading)的数据是不会被压缩的。对压缩数据的删除(delete)操作与删除非压缩数据的所需的时间基本相同。向压缩表中插入(insert)新数据的速度也与平常无异,因为常规的 INSERT 语句并不会压缩数据。而更新(update)操作相对非压缩表要慢一些。基于压缩表的这些特性,数据仓库(data warehousing)比 OLTP 系统更适于使用压缩模式。只读数据及不会经常改变的数据(例如历史数据(historical data))适合以压缩模式存储。 |
| 058 |
Nulls Indicate Absence of Value |
5.2.3 空值的含义 |
| 059 |
A null is the absence of a value in a column of a row. Nulls
indicate missing, unknown, or inapplicable data. A null should not be
used to imply any other value, such as zero. A column allows nulls
unless a NOT NULL or
PRIMARY KEY integrity constraint has been defined for the column,
in which case no row can be inserted without a value for that column. |
空值(null)表示一行的某列无值。空值的含义是数据缺失(missing),未知(unknown),或不适用(inapplicable)。空值不等同于其他任何值,例如
空值不等同于零值(zero)。当某列上定义了 NOT NULL
或
PRIMARY KEY 完整性约束时,此列就不允许为空值,即插入(insert)此列的数据必须有值。 |
| 060 |
Nulls are stored in the database if they fall between columns with data
values. In these cases they require 1 byte to store the length of the
column (zero). |
当一个空值(null)在一行中位于有数据值的两列之间时,此列在数据库中需要占用 1 字节(byte)的空间来存储其列长(值为0)。 |
| 061 |
Trailing nulls in a row require no storage because a new row header
signals that the remaining columns in the previous row are null. For
example, if the last three columns of a table are null, no information
is stored for those columns. In tables with many columns, the columns
more likely to contain nulls should be defined last to conserve disk
space. |
而当一个空值(null)位于行尾时无需占用存储空间,因为新一行的行头(row header)就标志着前一行未存储的字段均为空。例如,一个表的最后
3 列均为空,则数据库中不会存储这 3 列的任何信息。在一个包含多列的表中,包含空值几率较大的列因该被定义在表的最后,以便节约存储空间。 |
| 062 |
Most comparisons between nulls and other values are by definition
neither true nor false, but unknown. To identify nulls in SQL, use the
IS NULL predicate. Use the SQL function
NVL to convert nulls to non-null values. |
Oracle 中规定空值(null)和其他任何值得比较(comparison)结果既非真也非假,而是未知(unknown)。如需在 SQL
中判断空值,应该使用谓词(predicate)IS NULL。用户可以使用 SQL
函数 NVL 将空值转换为非空值。 |
| 063 |
Nulls are not indexed, except when the cluster key column value is null
or the index is a bitmap index. |
空值不会被加入索引(index),但有两种情况例外:其一是为空值的列属于簇键(cluster key),其二是索引为位图索引(bitmap
index)。 |
| 064 |
|
|
| 065 |
Default Values for Columns |
5.2.4 列的默认值 |
| 066 |
You can assign a default value to a column of a table so that when a new
row is inserted and a value for the column is omitted or keyword
DEFAULT is supplied, a default value is
supplied automatically. Default column values work as though an
INSERT statement actually specifies the
default value. |
用户可以为表的某列设定默认值(default value),当插入(insert)新的数据行时如果没有指定此列的值,或在此列使用了
DEFAULT
关键字(keyword),Oracle 将自动地为此列加入默认值。定义了默认列值(default column value)后,就如同在
INSERT 语句的相应位置中添加了一个默认值。 |
| 067 |
The datatype of the default literal or expression must match or be
convertible to the column datatype. |
默认值或默认表达式的数据类型应与相应列的数据类型相同,或能够进行数据转换(convertible)。 |
| 068 |
If a default value is not explicitly defined for a column, then the
default for the column is implicitly set to NULL. |
如果没有为某列显示地定义默认值(default
value),那么此列的默认值被隐式地定义为 NULL。 |
| 069 |
Default Value Insertion and Integrity Constraint Checking |
5.2.4.1 插入默认值及完整性约束检查 |
| 070 |
Integrity constraint checking occurs after the row with a default value
is inserted. For example, in Figure 5-4, a row is
inserted into the emp table that does not
include a value for the employee's department number. Because no value
is supplied for the department number, Oracle inserts the
deptno column's default value of 20. After
inserting the default value, Oracle checks the
FOREIGN KEY integrity constraint defined on the
deptno column. |
当一个包含默认值(default value)的行被插入(insert)之后将发生完整性约束检查(integrity constraint
checking)。例如 图5-4 所示,在插入
emp 表的一行数据中员工部门编号字段没有指定值。因此 Oracle 将使用
deptno 列的默认值 20。插入默认值后,Oracle 将对定义在
deptno 列上的
FOREIGN KEY 进行完整性约束检查。 |
| 071 |
Figure 5-4 DEFAULT Column
Values |
图 5-4 DEFAULT 列值 |
| 072 | ||
| 073 |
|
另见: |
| 074 |
Partitioned Tables |
5.2.5 分区表 |
| 075 |
Partitioned tables allow your data to be broken down into smaller, more
manageable pieces called partitions, or even subpartitions. Indexes can
be partitioned in similar fashion. Each partition can be managed
individually, and can operate independently of the other partitions,
thus providing a structure that can be better tuned for availability and
performance. |
用户可以使用分区表(partitioned
table)将数据划分为更小,更易管理的单位,这种单位被称为分区(partition),分区还可以被继续划分为子分区(subpartition)。索引也可以采取类似方式进行分区。每个分区可以独立操作,独立管理,采用这种存储结构有利于提高系统的可用性和性能。 |
| 076 |
|
提示: |
| 077 |
|
|
| 078 |
Nested Tables |
5.2.6 嵌套表 |
| 079 |
You can create a table with a column whose datatype is another table.
That is, tables can be nested within other tables as values in a column.
The Oracle database server stores nested table data out of line from the
rows of the parent table, using a
store table that is associated with
the nested table column. The parent row contains a unique set identifier
value associated with a nested table instance. |
一个表的某列的数据类型(datatype)可以为另一个表。即一个表可以作为一个列值嵌套(nest)到另一个表中。Oracle
数据库将嵌套表(nested table)的数据存储在其父表(parent table)的数据行之外(out of line)的存储表(store table)内,并将其与对应的嵌套列(nested
table column)相关联。父表中的每行包含一个指向嵌套表对象(nested table instance)的标识。 |
| 080 |
|
|
| 081 |
Temporary Tables |
5.2.7 临时表 |
| 082 |
In addition to permanent tables, Oracle can create
temporary tables
to hold session-private data that exists only for the duration of a
transaction or session. |
除了永久表(permanent table)之外,Oracle
还可以在事务(session)或会话(transaction)期间创建保存会话私有数据的临时表(temporary table)。 |
| 083 |
The CREATE GLOBAL TEMPORARY TABLE statement
creates a temporary table that can be transaction-specific or
session-specific. For transaction-specific temporary tables, data exists
for the duration of the transaction. For session-specific temporary
tables, data exists for the duration of the session. Data in a temporary
table is private to the session. Each session can only see and modify
its own data. DML locks are not acquired on the data of the temporary
tables. The LOCK statement has no effect on
a temporary table, because each session has its own private data. |
使用 CREATE GLOBAL TEMPORARY TABLE
语句可以创建与事务相关的(transaction-specific)或与会话相关的(session-specific)临时表(temporary
table)。在与事务相关的临时表中,数据只存在于事务期间。而在与会话相关的临时表中,数据只存在于会话期间。临时表中的数据为一个会话所私有。每个会话只能查询与修改属于此会话的数据。对临时表数据进行
DML 操作时无需加锁(Lock)。LOCK
语句对临时表无效,因为每个会话只能操作其私有数据。 |
| 084 |
A TRUNCATE statement issued on a
session-specific temporary table truncates data in its own session. It
does not truncate the data of other sessions that are using the same
table. |
针对与会话相关的临时表(session-specific temporary table)执行的 TRUNCATE
语句只会清除(truncate)属于此会话的数据,而不会清除此临时表中属于其他会话的数据。 |
| 085 |
DML statements on temporary tables do not generate redo logs for the
data changes. However, undo logs for the data and redo logs for the undo
logs are generated. Data from the temporary table is automatically
dropped in the case of session termination, either when the user logs
off or when the session terminates abnormally such as during a session
or instance failure. |
对临时表(temporary table)的 DML
操作不会产生数据修改的重做日志(redo log),但是将产生被修改数据的撤销记录(undo log),及撤销记录的重做日志(redo
log)。会话结束(terminate)后其存储于临时表中的数据将被自动地清除。上述的会话结束既包括用户退出系统(log
off),也包括由于会话或实例故障导致的会话异常终止。 |
| 086 |
You can create indexes for temporary tables using the
CREATE INDEX statement. Indexes created on
temporary tables are also temporary, and the data in the index has the
same session or transaction scope as the data in the temporary table. |
用户可以使用 CREATE INDEX 语句为临时表(temporary
table)创建索引。创建在临时表上的索引也是临时的,索引数据的生存周期与临时表内数据的生存周期相同。 |
| 087 |
You can create views that access both temporary and permanent tables.
You can also create triggers on temporary tables. |
用户可以创建同时访问永久表(permanent table)与临时表(temporary
table)的视图。用户还可以在临时表上创建触发器(trigger)。 |
| 088 |
Oracle utilities can export and import the definition of a temporary
table. However, no data rows are exported even if you use the
ROWS clause. Similarly, you can replicate
the definition of a temporary table, but you cannot replicate its data. |
Oracle 提供的工具可以导出/导入(export/import)临时表的定义,但是无法导出其中的数据,在工具中使用
ROWS
子句也是无效的。同样用户可以复制(replicate)临时表的定义,但不能复制其数据。 |
| 089 |
Segment Allocation |
5.2.7.1 临时表的段分配 |
| 090 |
Temporary tables use temporary segments. Unlike permanent tables,
temporary tables and their indexes do not automatically allocate a
segment when they are created. Instead, segments are allocated when the
first INSERT (or
CREATE TABLE AS SELECT) is performed. This means that if a
SELECT, UPDATE,
or DELETE is performed before the first
INSERT, then the table appears to be empty. |
临时表(temporary table)使用临时段(temporary segment)存储数据。与永久表(permanent
table)不同,Oracle 在创建临时表及临时索引时并不会为其分配段(segment),段是在第一次执行
INSERT(或
CREATE TABLE AS SELECT)语句时进行分配。在发生首次 INSERT
之前执行的 SELECT,UPDATE,或 DELETE
语句操作的是一个空表。 |
| 091 |
You can perform DDL statements (ALTER TABLE,
DROP TABLE, CREATE
INDEX, and so on) on a temporary table only when no session is
currently bound to it. A session gets bound to a temporary table when an
INSERT is performed on it. The session gets
unbound by a TRUNCATE, at session
termination, or by doing a COMMIT or
ROLLBACK for a transaction-specific
temporary table. |
当没有会话(session)与临时表(temporary table)绑定(bound)的时候,用户才能够对其执行 DDL 操作(ALTER TABLE,DROP TABLE,CREATE
INDEX 等)。对临时表执行 INSERT
语句时,会话将和此临时表绑定。在会话结束时对临时表执行的 TRUNCATE
语句将解除(unbound)会话与此临时表的绑定。对于与事务相关的(transaction-specific)临时表,执行 COMMIT
或
ROLLBACK 将解除会话与此临时表的绑定。 |
| 092 |
Temporary segments are deallocated at the end of the transaction for
transaction-specific temporary tables and at the end of the session for
session-specific temporary tables. |
在事务(transaction)结束时与事务相关的(transaction-specific)临时表(temporary
table)所使用的临时段(temporary
segment)将被回收。同样的,在会话(session)结束时与会话相关的(session-specific)临时表所使用的临时段也将被回收。 |
| 093 |
|
另见: |
| 094 |
Parent and Child Transactions |
5.2.7.2 父事务与子事务 |
| 095 |
Transaction-specific temporary tables are accessible by user
transactions and their child transactions. However, a given
transaction-specific temporary table cannot be used concurrently by two
transactions in the same session, although it can be used by
transactions in different sessions. |
与事务相关的(transaction-specific)临时表(temporary
table)中的数据可以被用户的事务(transaction)及子事务(child
transaction)访问。但是这些数据不能被同一会话(session)里的两个事务同时访问。不同会话中的事务可以同时使用同一个事务相关的临时表。 |
| 096 |
If a user transaction does an INSERT into
the temporary table, then none of its child transactions can use the
temporary table afterward. |
如果用户事务(user transaction)对临时表(temporary table)执行了
INSERT 操作,在此之后此事务的子事务(child transaction)将不能使用这个临时表。 |
| 097 |
If a child transaction does an INSERT into
the temporary table, then at the end of the child transaction, the data
associated with the temporary table goes away. After that, either the
user transaction or any other child transaction can access the temporary
table. |
如果在子事务(child transaction)中对临时表(temporary table)执行了
INSERT 操作,临时表中已有的数据将被清除。子事务结束后,父事务(parent
transaction)及其他子事务对此临时表访问权利将被恢复。 |
| 098 |
External Tables |
5.2.8 外部表 |
| 099 |
External tables access data in external sources as if it were in a table
in the database. You can connect to the database and create metadata for
the external table using DDL. The DDL for an external table consists of
two parts: one part that describes the Oracle column types, and another
part (the access parameters) that describes the mapping of the external
data to the Oracle data columns. |
用户可以使用外部表(external
table)技术,将位于数据库外部的数据源(external source)作为一个数据库表,以便访问其中的数据。用户连接到数据库后可以使用 DDL
语句创建外部表的元数据(metadata)。这种 DDL 语句由两部分构成:一部分描述外部表各列的数据类型,另一部分(数据访问参数(access parameter))描述外部数据与 Oracle 数据列的映射方式(mapping)。 |
| 100 |
An external table does not describe any data that is stored in the
database. Nor does it describe how data is stored in the external
source. Instead, it describes how the external table layer needs to
present the data to the server. It is the responsibility of the access
driver and the external table layer to do the necessary transformations
required on the data in the datafile so that it matches the external
table definition. |
外部表(external table)不会用来描述存储于数据库内的数据,也不会描述数据是如何在外部数据源(external
source)中存储的。她的作用是规定了外部表层(external table
layer)将外部数据展现给数据库服务器(server)的格式。而外部表层和数据访问驱动(access
driver)的职责是将外部数据文件(datafile)内的数据进行适当转换(transformation),使之符合外部表的定义。 |
| 101 |
External tables are read only; therefore, no DML operations are
possible, and no index can be created on them. |
外部表(external table)是只读的,因此不能对外部表进行增删改等操作。外部表上也不能建立索引。 |
| 102 |
The Access Driver |
5.2.8.1 数据访问驱动 |
| 103 |
When you create an external table, you specify its type. Each type of
external table has its own access driver that provides access parameters
unique to that type of external table. The access driver ensures that
data from the data source is processed so that it matches the definition
of the external table. |
当用户创建外部表(external table)时,需要指定她的类型(type)。每种类型的外部表都有专用的数据访问驱动(access
driver)及针对此驱动的数据访问参数(access parameter)。数据访问驱动将对外部数据源(external data
source)的数据进行处理使之符合外部表的定义。 |
| 104 |
In the context of external tables, loading data refers to the act of
reading data from an external table and loading it into a table in the
database. Unloading data refers to the act of reading data from a table
in the database and inserting it into an external table. |
外部表(external
table)的数据加载(loading data)是指从外部表中读取数据并加载到数据库表中。而反向数据加载(unloading
data)是指从数据库表中读取数据并存储到外部表中。 |
| 105 |
The default type for external tables is
ORACLE_LOADER, which lets you read table data from an external
table and load it into a database.
Oracle also provides the
ORACLE_DATAPUMP type, which lets you unload
data (that is, read data from a table in the database and insert it into
an external table) and then reload it into an Oracle database. |
外部表(external table)的默认类型是
ORACLE_LOADER,用户可以使用这种类型从外部表中读取数据并加载到数据库中。Oracle 还提供了
ORACLE_DATAPUMP
类型,用户可以使用这种类型先对外部表进行反向加载(unload data),再将其中的数据加载到另一个数据库中。 |
| 106 |
The definition of an external table is kept separately from the
description of the data in the data source. This means that:
|
创建外部表(external table)时,外部表的定义(definition)与外部数据源中(external data
source)数据的描述是分开的。这样做的目的是:
|
| 107 |
Data Loading with External Tables |
5.2.8.2 使用外部表进行数据加载 |
| 108 |
The main use for external tables is to use them as a row source for
loading data into an actual table in the database. After you create an
external table, you can then use a CREATE TABLE AS
SELECT or INSERT INTO ... AS SELECT
statement, using the external table as the source of the
SELECT clause. |
外部表(external table)的主要用途是作为数据源,以便将其中的数据加载到实际的数据表中。当用户创建了外部表后,就可以在 CREATE TABLE AS
SELECT 或 INSERT INTO ... AS SELECT
的
SELECT 子句中使用此外部表了。 |
| 109 |
|
提示: |
| 110 |
When you access the external table through a SQL statement, the fields
of the external table can be used just like any other field in a regular
table. In particular, you can use the fields as arguments for any SQL
built-in function, PL/SQL function, or Java function. This lets you
manipulate data from the external source. For data warehousing, you can
do more sophisticated transformations in this way than you can with
simple datatype conversions. You can also use this mechanism in data
warehousing to do data cleansing. |
当用户通过 SQL
语句访问外部表时,表内的各字段(field)可以像普通表内的字段一样使用。用户也可以将外部表内的字段作为 SQL 内置函数,PL/SQL
函数,及 Java
函数的参数使用。在数据仓库环境中,用户可以使用函数对数据进行更为复杂的转换(transformation),而不仅仅是数据类型转换(datatype
conversion);用户还可以利用函数进行数据清洗(data cleansing)。 |
| 111 |
While external tables cannot contain a column object, constructor
functions can be used to build a column object from attributes in the
external table. |
外部表(external table)中无法包含列对象(column
object),但是可以使用外部表中的字段(field)作为属性(attribute)以供构造函数(constructor
function)创建列对象。 |
| 112 |
Parallel Access to External Tables |
5.2.8.3 外部表的并行访问 |
| 113 |
After the metadata for an external table is created, you can query the
external data directly and in parallel, using SQL. As a result, the
external table acts as a view, which lets you run any SQL query against
external data without loading the external data into the database. |
当外部表(external table)的元数据(metadata)被定义后,用户就能够以直接(directly)或并行(parallel)的
SQL 查询(query)其中的数据。因此,外部表和视图(view)类似,用户无需将外部数据(external data)引入数据库就能使用
SQL 对其进行查询。 |
| 114 |
The degree of parallel access to an external table is specified using
standard parallel hints and with the PARALLEL clause. Using parallelism
on an external table allows for concurrent access to the datafiles that
comprise an external table. Whether a single file is accessed
concurrently is dependent upon the access driver implementation, and
attributes of the datafile(s) being accessed (for example, record
formats). |
对外部表(external table)访问的并行度(degree of parallel)可以在
PARALLEL 子句中使用标准的并行提示(parallel
hint)来设定。对外部表进行并行访问是指并发地(concurrently)访问构成外部表的数据文件(datafile)。是否对单一文件(single
file)进行并行访问取决于数据访问驱动(access
driver)的实现方式(implementation),及被访问数据文件(datafile)的属性(attribute)(例如,数据文件中的记录格式(record
format))。 |
| 115 |
|
|
| 116 |
Overview of Views |
5.3 视图概述 |
| 117 |
A view is a tailored presentation of the data contained in one or more
tables or other views. A view takes the output of a query and treats it
as a table. Therefore, a view can be thought of as a stored query or a
virtual table. You can use views in most places where a table can be
used. |
视图(view)用于展现整理后(tailored)的一个或多个表或其他视图中的数据。视图将一个查询的结果作为一个表来使用。因此视图可以被看作是存储的查询(stored query)或一个虚拟表(virtual table)。大多数情况下,能够使用表就能使用视图。 |
| 118 |
For example, the employees table has
several columns and numerous rows of information. If you want users to
see only five of these columns or only specific rows, then you can
create a view of that table for other users to access. |
例如,employees 表由数列(column)构成,且存储了数行(row)数据。如果管理员希望用户只能查询其中的 5 列及特定的数据行,就可以创建此表的视图供用户访问。 |
| 119 |
Figure 5-5 shows an example of a view called
staff derived from the
base table
employees. Notice that the view shows only
five of the columns in the base table. |
图5-5 显示了一个来源于(derived from)employees 基表(base table)的视图 staff。注意此视图只显示了基表中的 5 列。 |
| 120 |
Figure 5-5 An Example of a
View |
图 5-5 视图的例子 |
| 121 | ||
| 122 |
|
图 5-5 显示了一个具有 7 列的基表(base table)employees。一个来源于基表,但只有 5 列的视图 staff。 |
| 123 |
Because views are derived from tables, they have many similarities. For
example, you can define views with up to 1000 columns, just like a
table. You can query views, and with some restrictions you can update,
insert into, and delete from views. All operations performed on a view
actually affect data in some base table of the view and are subject to
the integrity constraints and triggers of the base tables. |
由于视图来源于表,因此二者有许多相似之处。例如,用户定义的视图和表一样最多包含 1000
列。用户可以查询(query)视图,遵从某些限制(restriction)时还可以对视图进行更新(update),插入(insert),删除(delete)等操作。所有对视图数据的修改最终都会被反映到视图的基表(base
table)中,这些修改必须服从基表的完整性约束(integrity
constraint),并同样会触发定义在基表上的触发器(trigger)。 |
| 124 |