当前位置:Gxlcms > 数据库问题 > Oracle中表列由VARCHAR2类型改成CLOB

Oracle中表列由VARCHAR2类型改成CLOB

时间:2021-07-01 10:21:17 帮助过:18人阅读

原来表中的列定义成VARCHAR2类型,众所周知,VARCHAR2类型最大支持长度为4000。假设因为业务须要。想把此列转换为CLOB类型,在Oracle中直接通过ALTER语句转换是行不通的。以下依据详细事例解说在Oracle数据库中怎样把表列由VARCHAR2类型转换为CLOB类型。


演示样例准备

1. 新建两张张表TB_WITHOUT_DATA(此VARCHAR2列不包括数据)和TB_WITH_DATA(此Varchar2列包括数据)

  1. create table TB_WITHOUT_DATA
  2. (
  3. id NUMBER,
  4. name VARCHAR2(100),
  5. description VARCHAR2(2000)
  6. );


  1. create table TB_WITH_DATA
  2. (
  3. id NUMBER,
  4. name VARCHAR2(100),
  5. description VARCHAR2(2000)
  6. );
  7. insert into TB_WITH_DATA VALUES (1,'David Louis','He is capable of resolving such kind of issue');
  8. insert into TB_WITH_DATA VALUES (2,'German Noemi','She is very beatiful and charming');
  9. insert into TB_WITH_DATA VALUES (3,'Oliver Queen','He is main actor in the Green Arrow');
  10. insert into TB_WITH_DATA VALUES (4,'Mark Williams','He plays snooker very well');
  11. insert into TB_WITH_DATA VALUES (5,'Sita Rama Raju Kata','I do not know this guy');
  12. insert into TB_WITH_DATA VALUES (6,'Promethus','This is a very nice movie');
  13. commit;

错误方法

  1. ALTER TABLE TB_WITHOUT_DATA MODIFY description clob;
错误信息:

SQL Error: ORA-22858: invalid alteration of datatype
22858. 00000 -  "invalid alteration of datatype"
*Cause:    An attempt was made to modify the column type to object, REF,
           nested table, VARRAY or LOB type.
*Action:   Create a new column of the desired type and copy the current
           column data to the new type using the appropriate type
           constructor.



解决方法

 方法一:对于此列没有数据的可通过下面方法改动-首先把该列改成Long类型,然后再改成clob类型

  1. alter table TB_WITHOUT_DATA modify description long;--首先改成Long类型
  2. alter table TB_WITHOUT_DATA modify description clob;--在Long类型的基础上改成clob类型

注:对于此列已经存在数据的。不能通过此方法。否则会报例如以下错误:

  1. alter table TB_WITH_DATA modify description long;--更改包括数据的列

  1. SQL Error: ORA-01439: column to be modified must be empty to change datatype
  2. 01439. 00000 - "column to be modified must be empty to change datatype"


方法二:此方法适合此列包括数据和此列不包括数据两种情况

步骤一:把原来表中该列重命名

  1. alter table TB_WITHOUT_DATA rename column description to description_bak;
  2. alter table TB_WITH_DATA rename column description to description_bak;

步骤二:在表中添加该列,并指定改列类型为clob

  1. alter table TB_WITHOUT_DATA add description clob;
  2. alter table TB_WITH_DATA add description clob;

步骤三:对此列包括数据的须要包数据从步骤一重命名列中拷出(对于此列没有数据的此步骤省略)

  1. update TB_WITH_DATA set description=description_bak;
  2. commit;

步骤四:删除步骤一中的备份列

  1. alter table TB_WITHOUT_DATA drop column description_bak;
  2. alter table TB_WITH_DATA drop column description_bak;

步骤五:验证

1) 表结构验证

  1. DESC TB_WITHOUT_DATA
  2. Name Null Type
  3. ----------- ---- -------------
  4. ID NUMBER
  5. NAME VARCHAR2(100)
  6. DESCRIPTION CLOB

  1. DESC TB_WITH_DATA
  2. Name Null Type
  3. ----------- ---- -------------
  4. ID NUMBER
  5. NAME VARCHAR2(100)
  6. DESCRIPTION CLOB
2) 数据验证

  1. select * from TB_WITH_DATA;
  2. ID NAME DESCRIPTION
  3. ---------- -------------------------- ------------------------------------------------
  4. 1 David Louis He is capable of resolving such kind of issue
  5. 2 German Noemi She is very beatiful and charming
  6. 3 Oliver Queen He is main actor in the Green Arrow
  7. 4 Mark Williams He plays snooker very well
  8. 5 Sita Rama Raju Kata I do not know this guy
  9. 6 Promethus This is a very nice movie
  10. 6 rows selected


方法三:此方法适合此列包括数据和此列不包括数据两种情况

在解说方法三之前,须要包表恢复到准备阶段,因为时间关系,直接通过drop然后re-create方法,脚本例如以下:

  1. drop table TB_WITHOUT_DATA;
  2. drop table TB_WITH_DATA;
  3. create table TB_WITHOUT_DATA
  4. (
  5. id NUMBER,
  6. name VARCHAR2(100),
  7. description VARCHAR2(2000)
  8. );
  9. create table TB_WITH_DATA
  10. (
  11. id NUMBER,
  12. name VARCHAR2(100),
  13. description VARCHAR2(2000)
  14. );
  15. insert into TB_WITH_DATA VALUES (1,'David Louis','He is capable of resolving such kind of issue');
  16. insert into TB_WITH_DATA VALUES (2,'German Noemi','She is very beatiful and charming');
  17. insert into TB_WITH_DATA VALUES (3,'Oliver Queen','He is main actor in the Green Arrow');
  18. insert into TB_WITH_DATA VALUES (4,'Mark Williams','He plays snooker very well');
  19. insert into TB_WITH_DATA VALUES (5,'Sita Rama Raju Kata','I do not know this guy');
  20. insert into TB_WITH_DATA VALUES (6,'Promethus','This is a very nice movie');
  21. commit;

步骤一:重命名两张表

  1. rename TB_WITHOUT_DATA to TB_WITHOUT_DATA_BAK;
  2. rename TB_WITH_DATA to TB_WITH_DATA_BAK;

步骤二:创建两张新表(通过下面语句创建两张表)

  1. create table TB_WITHOUT_DATA
  2. as
  3. select id, name, to_clob(description) description
  4. from TB_WITHOUT_DATA_BAK;
  5. create table TB_WITH_DATA
  6. as
  7. select id, name, to_clob(description) description
  8. from TB_WITH_DATA_BAK;

表结构与数据验证:

  1. desc TB_WITHOUT_DATA
  2. Name Null Type
  3. ----------- ---- -------------
  4. ID NUMBER
  5. NAME VARCHAR2(100)
  6. DESCRIPTION CLOB
  7. desc TB_WITH_DATA
  8. Name Null Type
  9. ----------- ---- -------------
  10. ID NUMBER
  11. NAME VARCHAR2(100)
  12. DESCRIPTION CLOB
  13. select * from TB_WITH_DATA;
  14. select * from TB_WITH_DATA;
  15. ID NAME DESCRIPTION
  16. ---------- -------------------------- ------------------------------------------------
  17. 1 David Louis He is capable of resolving such kind of issue
  18. 2 German Noemi She is very beatiful and charming
  19. 3 Oliver Queen He is main actor in the Green Arrow
  20. 4 Mark Williams He plays snooker very well
  21. 5 Sita Rama Raju Kata I do not know this guy
  22. 6 Promethus This is a very nice movie
  23. 6 rows selected

步骤三:删除备份表:

  1. DROP TABLE TB_WITHOUT_DATA_BAK;
  2. DROP TABLE TB_WITH_DATA_BAK;

-----------------------------------------------------------------------------------------------------------------------------------------------------------

假设您们在尝试的过程中遇到什么问题或者我的代码有错误的地方。请给予指正。很感谢!

联系方式:david.louis.tian@outlook.com

版权@:转载请标明出处。

Oracle中表列由VARCHAR2类型改成CLOB

标签:备份   action   snippet   play   rac   步骤   drop   empty   不能   

人气教程排行