Re-order columns of table in Oracle

前端 未结 5 786
粉色の甜心
粉色の甜心 2020-12-07 20:18

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 ORDERDE

5条回答
  •  情深已故
    2020-12-07 21:13

    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:

    1. Ensure you have enough space to copy the Table
    2. Note any Constraints, Grants, Indexes, Synonyms, Triggers, um.. maybe some other stuff - that belongs to a Table - that I haven't thought about?
    3. CREATE TABLE table_right_columns AS SELECT column1 column3, column2 FROM table_wrong_columns; -- Notice how we correct the position of the columns :)
    4. DROP TABLE table_wrong_columns;
    5. 'ALTER TABLE table_right_columns RENAME TO table_wrong_columns;`
    6. Now the yucky part: recreate all those items you noted in step 2 above
    7. Check what code is now invalid, and recompile to check for errors

    And next time you create a table, please consider the future requirements! ;)

提交回复
热议问题