Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Alter Table Add Field Before or After a field already present

Tags:

php

mysql

I have this, but it doesn't work:

$query = "ALTER TABLE `".$table_prefix."posts_to_bookmark`              ADD `ping_status` INT( 1 ) NOT NULL BEFORE `onlywire_status`"; 

I appreciate it!

like image 357
atwellpub Avatar asked Jul 31 '10 19:07

atwellpub


People also ask

How do I add a column before a specific column in MySQL?

Syntax. The syntax to add a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name ADD new_column_name column_definition [ FIRST | AFTER column_name ]; table_name.

When you use ALTER TABLE to add a column the new column?

Syntax. The basic syntax of an ALTER TABLE command to add a New Column in an existing table is as follows. ALTER TABLE table_name ADD column_name datatype; The basic syntax of an ALTER TABLE command to DROP COLUMN in an existing table is as follows.


1 Answers

$query = "ALTER TABLE `" . $table_prefix . "posts_to_bookmark`            ADD COLUMN `ping_status` INT(1) NOT NULL            AFTER `<TABLE COLUMN BEFORE THIS COLUMN>`"; 

I believe you need to have ADD COLUMN and use AFTER, not BEFORE.

In case you want to place column at the beginning of a table, use the FIRST statement:

$query = "ALTER TABLE `" . $table_prefix . "posts_to_bookmark`           ADD COLUMN `ping_status` INT(1) NOT NULL            FIRST"; 

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

like image 73
Chris Avatar answered Sep 25 '22 13:09

Chris