-- At work, we often encounter temporary needs to modify the table fields in the database.
-- Here I will introduce the methods and hope to help you.
Create table class_use
(
Id number (19 ),
Name varchar2 (50 ),
Birthday Date default sysdate,
Age number (19)
)
Select * from class_use -- for update
--- Use the preceding statement
Alter table CLASS_USE modify AGE varchar2 (19 );
--- The following error will be reported during execution
-- At work, we often encounter temporary needs to modify the table fields in the database.
-- Here I will introduce the methods and hope to help you.
--- I will introduce two methods below
-- 1. Back up the table
Create table class_user_temp as select * from class_use;
Select * from class_user_temp;
Update class_use c
Set c. age = null;
Select * from class_use
-- Execute again
Alter table CLASS_USE modify AGE varchar2 (19 );
Update CLASS_USE cc
Set cc. age = (select temp. age from class_user_temp temp
Where temp. id = cc. id );
--- 2. Add Fields
Alter table CLASS_USE add age1 number (19 );
Update CLASS_USE c
Set c. age1 = c. age;
Select * from CLASS_USE
Alter table CLASS_USE drop column age;
Alter table CLASS_USE rename column AGE1 to AGE;
Select * from CLASS_USE
--- I will introduce two methods below
-- 1. Back up the table
Create table class_user_temp as select * from class_use;
Select * from class_user_temp;
Update class_use c
Set c. age = null;
Select * from class_use
-- Execute again
Alter table CLASS_USE modify AGE varchar2 (19 );
Update CLASS_USE cc
Set cc. age = (select temp. age from class_user_temp temp
Where temp. id = cc. id );
--- 2. Add Fields
Alter table CLASS_USE add age1 number (19 );
Update CLASS_USE c
Set c. age1 = c. age;
Select * from CLASS_USE
Alter table CLASS_USE drop column age;
Alter table CLASS_USE rename column AGE1 to AGE;
Select * from CLASS_USE