Modify the field type of a table with data in oracle

[Data type conversion will be involved when modifying, be careful]
When modifying the length of a column, you can only edit a length larger than the actual length of the existing field, otherwise the following error will be prompted:
  ORA-01441: The column length cannot be reduced, Because some values ​​are too large,
here is a summary of data type conversion in oracle:
Oracle data type conversion rules:
(1) When comparing, generally character type is converted to numeric type, and character type is converted to date type
(2) During arithmetic operations , generally convert character type to numerical type, character type to date type
(3) When connecting (||), generally convert numerical type to character type, date type to character type
(4) When assigning and calling functions , whichever is the defined variable type.


The first method [recommended by me, more practical in production] (use auxiliary fields)
{experimental environment preparation}
create table t_person(
id varchar2(200) primary key,
name varchar2(200),
address varchar2(200)
);

desc t_person ;
SQL> desc t_person
Name Type Nullable Default Comments
------- ------------- -------- ------- ----- ---
ID VARCHAR2(200)                          
NAME VARCHAR2(200) Y                       
ADDRESS VARCHAR2(200) Y 

insert into t_person(id,name,address) values(sys_guid(),'zhangsan','beijing');
insert into t_person(id,name,address) values(sys_guid (),'lisi','shangqiu');

want to change the varchar2 type of address to clob type

(1)

alter table t_person rename column address to myaddress;

change the original field name, address to myaddress

(2)

alter table t_person add address clob;

add an original field name address to the table and define the type as the type you want to change, here is clob

(3)

update t_person set address = myaddress;

add the backup myaddress field content to the new field address

(4)
alter table t_person drop column myaddress;

delete the backup field myaddress

SQL> desc t_person
Name Type Nullable Default Comments
------- ------------- -------- ------- --------
ID VARCHAR2 (200)                          
NAME VARCHAR2(200) Y                       
ADDRESS CLOB Y

The second method (using auxiliary table)
[Experimental environment simulation]
create table TABLE1
(
  col1 number(9),
  col2 char(20)
);
//try to modify
ALTER TABLE TABLE1 MODIFY COL1 NUMBER(10); --Modify successfully because there is no data in the table

//Insert data
INSERT INTO TABLE1 (COL1, COL2) VALUES (1, 'aaa');
//Try to modify
ALTER TABLE TABLE1 again MODIFY COL2 VARCHAR2( 20);--Modification failed, because there is already data in the table, direct operation is not allowed
//Create auxiliary table T_TABLE1
CREATE TABLE T_TABLE1 AS SELECT * FROM TABLE1;
//Delete all data in the original table
truncate table TABLE1;
//Modify field
ALTER TABLE TABLE1 MODIFY COL1 NUMBER(9);--Modify successful
ALTER TABLE TABLE1 MODIFY COL2 VARCHAR2(20);--Modify successfully
//Insert original data
INSERT INTO TABLE1 SELECT * FROM T_TABLE1;
//Delete the cache table
DROP TABLE T_TABLE1;
//Note that if it is a CHAR type, the insufficient number of digits will be automatically filled with spaces, so use the CHAR type with caution, and use TRIM() to verify whether there are any conditions record of.
//In the initial table structure, SELECT * FROM TABLE1 WHERE COL2 = 'aaa'; there is no data, you need SELECT * FROM TABLE1 WHERE TRIM(COL2) = 'aaa';

5. Add a comment to the table

  comment column on table name. Column name is 'comment content'; //modify the comment of the column of the table

  COMMENT ON TABLE MOVO_NEW.TEST_SAKTE IS 'comment content'; //modify the comment of the table

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326797012&siteId=291194637