I have building MYSQL queries with backticks. For example,
我构建了带有回勾的MYSQL查询。例如,
SELECT `title` FROM `table` WHERE (`id` = 3)
as opposed to:
而不是:
SELECT title FROM table WHERE (id = 3)
I think I got this practice from the Phpmyadmin exports, and from what I understood, even Rails generates its queries like this.
我认为我从phpadmin导出中得到了这个实践,并且根据我的理解,甚至Rails也会生成这样的查询。
But nowadays I see less and less queries built like this, and also, the code looks messier and more complicated with backticks in queries. Even with SQL helper functions, things would be simpler without them. Hence, I'm considering to leave them behind.
但是现在,我看到越来越少的查询像这样构建,而且,代码看起来更混乱,而且在查询中有回调更复杂。即使有SQL helper函数,没有它们也会更简单。因此,我正考虑把他们抛在脑后。
I wanted to find out if there is other implication in this practice such as SQL (MySQL in my case) interpretation speed, etc. What do you think?
我想知道在这个实践中是否还有其他含义,比如SQL(在我的例子中是MySQL)解释速度等等。
6 个解决方案
#1
16
Backticks also allow spaces and other special characters (except for backticks, obviously) in table/column names. They're not strictly necessary but a good idea for safety.
在表/列名称中,回勾还允许空格和其他特殊字符(显然除了回勾)。它们并不是绝对必要的,而是安全的好主意。
If you follow sensible rules for naming tables and columns backticks should be unnecessary.
如果您遵循合理的命名表和列的规则,那么应该不需要回勾。
#2
15
Every time I see this discussed, I try to lobby for their inclusion, because, well, the answer is hidden in here already, although wryly winked away without further thought. When we mistakenly use a keyword as a field or table name, we can escape confusion by various methods, but only the keenly aware back-tick ` allows an even greater benefit!!!
每次我看到这个讨论,我都试图游说他们加入,因为答案已经藏在这里了,尽管我苦笑着眨了眨眼睛,没有进一步思考。当我们错误地使用一个关键字作为字段或表名时,我们可以通过各种方法来避免混淆,但是只有敏锐的后勾选“允许更大的好处!!!”
Every word in a sql statement is run through the entire keyword hash table to see if conflicts, therefore, you've done you query a great favor by telling the compiler that, hey, I know what I'm doing, you don't need to check these words because they represent table and field names. Speed and elegance.
每一个词在一个sql语句是贯穿整个关键字哈希表是否冲突,因此,你所做的你查询一个大忙,告诉编译器,嘿,我知道我在做什么,你不需要检查这些话,因为他们代表表和字段的名称。速度和优雅。
Cheers, Brad
干杯,布拉德
#3
3
backticks are used to escape reserved keywords in your mysql query, e.g. you want to have a count
column—not that uncommon.
反勾符用于转义mysql查询中的保留关键字,例如,您希望有一个计数列——这并不罕见。
you can use other special characters or spaces in your column/table/db names
您可以在列/表/db名称中使用其他特殊字符或空格
they do not keep you safe from injection attacks (if you allow users to enter column names in some way—bad practice anyway)
它们并不能使您免受注入攻击(如果您允许用户以某种方式输入列名——无论如何,这是不好的做法)
they are not standardized sql and will only work in mysql; other dbms will use "
instead
它们不是标准化的sql,只能在mysql中使用;其他dbms将使用“替代”。
#4
2
Well, if you ensure that you never accidentally use a keyword as an identifier, you don't need the backticks. :-)
好吧,如果您确保您不会意外地使用关键字作为标识符,那么您不需要回勾。:-)
#5
1
My belief was that the backticks were primarily used to prevent erroneous queries which utilized common SQL identifiers, i.e. LIMIT and COUNT.
我认为回勾主要用于防止使用常见SQL标识符(即LIMIT和COUNT)的错误查询。
#6
1
You read the documentation on identifiers at http://dev.mysql.com/doc/refman/5.6/en/identifiers.html
您可以在http://dev.mysql.com/doc/refman/5.6/en/identifiers.html上阅读有关标识符的文档
SQL generators will often include backticks, as it is simpler than including a list of all MySQL reserved words. To use any1 sequence of BMP Unicode characters except U+0000 as an identifier, they can simply
SQL生成器通常包括backticks,因为它比包含所有MySQL保留字的列表更简单。要使用除U+0000之外的BMP Unicode字符的任意1序列作为标识符,可以简单地使用
- Replace all backticks with double backticks
- 用双背勾代替所有的背勾
- Surround that with single backticks
- 用一个背勾围起来
When writing handmade queries, I know (most of) MySQL's reserved words, and I prefer to not use backticks where possible as it is shorter and IMO easier to read.
在编写手工查询时,我知道(大部分)MySQL的保留词,而且我更喜欢尽可能不使用回勾,因为它更短,而且在我看来更容易阅读。
Most of the time, it's just a style preference -- unless of course, you have a field like date
or My Field
, and then you must use backticks.
大多数时候,这只是一种风格偏好——当然,除非你有一个像date或My这样的字段,然后你必须使用backtick。
1. Though see https://bugs.mysql.com/bug.php?id=68676
1。尽管参见https://bugs.mysql.com/bug.php?id=68676
#1
16
Backticks also allow spaces and other special characters (except for backticks, obviously) in table/column names. They're not strictly necessary but a good idea for safety.
在表/列名称中,回勾还允许空格和其他特殊字符(显然除了回勾)。它们并不是绝对必要的,而是安全的好主意。
If you follow sensible rules for naming tables and columns backticks should be unnecessary.
如果您遵循合理的命名表和列的规则,那么应该不需要回勾。
#2
15
Every time I see this discussed, I try to lobby for their inclusion, because, well, the answer is hidden in here already, although wryly winked away without further thought. When we mistakenly use a keyword as a field or table name, we can escape confusion by various methods, but only the keenly aware back-tick ` allows an even greater benefit!!!
每次我看到这个讨论,我都试图游说他们加入,因为答案已经藏在这里了,尽管我苦笑着眨了眨眼睛,没有进一步思考。当我们错误地使用一个关键字作为字段或表名时,我们可以通过各种方法来避免混淆,但是只有敏锐的后勾选“允许更大的好处!!!”
Every word in a sql statement is run through the entire keyword hash table to see if conflicts, therefore, you've done you query a great favor by telling the compiler that, hey, I know what I'm doing, you don't need to check these words because they represent table and field names. Speed and elegance.
每一个词在一个sql语句是贯穿整个关键字哈希表是否冲突,因此,你所做的你查询一个大忙,告诉编译器,嘿,我知道我在做什么,你不需要检查这些话,因为他们代表表和字段的名称。速度和优雅。
Cheers, Brad
干杯,布拉德
#3
3
backticks are used to escape reserved keywords in your mysql query, e.g. you want to have a count
column—not that uncommon.
反勾符用于转义mysql查询中的保留关键字,例如,您希望有一个计数列——这并不罕见。
you can use other special characters or spaces in your column/table/db names
您可以在列/表/db名称中使用其他特殊字符或空格
they do not keep you safe from injection attacks (if you allow users to enter column names in some way—bad practice anyway)
它们并不能使您免受注入攻击(如果您允许用户以某种方式输入列名——无论如何,这是不好的做法)
they are not standardized sql and will only work in mysql; other dbms will use "
instead
它们不是标准化的sql,只能在mysql中使用;其他dbms将使用“替代”。
#4
2
Well, if you ensure that you never accidentally use a keyword as an identifier, you don't need the backticks. :-)
好吧,如果您确保您不会意外地使用关键字作为标识符,那么您不需要回勾。:-)
#5
1
My belief was that the backticks were primarily used to prevent erroneous queries which utilized common SQL identifiers, i.e. LIMIT and COUNT.
我认为回勾主要用于防止使用常见SQL标识符(即LIMIT和COUNT)的错误查询。
#6
1
You read the documentation on identifiers at http://dev.mysql.com/doc/refman/5.6/en/identifiers.html
您可以在http://dev.mysql.com/doc/refman/5.6/en/identifiers.html上阅读有关标识符的文档
SQL generators will often include backticks, as it is simpler than including a list of all MySQL reserved words. To use any1 sequence of BMP Unicode characters except U+0000 as an identifier, they can simply
SQL生成器通常包括backticks,因为它比包含所有MySQL保留字的列表更简单。要使用除U+0000之外的BMP Unicode字符的任意1序列作为标识符,可以简单地使用
- Replace all backticks with double backticks
- 用双背勾代替所有的背勾
- Surround that with single backticks
- 用一个背勾围起来
When writing handmade queries, I know (most of) MySQL's reserved words, and I prefer to not use backticks where possible as it is shorter and IMO easier to read.
在编写手工查询时,我知道(大部分)MySQL的保留词,而且我更喜欢尽可能不使用回勾,因为它更短,而且在我看来更容易阅读。
Most of the time, it's just a style preference -- unless of course, you have a field like date
or My Field
, and then you must use backticks.
大多数时候,这只是一种风格偏好——当然,除非你有一个像date或My这样的字段,然后你必须使用backtick。
1. Though see https://bugs.mysql.com/bug.php?id=68676
1。尽管参见https://bugs.mysql.com/bug.php?id=68676