1

到目前为止,我已经使用 PHP 使用 MySQL 的相同列字段和关键字来演示一个项目。今天再次使用 PhpMyAdmin 管理数据库时,我刚刚收到如下消息警告。

The column name 'order' is a MySQL reserved keyword.

我的项目仍在使用关键字order作为列名。

我的问题是what are the risks or ... when we're using the same keyword of MySQL as table column

4

2 回答 2

3

Risks? You have experienced the risk: the query doesn't run so you have to fix it.

For most keywords, there is little chance of a keyword being confused with an identifier and still producing a valid query. Of course, you could come up with some arcane examples.

The fix is to escape the identifiers. MySQL uses backticks for escaping. Other databases use double quotes or square braces. So, there is another risk: the more you use backticks, the less portable your code is.

For me, the major reason is writability and readability. The backtick key is, on my keyboard, way off on the upper left where my little pinky has to reach far for it. I find reading the resulting code more cumbersome with lots of arcane characters floating around. For clarity, avoid escaping identifiers. Avoiding reserved words and using only letters, numbers, and underscore is quite sufficient.

There is another risk . . . that you will confuse single quotes with backticks. Then your query might have a constant where you think it has a column value. This confusion is one of the most common MySQL problems in this community.

于 2014-06-12T11:05:50.193 回答
0

在查询中的 order 周围使用反引号(`)。

例子:

SELECT *
FROM TableName
WHERE `order`='someval'

边注:

反引号用于表和列标识符,但仅当标识符是 MySQL 保留关键字,或者标识符包含空白字符或超出限制集的字符时才需要,通常建议避免使用保留关键字作为列或尽可能使用表标识符,避免引用问题。

对于以下情况,需要使用反勾号:

SELECT id, `my name`, `another field` , `field,with,comma`
于 2014-06-12T11:02:01.303 回答