Oracle field additions and deletions method summary

Source: Internet
Author: User
Tags anonymous

First, modify the syntax of the field: ALTER TABLE tablename modify (field name type [default Value][null/not null],....);
There is a table named TB, field segment named Name, data type nchar (20).
1. ALTER TABLE TB Modify (name Nvarchar2 (20));

2, if the field has data, then to VARCHAR2 (40) execution will pop up: "ORA-01439: To change the data type, the column to be modified must be empty", the following method to solve the problem:

/* Modify the original field name name to name_tmp*/
ALTER TABLE TB Rename column name to Name_tmp;

/* Add a field with the same name as the original field name*/
ALTER TABLE TB Add name VARCHAR2 (40);

/* Update the original field NAME_TMP data to the added field name*/
Update TB set Name=trim (NAME_TMP);

/* Finish updating, delete the original field name_tmp*/
ALTER TABLE TB drop column name_tmp;

Summarize:
1, when the field has no data, or to modify the new type and the original type is compatible, you can directly modify modification.
2. When the field has data and is incompatible with the new type and the original type to be modified, create a new field to transfer indirectly.

Second, new fields

Syntax for new field: ALTER TABLE tablename Add (field name type [default Value][null/not null],....);
1. Add a field:
ALTER TABLE test1 Add (name VARCHAR2 () default ' anonymous ' not NULL);

2. Add three fields using one SQL statement at a time:
ALTER TABLE test1 Add (name VARCHAR2 () default ' anonymous ' NOT NULL,
The age integer default is not NULL, Has_money number (9,2));

Third, delete the field
Syntax for deleting a field: ALTER TABLE tablename drop column field name
Delete multiple fields: ALTER TABLE tablename drop (field name 1, field name 2)

Oracle field additions and deletions method summary

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.