链接:http://www.cnblogs.com/david-zhang-index/archive/2012/04/10/2441015.html
对字段操作 | 操作方法 |
更新字段名 | alter table TABLE_NAME rename column column_old to column_new; |
添加字段 | alter table TABLE_NAME add COLUMN_NAME varchar(10); |
删除字段 | alter table TABLE_NAME drop column COLUMN_NAME; |
添加字段并附值 | alter table TABLE_NAME ADD COLUMN_NAME NUMBER(1) DEFAULT 1; |
修改字段值 | update TABLE_NAME set filedname=value where filedname=value; |
修改字段数据类型 | alter table tablename modify filedname varchar2(20); |
SQL> select * from v$version; BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
1.修改字段数据类型时,如果该列有数据则报ORA-01439: column to be modified must be empty to change datatype,此时需要通过另外一种方法修改:
SQL> alter table zyt add id_temp varchar2(10); Table altered. SQL> commit; Commit complete. SQL> select * from zyt; NAME ID ID_TEMP
---------- ---------- ----------
zyt1 1
david 2 SQL> alter table zyt rename column id to id_bak; Table altered. SQL> select * from zyt; NAME ID_BAK ID_TEMP
---------- ---------- ----------
zyt1 1
david 2 SQL> desc zyt;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
ID_BAK NOT NULL NUMBER(2)
ID_TEMP VARCHAR2(10) SQL> update zyt set ID_TEMP = cast(ID_BAK as varchar2(10)); 2 rows updated. SQL> commit; Commit complete. SQL> select * from zyt; NAME ID_BAK ID_TEMP
---------- ---------- ----------
zyt1 1 1
david 2 2 SQL> alter table zyt drop column ID_BAK; Table altered. SQL> commit; Commit complete. SQL> select * from zyt; NAME ID_TEMP
---------- ----------
zyt1 1
david 2 SQL> desc zyt;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
ID_TEMP VARCHAR2(10)
备注:这种方法能满足需求,因新增字段默认添加到表末尾,有可能发生行迁移,对应用程序会产生影响,同时也涉及复杂数据,不算最好的方法
2.建立一个中间跳板,临时存储数据
SQL> desc zyt;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
ID VARCHAR2(10) SQL> select * from zyt; NAME ID
---------- ----------
zyt1 1
david 2 SQL> alter table zyt add id_temp VARCHAR2(10) Table altered. SQL> select * from zyt; NAME ID ID_TEMP
---------- ---------- ----------
zyt1 1
david 2 SQL> update zyt set ID_TEMP=id,id=null; 2 rows updated. SQL> select * from zyt; NAME ID ID_TEMP
---------- ---------- ----------
zyt1 1
david 2 SQL> alter table zyt modify id number(10); Table altered. SQL> desc zyt;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
ID NUMBER(10)
ID_TEMP VARCHAR2(10) SQL> update zyt set id=ID_TEMP,ID_TEMP=null; 2 rows updated. SQL> select * from zyt; NAME ID ID_TEMP
---------- ---------- ----------
zyt1 1
david 2 SQL> alter table zyt drop column ID_TEMP; Table altered. SQL> commit; Commit complete. SQL> select * from zyt; NAME ID
---------- ----------
zyt1 1
david 2 SQL> desc zyt;
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(10)
ID NUMBER(10)
备注:第二种方法,是增加一个与被修改的列类型一样的列,之后将要修改列的数据复制到新增的列并置空要修改的列,之后修改数据类型,再从新增列将数据拷贝回来,该过程涉及两次数据复制,如果是数据量很多,会比较慢同时也会产生很多undo和redo;优点是数据不会发生行迁移。