添加字段并附值
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);1SQLselect*fromv$version;23BANNER4--------------------------------------------------------------------------------5OracleDatabase11g Enterprise Edition Release11.2.
0.1.0-64bitProduction6PL/SQL Release11.2.
0.1.0-Production7CORE11.2.
0.1.0Production8TNSforLinux: Version11.2.
0.1.0-Production9NLSRTL Version11.2.
0.1.0-Production
1.修改字段数据类型时,如果该列有数据则报ORA-01439: column to be modified must be empty to change datatype,此时需要通过另外一种方法修改:1SQLaltertablezytaddid_tempvarchar2(10);23Tablealtered.45SQLcommit;67Commitcomplete.89SQLselect*fromzyt;1011NAME ID ID_TEMP12---------- ---------- ----------13zyt1114david21516SQLaltertablezyt renamecolumnidtoid_bak;1718Tablealtered.1920SQLselect*fromzyt;2122NAME ID_BAK ID_TEMP23---------- ---------- ----------24zyt1125david22627SQLdesczyt;28NameNull? Type29----------------------------------------- -------- ----------------------------30NAMEVARCHAR2(10)31ID_BAKNOTNULLNUMBER(2)32ID_TEMPVARCHAR2(10)3334SQLupdatezytsetID_TEMP=cast(ID_BAKasvarchar2(10));35362rows updated.3738SQLcommit;3940Commitcomplete.4142SQLselect*fromzyt;4344NAME ID_BAK ID_TEMP45---------- ---------- ----------46zyt11147david224849SQLaltertablezytdropcolumnID_BAK;5051Tablealtered.5253SQLcommit;5455Commitcomplete.5657SQLselect*fromzyt;5859NAME ID_TEMP60---------- ----------61zyt1162david26364SQLdesczyt;65NameNull? Type66----------------------------------------- -------- ----------------------------67NAMEVARCHAR2(10)68ID_TEMPVARCHAR2(10)备注:这种方法能满足需求,因新增字段默认添加到表末尾,有可能发生行迁移,对应用程序会产生影响,同时也涉及复杂数据,不算最好的方法
2.建立一个中间跳板,临时存储数据1SQLdesczyt;2NameNull? Type3----------------------------------------- -------- ----------------------------4NAMEVARCHAR2(10)5IDVARCHAR2(10)67SQLselect*fromzyt;89NAME ID10---------- ----------11zyt1112david21314SQLaltertablezytaddid_tempVARCHAR2(10)1516Tablealtered.1718SQLselect*fromzyt;1920NAME ID ID_TEMP21---------- ---------- ----------22zyt1123david22425SQLupdatezytsetID_TEMP=id,id=null;26272rows updated.2829SQLselect*fromzyt;3031NAME ID ID_TEMP32---------- ---------- ----------33zyt1134david23536SQLaltertablezyt modify idnumber(10);3738Tablealtered.3940SQLdesczyt;41NameNull? Type42----------------------------------------- -------- ----------------------------43NAMEVARCHAR2(10)44IDNUMBER(10)45ID_TEMPVARCHAR2(10)4647SQLupdatezytsetid=ID_TEMP,ID_TEMP=null;48492rows updated.5051SQLselect*fromzyt;5253NAME ID ID_TEMP54---------- ---------- ----------55zyt1156david25758SQLaltertablezytdropcolumnID_TEMP;5960Tablealtered.6162SQLcommit;6364Commitcomplete.6566SQLselect*fromzyt;6768NAME ID69---------- ----------70zyt1171david27273SQLdesczyt;74NameNull? Type75----------------------------------------- -------- ----------------------------76NAMEVARCHAR2(10)77IDNUMBER(10)备注:第二种方法,是增加一个与被修改的列类型一样的列,之后将要修改列的数据复制到新增的列并置空要修改的列,之后修改数据类型,再从新增列将数据拷贝回来,该过程涉及两次数据复制,如果是数据量很多,会比较慢同时也会产生很多undo和redo;优点是数据不会发生行迁移。