Mariadb 更改列类型时出现奇怪的语法错误
我正在尝试更改2列的类型。第一个命令有效,但第二个命令给出了同一命令的语法错误:Mariadb 更改列类型时出现奇怪的语法错误,mariadb,Mariadb,我正在尝试更改2列的类型。第一个命令有效,但第二个命令给出了同一命令的语法错误: > show full columns from KernelParams; +-------+------------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+ | Field | Type | Colla
> 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是一个保留字,所以你需要引用列名,就像奥塔在评论中说的那样。MySQL和MariaDB中的表和列引号字符是回勾(`) 这与预期的效果一样:
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)
desc是MySQL中的保留字吗?尝试将其包装为单引号,修改“desc”varchar(128),将额外的引号添加到错误文本中。这只是猜测,我从未使用过MySQL,但在Oracle中,desc列出了表中的列,或表示desc(结束)排序,我的想法是MySQL可能正在做类似的事情,所以你需要以某种方式转义列名。MySQL也有desc。但是“”似乎并没有使解析器变得更智能。
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)