【问题标题】:deleting column containing white spaces in mysql删除mysql中包含空格的列
【发布时间】:2017-03-22 23:08:13
【问题描述】:

我想知道如何在 mysql 中删除包含空格的列。 尝试了以下,得到了以下异常:

alter table test17 drop column [added column2];
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 '[added column2]' at line 1

alter table test17 drop column 'added column2';
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 ''added column2'' at line 1

alter table test17 drop column (added column2);
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 '(added column2)' at line 1

谢谢:)

【问题讨论】:

  • 列名是否包含空格?

标签: mysql linux mysql-error-1064


【解决方案1】:

首先,带有空格的列名是 BAD PRACTICE。然后,如果您真的想删除一列,请改用反引号:

ALTER TABLE test17 DROP COLUMN `added_column2`;

【讨论】:

  • 是的,这是一个错误...谢谢:)
【解决方案2】:

试试:

alter table test17 drop column `added column2`;

请注意,引号字符向左倾斜。

顺便说一句,如果您在标识符名称中包含空格(例如函数、过程、表、列等),这是一种非常、非常不好的做法。

【讨论】:

  • 谢谢!初学者错误:/
  • 确实:-)。顺便说一句,如果您使用正常的命名方法(例如,ThisIsColumn_5 或类似的方法,仅使用字母数字字符和_,以字母开头),您可以避免将标识符包含在标题引号中。例如,在您的情况下,它看起来是:alter table test17 drop column added_column2;。最后,我的答案是第一个,但您选择将其他标记为答案...
  • 嗯,谢谢..我不知道我使用了其他人的答案(没有专栏),所以我认为您最终会选择实际使用的答案...
【解决方案3】:

您应该尝试使用反引号 ("`") 来引用您的列名。

ALTER TABLE test17 DROP COLUMN `added column2`;

没有COLUMN字

ALTER TABLE test17 DROP `added column2`;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 2020-12-03
    • 2016-04-02
    • 2019-09-28
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多