How Oracle modifies field types (2)

Source: Internet
Author: User

At the time of development, I encountered the need to change the field type of the data table from number to varchar, but the field has a value.

Use ALTER TABLE t-name modify CNAME newType;

Words, at that time on the internet did not find the right solution, very distressed!

Today in the blog Park to see the solution to this problem, very nice, share!

You cannot change the field type when the field you want to modify has a value :

two ways to solve the problem :

1, > Added a column, the column type is the same as the new type to be modified;

> assigns the value of the old column to the new column (forcing type conversions);

> Update tname Set Cname_new=cast (cname_old as new type)

> Delete old columns.

This method changes the position of the column in the table.

2, > added a column, the column type is the same as the old column;

> assigns the value of the old column to the new column, and the value of the old column becomes null;

Update tname set cname_new =cname_old;

> Change the field type of the old column (null at this time, can be modified)

ALTER TABLE tname modify Cname_old newType;

> assigns the value of the new column to the old column;

Update Tname set cname_old=cname_new;

> Delete new columns.

Related SQL statements:

View Oracle Version : SELECT * from V$version;

Update Field name : ALTER TABLE t-name rename column cname_old to cname_new;

new field : ALTER TABLE t-name add CNAME type;

add field and assign value : ALTER TABLE t-name add CNAME type default value;

Delete field : ALTER TABLE t-name drop column cname;

Modify field value : Update tname set Cname=value where ...

Modify field type : ALTER TABLE t-name modify CNAME type;

Reference: http://www.cnblogs.com/david-zhang-index/archive/2012/04/10/2441015.html

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.