-2

I have a table AA which it's definition is:

CREATE TABLE `AA` (
   `a` int(11) NOT NULL,
   `b` int(11) NOT NULL,
   `c` int(11) NOT NULL,
   PRIMARY KEY  (`a`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1

I would like to add a column to the table however I want this column to be between b and `c, If I would create the table now it would have been like:

CREATE TABLE `AA` (
   `a` int(11) NOT NULL,
   `b` int(11) NOT NULL,
   `ba` int(11) NOT NULL,
   `c` int(11) NOT NULL,
   PRIMARY KEY  (`a`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1

The thing is that the table already contains data so I can not drop and recreate it. When I use alter table add column it adds the column to the end.

How do I insert a new column in specific position?

M0rtiis
  • 3,665
  • 1
  • 13
  • 22
aayushdagra
  • 89
  • 1
  • 3
  • 13

3 Answers3

4

You can use AFTER. See the mysql ALTER TABLE syntax for more details

ALTER TABLE AA ADD COLUMN c int AFTER b

If you wanted to add it at the beginning you would use FIRST.

Explosion Pills
  • 183,406
  • 48
  • 308
  • 385
0

Easy with alter table

ALTER table table_name
    Add column columnName integer AFTER columnOtherName
Juan Carlos Oropeza
  • 45,789
  • 11
  • 74
  • 113
0
ALTER TABLE `AA` ADD `ba` INT NOT NULL AFTER `b`
M0rtiis
  • 3,665
  • 1
  • 13
  • 22