Friday, April 17, 2009

Change column datatype in Oracle table (ALTER TABLE)

I had to change my column from USER_ID NUMBER(22) to USERNAME VARCHAR2 (20 BYTE)Oracle does not allow to modify column when there is data in the table. Also, you can not rename the column.

Finally I came up with the following script:
  1. ALTER TABLE: add new column (USERNAME) with desired datatype.
  2. UPDATE: copy data to the new column
  3. ALTER TABLE: drop the original column (USER_ID)
I heard you can rename a column in Ms SQL.

No comments:

Post a Comment

Thank you for your feedback