更改列类型时出现奇怪的语法错误 [英] Strange syntax error when changing type of a column

查看:18
本文介绍了更改列类型时出现奇怪的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 2 列的类型.第一个有效,但第二个给出了相同命令的语法错误:

I'm trying to change the type of 2 columns. The first works but the second gives a syntax error for the same command:

> 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

有什么想法吗?

推荐答案

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

DESC is a reserved word, so you need to quote the column name, like OTTA said in their comment. The table and column quoting character in MySQL and MariaDB is the backtick (`)

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)

这篇关于更改列类型时出现奇怪的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆