Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

mariadb - Strange syntax error when changing type of a column

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

Any ideas what is wrong there?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

This works as expected:

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...