Home  >  Article  >  How to change column type in oracle

How to change column type in oracle

小老鼠
小老鼠Original
2023-07-11 15:38:524339browse

Oracle's method of changing the column type is: 1. When the field has no data or the new type to be modified is compatible with the original type, you can directly modify it; 2. When the field has data and uses the new type to be modified and When the original types are incompatible, new fields must be created indirectly for transfer.

How to change column type in oracle

The operating environment of this tutorial: Windows 7 system, Laravel version 5.7, Dell G3 computer.

How to change column type in oracle?

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

2 . Assuming that the field has data, change it to nvarchar2(20) and execute it directly:

alter table tb modify (name nvarchar2(20));

3. Assume that the field has data, change it to When executing for varchar2(40), it will pop up: "ORA-01439: To change the data type, 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 is name_tmp*/

alter table tb rename column name to name_tmp;

/*Add a field name 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);

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

alter table tb drop column name_tmp;

Summary:

1. When the field has no data or needs to be modified When the new type is compatible with the original type, it can be modified directly.

2. When a field has data and the new type to be modified is incompatible with the original type, create a new field indirectly for transfer.

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

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