ALTER TABLE-重命名列


102

这真让我发疯!

我想将博客表中的列从read-more重命名为read_more

我尝试了所有这些:

  ALTER TABLE blog RENAME COLUMN read-more to read_more;
  ALTER TABLE blog CHANGE COLUMN 'read-more' 'read_more' VARCHAR(255) NOT NULL;

我总是得到这个!

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 'COLUMN read-more to read_more' at line 1

我正在使用MySQL 5.5

Answers:


139

有效语法接近第二次尝试,但是您需要使用反引号而不是单引号对列名进行转义:

ALTER TABLE `blog` CHANGE COLUMN `read-more` `read_more` VARCHAR(255) NOT NULL;

18
ALTER TABLE `blog` CHANGE  `read-more` `read_more` VARCHAR(255) NOT NULL;

上面提到的查询是正确的,如果使用的是mysql数据库,则无需使用“列”关键字和表名和列名的引号:

ALTER TABLE blog CHANGE read-more read_more VARCHAR(255) NOT NULL;

3

这对我有用:

ALTER TABLE vm_list CHANGE `vm_notes]` vm_notes VARCHAR(255); 

Query OK, 0 rows affected (0.01 sec) 
Records: 0  Duplicates: 0  Warnings: 0

是的,我以某种方式在那里找到了一个名为“ vm_notes]”的列。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.