Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle move column to the first position

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 problem because some of our PHP Scripts are using the first column as an identifier (one Abstract Model with more than 100 other Models using this base object...)

See also:

  • In Oracle, is it possible to “insert” a column into a table?
  • Inserting new columns in the middle of a table?
like image 830
opHASnoNAME Avatar asked Dec 01 '09 08:12

opHASnoNAME


People also ask

Can we change position of column in Oracle?

For certain tables, you can change the order in which columns are displayed by clicking and dragging a column heading to a new position. In other tables, you can use the Reorder Columns dialog to change the position of the columns.

How do I move a column to the first column in SQL?

[FIRST | AFTER col_name]: FIRST or AFTER can be used to change a column's order. FIRST is used if we want to make our column the first column of a table, AFTER keyword is used if we're going to place our column after a specific column (col_name).

How do I change the position of a column 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 do I add a column to a specific position in Oracle?

The position of the column cannot be specifically given. If you need a column list in a certain order, then use a view. SELECT * FROM table_x ; new_col will appear last.


2 Answers

In Oracle 12c it is now easier to rearrange columns logically. It can be achived by making column invisible/visible.If you change a invisible column to visible , the column will appear last in the odering.

Consider Using Invisible Columns

Create wxyz table:

CREATE TABLE t ( w INT,
y VARCHAR2, z VARCHAR2, x VARCHAR2

);

rearrange the x column to the middle:

ALTER TABLE wxyz MODIFY (y INVISIBLE, z INVISIBLE); ALTER TABLE wxyz MODIFY (y VISIBLE, z VISIBLE);

DESCRIBE wxyz;

Name

w

x

y

z

like image 80
Sulaha Avatar answered Oct 13 '22 03:10

Sulaha


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.

like image 21
Vincent Malgrat Avatar answered Oct 13 '22 04:10

Vincent Malgrat