Goswin von Brederlow
Goswin von Brederlow

Reputation: 12332

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?

Upvotes: 0

Views: 538

Answers (1)

stwalkerster
stwalkerster

Reputation: 1808

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)

Upvotes: 2

Related Questions