更改列类型时出现奇怪的语法错误

时间:2015-03-05 14:13:06

标签: mariadb

我试图更改2列的类型。第一个工作,但第二个给出相同命令的语法错误:

> show full columns from KernelParams;
+-------+------------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type             | Collation         | Null | Key | Default | Extra          | Privileges                      | Comment |
+-------+------------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| id    | int(10) unsigned | NULL              | NO   | PRI | NULL    | auto_increment | select,insert,update,references |         |
| param | varchar(256)     | latin1_swedish_ci | YES  | UNI | NULL    |                | select,insert,update,references |         |
| desc  | varchar(256)     | latin1_swedish_ci | YES  |     | NULL    |                | select,insert,update,references |         |
+-------+------------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+

> ALTER TABLE KernelParams MODIFY param varchar(128);
Query OK, 6 rows affected (0.08 sec)               
Records: 6  Duplicates: 0  Warnings: 0

> ALTER TABLE KernelParams MODIFY desc varchar(128);
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 'desc varchar(128)' at line 1

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

DESC是一个保留字,所以你需要引用列名,就像OTTA在评论中所说的那样。 MySQL和MariaDB中的表和列引用字符是反引号(`)

ALTER TABLE KernelParams MODIFY `desc` varchar(128);

这可以按预期工作:

MariaDB [test]> describe new_table;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| idnew_table | int(11)     | NO   | PRI | NULL    |       |
| desc        | varchar(45) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)

MariaDB [test]> ALTER TABLE new_table MODIFY `desc` varchar(128);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test]> describe new_table;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| idnew_table | int(11)      | NO   | PRI | NULL    |       |
| desc        | varchar(128) | YES  |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
2 rows in set (0.02 sec)