在字段名称周围使用反引号
在阅读了一些关于一些 SQL 问题的答案和评论之后,还听说我的一个朋友在一个有禁止它们的政策的地方工作,我想知道在 MySQL 中的字段名称周围使用反引号是否有什么问题。
那是:
SELECT `id`, `name`, `anotherfield` ...
-- vs --
SELECT id, name, anotherfield ...
After reading a couple of answers and comments on some SQL questions here, and also hearing that a friend of mine works at a place which has a policy which bans them, I'm wondering if there's anything wrong with using backticks around field names in MySQL.
That is:
SELECT `id`, `name`, `anotherfield` ...
-- vs --
SELECT id, name, anotherfield ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用反引号允许您使用替代字符。 在查询编写中,这不是一个问题,但如果假设您可以只使用反引号,我会认为它可以让您摆脱诸如“
Which does course generated bad-named table”之类的可笑的东西。
如果你只是简明扼要的话,我不认为有什么问题,
您会注意到,如果您这样运行查询,则
返回的生成警告将带有反引号和完全限定的表名称。 因此,如果您使用查询生成功能和自动重写查询,反引号将使任何解析代码的内容不再那么混乱。
然而,我认为,他们不应该强制要求是否可以使用反引号,而应该有一个名称标准。 它解决了更多“实际”问题。
Using backticks permits you to use alternative characters. In query writing it's not such a problem, but if one assumes you can just use backticks, I would assume it lets you get away with ridiculous stuff like
Which does of course generate badly named tables.
If you're just being concise I don't see a problem with it,
you'll note if you run your query as such
The generated warning that comes back will have back-ticks and fully qualified table names. So if you're using query generation features and automated re-writing of queries, backticks would make anything parsing your code less confused.
I think however, instead of mandating whether or not you can use backticks, they should have a standard for names. It solves more 'real' problems.
反引号的唯一问题是它们不符合 ANSI-SQL,例如它们不能在 SQL Server 中工作。
如果您有可能必须将 SQL 移植到另一个数据库,请使用双引号。
The only problem with backticks is that they are not ANSI-SQL compliant, e.g. they don't work in SQL Server.
If there is a chance you would have to port your SQL to another database, use double quotes.
对我来说,在处理字段名称时始终使用它们很有意义。
To me it makes a lot of sense to use them at all times when dealing with field names.
反引号不是标准 ANSI SQL 的一部分。 来自 mysql 手册:
因此,如果您使用反引号,然后决定放弃 MySQL,那么您就会遇到问题(尽管您可能还有很多更大的问题)
Backticks aren't part of standard ANSI SQL. From the mysql manual:
So if you use backticks and then decide to move away from MySQL, you have a problem (although you probably have a lot bigger problems as well)
如果您继续使用 MYSQL,除了查询的视觉模糊性之外,没有任何问题。 但它们确实允许使用保留关键字或嵌入空格作为表名和列名。 对于大多数数据库引擎来说这是禁忌,并且会阻止以后的任何迁移。
为了方便阅读,很多人对 SQL 关键字使用大写,例如。
There isn't anything wrong if you keep using MYSQL, except maybe the visual fuziness of the queries. But they do allow the use of reserved keywords or embedded spaces as table and column names. This is a no-no with most database engines and will prevent any migration at a later time.
As for easy reading, many people use caps for SQL keywords, eg.
如果你问我,应该始终使用反引号。 但出于某些原因,团队可能不愿意使用它们。
优点:
缺点:
If you ask to me, backticks should always be used. But there are some reasons why a team may prefer not to use them.
Advantages:
Disadvantages:
在代码库中搜索反引号中的内容要容易得多。 假设您有一个名为
event
的表。grep -r "event" *
可能会返回数百个结果。grep -r "\`event\`" *
将返回任何可能引用您的数据库的内容。It's a lot easier to search your code-base for something in backticks. Say you have a table named
event
.grep -r "event" *
might return hundreds of results.grep -r "\`event\`" *
will return anything probably referencing your database.嗯,据我所知,使用反引号的全部目的是让您可以使用与保留关键字一致的名称。 因此,如果名称不与保留关键字冲突,我认为没有任何理由使用反引号。 但是,这也不是禁止它们的理由。
Well, as far as I know, the whole purpose of using backticks is so you can use names that coincide with reserved keywords. So, if the name isn't colliding with a reserved keyword, I don't see any reason to use backticks. But, that's no reason to ban them, either.
关于反引号 `` 的简单事情是用于表示标识符,如数据库名,表名等,单引号 '',双引号 "" 用于字符串文字,而“”按原样用于打印值,而“”打印值变量保留或在另一种情况下打印他拥有的文本。
Simple Thing about backtick `` is use for denote identifier like database_name, table_name etc, and single quote '', double quote "" for string literals, whereas "" use for print value as it is and '' print the value variable hold or in another case print the text his have.
如果您使用某些字段名称作为默认 mysql 或 mssql 值,例如“status”,则必须使用反引号(“select
status
from table_name”或“select id from table_name wherestatus< /代码>=1”)。
因为 mysql 返回错误或无法执行查询。
if you are using some field names as default mysql or mssql values for example "status", you have to use backticks ( "select
status
from table_name" or "select id from table_name wherestatus
=1" ).because mysql returns errors or doesnt work the query.
SQL 中反引号 (`) 的主要用途是在接下来的子句中再次调用它们的情况下使用它们。 在其他时间,建议使用双引号(“”)。
例如,
在上面的语句中,您是否看到如何在
GROUP BY
子句中再次使用Publisher and Location
。而不是使用
我刚刚使用的
仅当出现这种情况时,使用反引号才有用。 在所有其他时间,建议使用双引号。
The main use of backticks (`) in SQL is to use them in situations where you are going to call them again in upcoming clauses. In every other time it is recommended to use double quotes("").
For example
In the above statement do you see how
Publisher and Location
is used again inGROUP BY
clause.Instead of using
I just used
Only when such situations arise, it is useful to use backticks. In all other times using double quotes is recommended.