在 Oracle 中更改列的数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10321775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 04:09:15  来源:igfitidea点击:

Changing the data type of a column in Oracle

oracleoracle10goracle11g

提问by user1232622

I created the following table

我创建了下表

CREATE TABLE PLACE(
  POSTCODE VARCHAR(10) PRIMARY KEY,
  STREET_NAME VARCHAR(10),
  COUNTY VARCHAR(10),
  CITY VARCHAR(10));  

I want to change the name, countyand cityfrom varchar(10)to varchar(20). How do I do that?

我想将name,countycity从更改varchar(10)varchar(20)。我怎么做?

回答by Justin Cave

ALTER TABLE place
  MODIFY( street_name VARCHAR2(20),
          county      VARCHAR2(20),
          city        VARCHAR2(20) )

Note that I am also changing the data type from VARCHARto VARCHAR2to be more conventional. There is no functional difference at present between the two though the behavior of VARCHARmay change in the future to match the SQL standard.

请注意,我还将数据类型从VARCHAR更改VARCHAR2为更常规。尽管VARCHAR未来可能会更改行为以匹配 SQL 标准,但目前两者之间没有功能差异。

回答by Ali Tofigh

if you want to change only type of column use below:

如果您只想更改下面的列使用类型:

ALTER TABLE <table_name> MODIFY (<column_name> <new_Type>)
in your case:    
ALTER TABLE place MODIFY (street_name VARCHAR2(20),
                          county      VARCHAR2(20),
                          city        VARCHAR2(20))

If your table has data you could act below:

如果您的表有数据,您可以执行以下操作:

  1. add a column with new type to table.
  2. copy data from old column to new column.
  3. drop old column.
  4. rename new column to old.
  1. 将新类型的列添加到表中。
  2. 将数据从旧列复制到新列。
  3. 删除旧列。
  4. 将新列重命名为旧列。

For rename a column use below:

要重命名列,请使用以下内容:

ALTER TABLE <table_name> rename column <column_name> to <new_column_name>

回答by harit

Oracle 10G and later

Oracle 10G 及更高版本

ALTER TABLE table_name
MODIFY column_name datatype;

回答by barsha

Alter table placemodify(street name varchar2(20),city varchar2(20)

回答by Satyam Annu

You can't modify the data type of a table if you have some amount of records already present in the table.

如果表中已经存在一定数量的记录,则无法修改表的数据类型。

You have to empty the table records of the column (you want to modify the data type) first and then use the below command :

您必须先清空列的表记录(要修改数据类型),然后使用以下命令:

alter table place
modify ( street_name varchar2(20), country varchar2(20), city varchar2(20) );

Definitely it will work!

肯定会奏效的!