Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the data type of a column in Oracle

Tags:

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, county and city from varchar(10) to varchar(20). How do I do that?

like image 351
user1232622 Avatar asked Apr 25 '12 18:04

user1232622


People also ask

Can we change data type in Oracle?

Oracle will allow you to modify data types on existing columns in an existing table where there is already data, as long as the existing data is compatible with the new data type.

Can we change datatype of column?

We can use ALTER TABLE ALTER COLUMN statement to change the datatype of the column. The syntax to change the datatype of the column is the following. In the syntax, Tbl_name: Specify the table name that contains the column that you want to change.

What command is used to change the data type of a column?

The ALTER COLUMN command is used to change the data type of a column in a table.


2 Answers

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

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

like image 71
Justin Cave Avatar answered Dec 09 '22 12:12

Justin Cave


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.

For rename a column use below:

ALTER TABLE <table_name> rename column <column_name> to <new_column_name> 
like image 23
Ali Tofigh Avatar answered Dec 09 '22 14:12

Ali Tofigh