Q

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?

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?
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
This Content Component encountered an error

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close