在字段名称周围使用反引号

发布于 2024-07-08 00:29:29 字数 215 浏览 13 评论 0原文

在阅读了一些关于一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

胡渣熟男 2024-07-15 00:29:29

使用反引号允许您使用替代字符。 在查询编写中,这不是一个问题,但如果假设您可以只使用反引号,我会认为它可以让您摆脱诸如“

SELECT `id`, `my name`, `another field` , `field,with,comma` 

Which does course generated bad-named table”之类的可笑的东西。

如果你只是简明扼要的话,我不认为有什么问题,
您会注意到,如果您这样运行查询,则

EXPLAIN EXTENDED Select foo,bar,baz 

返回的生成警告将带有反引号完全限定的表名称。 因此,如果您使用查询生成功能和自动重写查询,反引号将使任何解析代码的内容不再那么混乱。

然而,我认为,他们不应该强制要求是否可以使用反引号,而应该有一个名称标准。 它解决了更多“实际”问题。

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

SELECT `id`, `my name`, `another field` , `field,with,comma` 

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

EXPLAIN EXTENDED Select foo,bar,baz 

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.

老街孤人 2024-07-15 00:29:29

反引号的唯一问题是它们不符合 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.

乱世争霸 2024-07-15 00:29:29

对我来说,在处理字段名称时始终使用它们很有意义。

  • 首先,一旦你养成了这个习惯,按下反引号键也没什么坏处。
  • 其次,对我来说,它可以更轻松地查看查询中的字段到底是什么,以及关键字或方法是什么。
  • 最后,它允许您在设计表时使用您想要的任何字段名称。 有时,将字段命名为“键”、“顺序”或“值”很有意义......所有这些在引用它们时都需要反引号。

To me it makes a lot of sense to use them at all times when dealing with field names.

  • Firstly, once you get into the habit, it doesn't hurt to just hit the backtick key.
  • Secondly, to me, it makes it easier to see what exactly are the fields in your query, and what are keywords or methods.
  • Lastly, it allows you to use whatever field name you wish when designing your table. Sometimes it makes a lot of sense to name a field "key", "order", or "values"... all of which require backticks when referring to them.
陪我终i 2024-07-15 00:29:29

反引号不是标准 ANSI SQL 的一部分。 来自 mysql 手册

如果 ANSI_QUOTES SQL 模式是
启用,也可以引用
双引号内的标识符

因此,如果您使用反引号,然后决定放弃 MySQL,那么您就会遇到问题(尽管您可能还有很多更大的问题)

Backticks aren't part of standard ANSI SQL. From the mysql manual:

If the ANSI_QUOTES SQL mode is
enabled, it is also allowable to quote
identifiers within double quotes

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)

流年里的时光 2024-07-15 00:29:29

如果您继续使用 MYSQL,除了查询的视觉模糊性之外,没有任何问题。 但它们确实允许使用保留关键字或嵌入空格作为表名和列名。 对于大多数数据库引擎来说这是禁忌,并且会阻止以后的任何迁移。

为了方便阅读,很多人对 SQL 关键字使用大写,例如。

SELECT some_fied, some_other_field FROM whatever WHERE id IS NULL;

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.

SELECT some_fied, some_other_field FROM whatever WHERE id IS NULL;
败给现实 2024-07-15 00:29:29

如果你问我,应该始终使用反引号。 但出于某些原因,团队可能不愿意使用它们。

优点:

  • 使用它们,没有保留字或禁止字符。
  • 在某些情况下,您会收到更具描述性的错误消息。
  • 如果你避免了不好的做法,你并不关心,但是......实际上,有时它们是避免 SQL 注入的好方法。

缺点:

  • 它们不是标准的,通常不便于携带。 但是,只要您不使用反引号作为标识符的一部分(这是我能想象到的最糟糕的做法),您就可以通过自动删除反引号来移植您的查询。
  • 如果您的某些查询来自 Access,它们可能会用“引用表名(也许您不能盲目删除所有“)。 但是,允许混合使用反引号和双引号。
  • 一些愚蠢的软件或功能会过滤您的查询,并且存在反引号问题。 然而,它们是 ASCII 的一部分,所以这意味着你的软件/功能非常糟糕。

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:

  • Using them, there are no reserved words or forbidden chars.
  • In some cases, you get more descriptive error messages.
  • If you avoid bad practices you don't care, but... in real word, sometimes they are a decent way to avoid SQL injections.

Disadvantages:

  • They are not standard and usually not portable. However, as long as you don't use a backtick as part of an identifier (which is the worst practice I am able to imagine), you can port your query by automatically removing backticks.
  • If some of your query come from Access, they may quote table names with " (and maybe you can't remove all the " blindly). However, mixtures of backticks and double quotes are allowed.
  • Some stupid software or function filters your queries, and has problems with backticks. However, they are part of ASCII so this means that your software/function is very bad.
揽清风入怀 2024-07-15 00:29:29

在代码库中搜索反引号中的内容要容易得多。 假设您有一个名为 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.

冷月断魂刀 2024-07-15 00:29:29

嗯,据我所知,使用反引号的全部目的是让您可以使用与保留关键字一致的名称。 因此,如果名称不与保留关键字冲突,我认为没有任何理由使用反引号。 但是,这也不是禁止它们的理由。

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.

无悔心 2024-07-15 00:29:29

关于反引号 `` 的简单事情是用于表示标识符,如数据库名,表名等,单引号 '',双引号 "" 用于字符串文字,而“”按原样用于打印值,而“”打印值变量保留或在另一种情况下打印他拥有的文本。

i.e 1.-> use `model`;   
    here `model` is database name not conflict with reserve keyword 'model'
2- $age = 27;
insert into `tbl_people`(`name`,`age`,`address`) values ('Ashoka','$age',"Delhi");

here i used both quote for all type of requirement. If anything not clear let me know..

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.

i.e 1.-> use `model`;   
    here `model` is database name not conflict with reserve keyword 'model'
2- $age = 27;
insert into `tbl_people`(`name`,`age`,`address`) values ('Ashoka','$age',"Delhi");

here i used both quote for all type of requirement. If anything not clear let me know..
最舍不得你 2024-07-15 00:29:29

如果您使用某些字段名称作为默认 mysql 或 mssql 值,例如“status”,则必须使用反引号(“select status from table_name”或“select id from table_name where status< /代码>=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 where status=1" ).
because mysql returns errors or doesnt work the query.

惜醉颜 2024-07-15 00:29:29

SQL 中反引号 (`) 的主要用途是在接下来的子句中再次调用它们的情况下使用它们。 在其他时间,建议使用双引号(“”)。

例如,

SELECT CONCAT(Name, ' in ', city, ', ', statecode) AS `Publisher and Location`,
    COUNT(ISBN) AS "# Books",
    MAX(LENGTH(title)) AS "Longest Title",
    MIN(LENGTH(title)) AS "Shortest Title"
FROM Publisher JOIN Book
ON Publisher.PublisherID = Book.PublisherID WHERE INSTR(name, 'read')>0
GROUP BY `Publisher and Location`
HAVING COUNT(ISBN) > 1;

在上面的语句中,您是否看到如何在 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

SELECT CONCAT(Name, ' in ', city, ', ', statecode) AS `Publisher and Location`,
    COUNT(ISBN) AS "# Books",
    MAX(LENGTH(title)) AS "Longest Title",
    MIN(LENGTH(title)) AS "Shortest Title"
FROM Publisher JOIN Book
ON Publisher.PublisherID = Book.PublisherID WHERE INSTR(name, 'read')>0
GROUP BY `Publisher and Location`
HAVING COUNT(ISBN) > 1;

In the above statement do you see how Publisher and Location is used again in GROUP BY clause.

Instead of using

GROUP BY Name, city, statecode

I just used

GROUP BY Publisher and Location

Only when such situations arise, it is useful to use backticks. In all other times using double quotes is recommended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文