Ask the Expert

Alternate way to alter table

I created a table with 10 columns and have about 100,000 records in it. I want to alter the structure of the table. Currently Oracle has the alter table add columns option and it works fine. I want to do this in a different way. I want to export the table's data, delete the data in the existing table, alter the table and then import it. Can I do this in Oracle 10g? If so, how?

    Requires Free Membership to View

To me, the best way to add a column is to do the following:
ALTER TABLE table_name ADD (column_name datatype);
I'm not sure why you do not want to use the above command as it works great! However, you have another option at your disposal with slightly more work. This option involves copying the data to another table, re-creating the original table and moving the data back:
CREATE TABLE my_table_interim AS SELECT * FROM my_table;
DROP TABLE my_table;
CREATE TABE my_table ( ....);
INSERT INTO my_table (col1, col2 ...) 
  SELECT col1, col2, ... FROM my_table_interim;
DROP TABLE my_table_interim;
In the INSERT statement above, you will have to explictily denote which columns you are copying from the interim table to the destination table since both tables will have a different number of columns.

This was first published in September 2006

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: