在sql语句中使用关键字

时间:2023-03-08 23:45:38
在sql语句中使用关键字

背景

开发过程中遇到了遇到了一句sql语句一直报错,看了一下字段名和表名都对应上了,但是还是一直报错

sql语句如下:

update table set using = "hh" where id = 2

报错信息:

[Err] 1064 - 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 'table set using = ""hh where id = 2' at line 1

分析

经过分析,我的sql语句中有关键字 table 和 using ,所以是执行失败的,在sql语句中如果表名和字段名有关键字的话,要用Tab键上面那个键(``)引用一下。

正确写法:

update `table` set `using` = "hhh" where id = 2

总结

避免在字段名和表名使用关键字,如果一定得使用的话要加引用符号

附上mysql5.7保留关键字官方文档:

https://dev.mysql.com/doc/refman/5.7/en/keywords.html