MySQL INSERT-字段名称是否需要反引号/重音符号? [英] MySQL INSERT - Do field names require backtick/accent delimination?
问题描述
我对MySQL有点陌生(来自MS SQL),最近在尝试将值插入数据库时遇到了让我非常困惑的事情.
I am somewhat new to MySQL (come from MS SQL) and have recently come across something very confusing to me when trying to INSERT values into the database.
我通过PHP创建了INSERT SQL命令,但发现它在尝试执行时抛出了错误.
I created an INSERT SQL command via PHP yet found it threw an error when trying to execute.
INSERT INTO myTableName (first-name, last-name) VALUES ('Ted', 'Johnson')
以我的想法,上述字符串应该有效..但没有成功.我什至尝试直接在phpMyAdmin中使用它,但是出现相同的语法错误.
To my way of thinking the above string should have worked .. but it didn't. I even tried using it directly in phpMyAdmin but I got the same syntax error.
最后使它起作用的唯一事情是,如果我在SQL语句中用反引号"或重音"字符(键盘上的波浪号上的另一个字符)包围了字段名.例如...
The only thing that ended up making it work is if I surrounded the field names in the SQL statement by the "backtick" or "accent" character (the other character on the tilde key on the keyboard). For example ...
INSERT INTO myTableName(`first-name`, `last-name`) VALUES ('Ted', 'Johnson')
我从不知道在MySQL或MS SQL Server中这是必需的.我一直只列出字段名称而不用定界.有人遇到过吗?我正在使用MySQL 5.0.77.谢谢.
I have never known this to be necessary in MySQL or MS SQL Server. I have always just listed the field names without delimiting them. Has anyone run across this before? I am using MySQL 5.0.77. Thank you.
推荐答案
您的列名包含连字符,通常不是一个好主意,因为解析器可能会将其误解为first minus name
.反引号是引用列名以确保正确处理的MySQL方法.
Your column names contain hyphens, which is generally not a good idea, since the parser could misinterpret as first minus name
. The back-ticks are the MySQL method of quoting column names to ensure they're treated correctly.
如果您的列是first_name和last_name,则不需要反引号.
If your columns were first_name and last_name, the back-ticks would not be necessary.
这篇关于MySQL INSERT-字段名称是否需要反引号/重音符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!