Home > Database > Oracle > How to modify table field type in oracle

How to modify table field type in oracle

青灯夜游
Release: 2022-04-18 12:02:14
Original
52931 people have browsed it

In Oracle, you can use the "alter table" statement to modify the table field type. The function of this statement is to change the structure of the original table. The syntax is "alter table table name modify (field name new type name); "statement.

How to modify table field type in oracle

The operating environment of this tutorial: Windows 7 system, Oracle 11g version, Dell G3 computer.

Modify table field data type

There is a table named tb, a field segment named name, and a data type of nchar(20).

1. Assuming that the field data is empty, no matter what field type is changed, you can directly execute:

alter table tb modify (name nvarchar2(20));
Copy after login
Copy after login

2. Assuming that the field has data, then change It can be executed directly for nvarchar2(20):

alter table tb modify (name nvarchar2(20));
Copy after login
Copy after login

3, Assuming that the field has data, it will pop up when executing varchar2(40): "ORA-01439: To If the data type is changed, the column to be modified must be empty." In this case, the following method should be used to solve this problem:

/*Modify the original field name to name_tmp*/

alter table tb rename column name to name_tmp;
Copy after login

/ *Add a field name with the same name as the original field name*/

alter table tb add name varchar2(40);
Copy after login

/*Update the original field name_tmp data to the added field name*/

update tb set name=trim(name_tmp);
Copy after login

/*After updating, delete the original field name_tmp*/

alter table tb drop column name_tmp;
Copy after login

3. Add fields and delete fields

    --添加新的字段

    alter table lyz_emp add(e_age number(3));

    --添加多个新的字段

    alter table lyz_emp add(

    e_age number(3),

    e_age_tmp,varchar2(20));

    --获取表字段:

    select *

    from user_tab_columns

    where Table_Name='lyz_emp'

    order by column_name

    --删除表中的字段

    alter table lyz_emp drop column e_age;
Copy after login

Recommended tutorial: "Oracle Tutorial"

The above is the detailed content of How to modify table field type in oracle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template