MySQL Alter Table在已存在的字段之前或之后添加字段

时间:2010-07-31 19:07:19

标签: php mysql

我有这个,但它不起作用:

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

我很感激!

1 个答案:

答案 0 :(得分:147)

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

我认为您需要ADD COLUMN并使用AFTER,而不是BEFORE

如果您想将列放在表的开头,请使用FIRST语句:

$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