Methods for modifying database fields

Source: Internet
Author: User

-- 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.