在字段名称周围使用反引号 [英] Using backticks around field names
问题描述
在阅读了一些有关SQL问题的答案和评论之后,还听说我的一个朋友在一个禁止他们使用的政策的地方工作,我想知道在现场使用反引号是否有问题MySQL中的名称.
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.
也就是说:
SELECT `id`, `name`, `anotherfield` ...
-- vs --
SELECT id, name, anotherfield ...
推荐答案
使用反引号允许您使用其他字符.在查询书写中,这不是问题,但是如果有人假设您可以使用反引号,那么我认为它可以让您摆脱诸如
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.
这篇关于在字段名称周围使用反引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!