Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-order columns of table in Oracle

Tags:

sql

rename

oracle

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:

  1. Rename the existing table.
  2. Drop the primary key constraint.
  3. Re-create the table with the correct column order.
  4. List item
  5. Run INSERT INTO .. SELECT to move the data from temp to the table in step #3.
  6. 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.

like image 434
Ryan Rodemoyer Avatar asked Feb 08 '11 23:02

Ryan Rodemoyer


People also ask

How do I rearrange columns in Oracle SQL Developer?

Instead, just right-click on the column headers, select 'Columns', and reorder as desired. Then move up the columns you want to see first… Put them in the order you want – it won't affect the database.

How can the order of columns in a table be changed?

In Object Explorer, right-click the table with columns you want to reorder and select Design. Select the box to the left of the column name that you want to reorder. Drag the column to another location within the table.

How do I rearrange columns in Toad?

Go to the Columns Tab and from there you can arrange the Columns by dragging them up or down. When you are done arranging the columns, click on the Script Tab, which will show you what Toad will run in the background.

Does column order matter in Oracle?

The datatype and length of the column have an impact on the return we can get from index compression but not on the best order of columns in an index.


1 Answers

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:

When you make an invisible column visible, the column is included in the table's column order as the last column.

Example

Create a table:

CREATE TABLE t (     a INT,     b INT,     d INT,     e INT ); 

Add a column:

ALTER TABLE t ADD (c INT); 

Move the column to the middle:

ALTER TABLE t MODIFY (d INVISIBLE, e INVISIBLE); ALTER TABLE t MODIFY (d VISIBLE, e VISIBLE); 

DESCRIBE t;

Name ---- A B C D E 

Credits

I learned about this from an article by Tom Kyte on new features in Oracle 12c.

like image 105
Jonas Meller Avatar answered Sep 20 '22 22:09

Jonas Meller