問(wèn)題描述
在這里閱讀了一些關(guān)于 SQL 問(wèn)題的答案和評(píng)論后,還聽(tīng)說(shuō)我的一個(gè)朋友在一個(gè)有禁止它們的政策的地方工作,我想知道在 field 周?chē)褂梅匆?hào)是否有什么問(wèn)題MySQL 中的名稱(chēng).
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 ...
推薦答案
使用反引號(hào)允許您使用替代字符.在查詢編寫(xiě)中,這不是一個(gè)問(wèn)題,但是如果有人假設(shè)您可以只使用反引號(hào),我認(rèn)為它可以讓您擺脫諸如
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`
這當(dāng)然會(huì)生成名稱(chēng)錯(cuò)誤的表.
Which does of course generate badly named tables.
如果你只是簡(jiǎn)明扼要,我看不出有什么問(wèn)題,你會(huì)注意到你是否這樣運(yùn)行你的查詢
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
返回的生成警告將帶有反引號(hào)和完全限定的表名.因此,如果您正在使用查詢生成功能和自動(dòng)重寫(xiě)查詢,反引號(hào)將使解析代碼的任何內(nèi)容變得不那么混亂.
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.
然而,我認(rèn)為,與其強(qiáng)制您是否可以使用反引號(hào),不如他們應(yīng)該有一個(gè)名稱(chēng)標(biāo)準(zhǔn).它解決了更多真實(shí)"的問(wèn)題.
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.
這篇關(guān)于在字段名稱(chēng)周?chē)褂梅匆?hào)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!