Search code examples
sqlnetbeanssqlcommandalter

Altering data types within a table using SQL command ALTER


I have researched how to alter table data types and I understand how to do it but I cannot get it to work. I am trying to update my table Person within APP using the following command:

ALTER TABLE APP.PERSON ALTER uName varchar;

What I have tried so far:

  • using Modify but realised that, after I received errors, this should indeed be ALTER.
  • changing uName to UNAME.
  • changing it to a data type of varchar2.
  • adding in the size of the data type '(20)' and 'NOT NULL' at the end.

Where am I going wrong? I am using Netbeans 7.3 Beta 2 running on Mac OS X, this is all being done within the SQL Commands section of Netbeans, using Java-DB as my database if any of that matters.


Solution

  • It has already been answered here on SO. You basically need to create new column with desired datatype and delete the old one. However, if you take a look into Apache Derby doc, there is a SET DATA TYPE command, so try something like

    ALTER TABLE APP.PERSON ALTER UNAME SET DATA TYPE VARCHAR(30)
    

    EDIT

    If code above doesn't work, then you just have to recreate the column as I suggested before.

    ALTER TABLE APP.PERSON ADD COLUMN UNAME_NEW VARCHAR(30);
    UPDATE APP.PERSON SET UNAME_NEW = UNAME;
    ALTER TABLE APP.PERSON DROP COLUMN UNAME;
    RENAME COLUMN APP.PERSON.UNAME_NEW TO UNAME;