Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert columns at a specific position in existing table?

Tags:

sql

mysql

I created a table with 85 columns but I missed one column. The missed column should be the 57th one. I don't want to drop that table and create it again. I'm looking to edit that table and add a column in the 57th index.

I tried the following query but it added a column at the end of the table.

ALTER table table_name Add column column_name57 integer 

How can I insert columns into a specific position?

like image 373
PathmanKIP Avatar asked Jan 24 '14 10:01

PathmanKIP


People also ask

How do you add a column in a table at a particular 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.

How do I add a column to a specific position in hive?

ALTER TABLE table_name ADD COLUMNS (user_id BIGINT); Now to make user_id column as the first column in your table use change column with FIRST clause: ALTER TABLE table_name CHANGE COLUMN user_id user_id BIGINT first; This will move the user_id column to the first position.


1 Answers

ALTER TABLE by default adds new columns at the end of the table. Use the AFTER directive to place it in a certain position within the table:

ALTER table table_name     Add column column_name57 integer AFTER column_name56 

From mysql doc

To add a column at a specific position within a table row, use FIRST or AFTERcol_name. The default is to add the column last. You can also use FIRST and AFTER in CHANGE or MODIFY operations to reorder columns within a table.

http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

I googled for this for PostgreSQL but it seems to be impossible.

like image 68
AmazingDreams Avatar answered Sep 22 '22 17:09

AmazingDreams