重命名名为&#34的列;添加"在mysql中

时间:2014-08-29 15:58:57

标签: mysql rename alter

我从未想过这个含义,我创建了一个名为&#34的列;添加"在mysql中。 (显然,创建表时添加的是保留字并不是我的想法)虽然给出了每个查询,但结果却是一个问题。我已决定更改名称,现在我也无法更改名称。

我尝试了以下变化:无效

mysql> alter table ml_n1 m modify column m.add addmovie tinyint(4);                                 
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'm modify column m.add addmovie tinyint(4)' at line 1

mysql> alter table ml_n1 modify column add addmovie tinyint(4);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'add addmovie tinyint(4)' at line 1

mysql> alter table ml_n1 modify column 'add' 'addmovie tinyint(4)';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''add' 'addmovie tinyint(4)'' at line 1

mysql> alter table ml_n1 modify column 'add' addmovie tinyint(4);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''add' addmovie tinyint(4)' at line 1

mysql> alter table ml_n1 change add addmovie tinyint(4);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'add addmovie tinyint(4)' at line 1

mysql> alter table ml_n1 change 'add' addmovie tinyint(4);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''add' addmovie tinyint(4)' at line 1

有人会建议解决方法吗?

4 个答案:

答案 0 :(得分:2)

ALTER TABLE ml_n1 CHANGE COLUMN `add` addmovie TINYINT(4)

正如你暗示的那样;调用列add是有问题的,因为add是MySQL中的保留字,为了引用你需要用反引号转义它的列,`。

您需要CHANGE而不是MODIFY来更改列的名称。

N.B。来自docs

  

当您使用CHANGE或MODIFY时,column_definition必须包含数据类型和应该应用于新列的所有属性,而不是索引属性,例如PRIMARY KEY或UNIQUE。

我不会称之为解决方法。

答案 1 :(得分:2)

alter table ml_n1 change `add` addmovie tinyint(4);

答案 2 :(得分:2)

以下内容对您有用:

ALTER TABLE `ml_n1` CHANGE COLUMN `add` `addmovie` tinyint(4);

反向标记允许您引用包含保留字或空格等的列名。
我个人习惯在用MySQL编码时在表和列引用周围放置反引号。

答案 3 :(得分:2)

在列名称周围使用反引号

    mysql>alter table m1_n1 change `add` addmovie tinyint(4);
相关问题