Oracle 11g中 ADD COLUMN 功能增强 说明(一)

2014-11-24 12:41:51 · 作者: · 浏览: 2
一. Enhanced ADD COLUMN 说明
在Oracle 11gR1中,Oracle 对add column 进行了增强。 官网的说明地址:
http://docs.oracle.com/cd/B28359_01/server.111/b28279/chapter1.htm#NEWFTCH1

1.1 Enhanced ADD COLUMN Functionality
Default valuesof columns are maintained in the data dictionary for columns specifiedas NOT NULL.
--当列指定为not null,那么该列对应的默认值在数据字典中进行维护。

Adding newcolumns with DEFAULT values and NOT NULL constraint nolonger requires the default value to be stored in all existing records. Thisnot only enables a schema modification in sub-seconds and independent of theexisting data volume, it also consumes no space.
--添加一列,该列不空,且有默认值,在11g中不在需要存储这个默认在所有的记录中,该默认值单独保存在数据字典里,在使用时,在从数据字典中调用,从而减少了DDL操作的时间,也减少了空间的使用。

1.2 Adding Table Columns
To add a columnto an existing table, use the ALTERTABLE...ADD statement.
The followingstatement alters the hr.admin_emp table to add a new columnnamed bonus:
ALTER TABLE hr.admin_emp
ADD (bonus NUMBER (7,2));

If a new columnis added to a table, the column is initially NULL unless you specifythe DEFAULT clause. When you specify a default value, the databaseimmediately updates each row with the default value.
--如果对表添加一个新列,那么在不指定default 值的情况下,该列初始化为NULL。 当我们指定默认值后,数据会立即更新该表中的所有记录。

Note that thiscan take some time, and that during the update, there is an exclusive DML lockon the table. For some types of tables (for example, tables without LOBcolumns), if you specify both a NOT NULL constraint and adefault value, the database can optimize the column add operation and greatlyreduce the amount of time that the table is locked for DML.
--注意,这个更新操作可能需要很多时间,并且在表上还会添加一个排它锁。

You can add acolumn with a NOT NULL constraint only if the table does notcontain any rows, or you specify a default value.
--我们仅可以在表中没有记录或者指定默认值的情况下才可以使用NOT NULL 限制。

1.3 说明
通过上面的说明,对add column 有了一定的了解。在Oracle 11g 以前,如果我们要添加一列,且该列不为空,那么需要指定默认值,如果表非常大,那么在执行时,在添加列之后,更新表中所有的记录,添加新的默认值。这样会花费很长的时间,同时也会产生大量的redo log。 所以在11g以前添加带默认值的列需要在DB 相对空闲时进行。

在Oracle 11g对add column功能进行了增强,在上述情况下,11g中不会更新表中所有的记录,而是将默认值保存到数据字典里。 当用户查询该列的记录时,在从数据字典(sys.col$.default$)中获取默认值。 这样做可以减少系统的开销。

注意:
在第一添加列是,会同时更新ecol$和col$ 字典,但是如果以后修改这个默认值,就仅修改col$中的值,我们以后的查询也是从col$中获取,而ecol$中,则永远保存的是我们第一次赋予的default值。

MOS上与该功能相关的一个BUG 说明:
Wrong Result For Added Column After TableCreation in 11g [ID 1106553.1]

二.示例

2.1 add column 操作示例
SQL> select * from v$version;

BANNER
----------------------------------------------------------------------
Oracle Database 11g Enterprise EditionRelease 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 32-bit Windows: Version 11.2.0.1.0- Production
NLSRTL Version 11.2.0.1.0 – Production

SQL> create table t1(id number,namevarchar2(20));
Table created.
SQL> insert into t1 values(1,'dave');
1 row created.
SQL> insert into t1 values(2,'anqing');
1 row created.
SQL> insert into t1values(3,'huaining');
1 row created.

SQL> commit;
Commit complete.

SQL> select * from t1;
ID NAME
---------- -----------------
1 dave
2 anqing
3 huaining

在执行add column 之前,我们启用10046 事件跟踪一下这个过程:

SQL> oradebug setmypid
Statement processed.
SQL> oradebug event 10046 trace name context forever,level 8;
Statement processed.

--执行操作
SQL> alter table t1 add tel varchar2(20)de