Oracle move column to the first position

后端 未结 8 2025
借酒劲吻你
借酒劲吻你 2021-01-17 23:39

Is there any way to move an column in an Oracle table from last to first position? Someone has dropped the ID column, and recreated it. So now it is at the end, which is a p

相关标签:
8条回答
  • 2021-01-18 00:28

    the simplest way to modify the logical order of the columns of a table is to rename your table and create a view with the "right" column positions:

    ALTER TABLE your_table RENAME TO your_table_t;
    
    CREATE VIEW your_table AS SELECT <columns in the right order> FROM your_table_t;
    
    -- grants on the view (the same as the table)
    GRANT ** TO ** ON your_table;
    

    Your application will behave as if the columns were in the "right" position. You don't have to touch at the physical structure.

    0 讨论(0)
  • 2021-01-18 00:29

    The Oracle FAQ says:

    Oracle only allows columns to be added to the end of an existing table.

    You'd have to recreate your table.

    RENAME tab1 TO tab1_old;
    
    CREATE TABLE tab1 AS SELECT id, <the rest of your columns> FROM tab1_old;
    
    0 讨论(0)
提交回复
热议问题