I have a table with 50+ columns and I need to swap the order of the first two columns. What is the best way to accomplish this using Oracle? Assume the table name is ORDERDETAILS and as it is, the first two columns are ITEM_ID and ORDER_ID. Once the rename is complete, the table name should still be ORDERDETAILS but the first two columns will be ORDER_ID and ITEM_ID. FWIW, column types and the rest of the columns and their order is irelevent.
Correct me if I'm wrong, but I think the general steps are:
- Rename the existing table.
- Drop the primary key constraint.
- Re-create the table with the correct column order.
- List item
- Run INSERT INTO .. SELECT to move the data from temp to the table in step #3.
- Drop the temp table.
I have little experience with Oracle so perhaps I'm missing a step or two.
Does a primary key imply an index in Oracle? Does dropping the primary key also drop the index?
SQL examples are much appreciated.
EDIT: Un-sincere thanks to those who question why it needs done instead of providing help. To answer your question as to why it needs done: I'm following orders from someone else who says I need to do it this way and the order of the columns DOES matter. My thoughts/opinions on this are irrelevent.
Look at the package DBMS_Redefinition. It will rebuild the table with the new ordering. It can be done with the table online.
As Phil Brown noted, think carefully before doing this. However there is overhead in scanning the row for columns and moving data on update. Column ordering rules I use (in no particular order):
These rules conflict and have not all been tested for performance on the latest release. Most have been tested in practice, but I didn't document the results. Placement options target one of three conflicting goals: easy to understand column placement; fast data retrieval; and minimal data movement on updates.
It's sad that Oracle doesn't allow this, I get asked to do this by developers all the time..
Here's a slightly dangerous, somewhat quick and dirty method:
CREATE TABLE table_right_columns AS SELECT column1 column3, column2 FROM table_wrong_columns; -- Notice how we correct the position of the columns :)
DROP TABLE table_wrong_columns;
And next time you create a table, please consider the future requirements! ;)
Since the release of Oracle 12c it is now easier to rearrange columns logically.
Oracle 12c added support for making columns invisible and that feature can be used to rearrange columns logically.
Quote from the documentation on invisible columns:
Example
Create a table:
Add a column:
Move the column to the middle:
DESCRIBE t;
Credits
I learned about this from an article by Tom Kyte on new features in Oracle 12c.
Use the View for your efforts in altering the position of the column: CREATE VIEW CORRECTED_POSITION AS SELECT co1_1, col_3, col_2 FROM UNORDERDED_POSITION should help.
This requests are made so some reports get produced where it is using SELECT * FROM [table_name]. Or, some business has a hierarchy approach of placing the information in order for better readability from the back end.
Thanks Dilip