是否有充分的理由对 SQL 关键字使用大写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/292026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a good reason to use upper case for SQL keywords?
提问by Hertanto Lie
The default seems to be upper case, but is there really any reason to use upper case for keywords? I started using upper case because I was just trying to match what SQL Server gives me whenever I tried to create something, like a new stored procedure. But then, I felt terrible for my baby (5th) finger, that always needs to hold down the Shiftbutton, so I stopped using upper case. Any reason why I should go back to upper case?
默认似乎是大写,但真的有理由对关键字使用大写吗?我开始使用大写是因为我只是想在我尝试创建一些东西时匹配 SQL Server 给我的东西,比如一个新的存储过程。但是后来,我对我的宝贝(第 5 个)手指感到很糟糕,它总是需要按住Shift按钮,所以我停止使用大写字母。我应该回到大写的任何理由?
Edit: Thanks for the answers guys. I wasn't programming yet back in the days when COBOLwas king, so I wasn't aware of this. I'll stick with lower case from now on.
编辑:感谢各位的回答。在COBOL为王的日子里,我还没有编程,所以我没有意识到这一点。从现在开始我会坚持使用小写字母。
采纳答案by Mitch Wheat
It's just a matter of style, probably originating in the days when editors didn't do code colouring.
这只是风格问题,可能起源于编辑不进行代码着色的时代。
I used to prefer all upper case, but I'm now leaning towards all lower.
我曾经喜欢全部大写,但我现在倾向于全部小写。
回答by Gordon Bell
PERSONALLY, I DON'T LIKE MY SQL YELLING AT ME. IT REMINDS ME OF BASIC OR COBOL.
就我个人而言,我不喜欢我的 SQL 对我大喊大叫。它让我想起了 BASIC 或 COBOL。
So I prefer my T-SQL lowercase with database object names MixedCase.
所以我更喜欢我的 T-SQL 小写与数据库对象名称 MixedCase。
It is much easier to read, and literals and comments stand out.
它更容易阅读,文字和注释也很突出。
回答by bignose
SQL IS OLD. UPPER CASE IS SHOUTING. IT LOOKS STRANGE AND PERHAPS UGLY.
SQL 是旧的。大写大写。它看起来很奇怪,而且可能很丑。
While arguably true, none of those address the reasons special to the SQL languagewhy upper-case keywords are a good convention.
虽然可以说是正确的,但这些都没有解决SQL 语言为什么大写关键字是一个好的约定的特殊原因。
Unlike many newer languages, SQL has a large number of keywordsand relies on the reader's ability to distinguish keywords versus identifiersin order to mentally parse the syntax.
与许多较新的语言不同,SQL 具有大量关键字,并且依赖于读者区分关键字和标识符的能力,以便在心理上解析语法。
The direct answer to your question, then, is more an answer to “why does the reader of SQL code benefitso much from uppercase keywords, when that's not as true for most modern languages?”:
那么,对您的问题的直接回答更像是对“为什么SQL 代码的读者从大写关键字中获益如此之多,而大多数现代语言并非如此?”的答案:
To rely on keeping the keywords in one's headis reasonable for many modern languages, but unreasonable for SQL; it has too many keywords, and too many variants.
To rely on punctuation cuesis reasonable for most modern languages, but unreasonable for SQL; it has too few, instead depending on the precise order of keywords to indicate syntax.
To rely on automatic highlightersfor distinguishing keywords is reasonable for modern languages in usual cases, but ignores the reality of what highlighters can achieve for SQL. Most don't cover all keywords of all variants of SQL, and regardless, SQL is frequently and routinely read in contexts where a highlighter won't help.
对于许多现代语言来说,依靠将关键字牢记在心是合理的,但对于 SQL 则不合理;它有太多的关键字和太多的变体。
对于大多数现代语言来说,依赖标点符号是合理的,但对于 SQL 则不合理;它太少了,而是依靠关键字的精确顺序来指示语法。
在通常情况下,依靠自动荧光笔来区分关键字对于现代语言来说是合理的,但忽略了荧光笔可以为 SQL 实现的现实。大多数都没有涵盖 SQL 的所有变体的所有关键字,并且无论如何,在荧光笔无济于事的上下文中经常和例行地阅读 SQL。
These are some of the reasons, specific to SQL, that the reader of SQL code is best served by standardising on upper case for keywords, and only using not-upper (i.e. lower, or mixed) case for identifiers.
这些是特定于 SQL 的一些原因,SQL 代码的读者最好通过将关键字的大写标准化,并且仅对标识符使用非大写(即小写或混合)大小写来提供最佳服务。
Highlighting can sometimes help. But only if the highlighter knows you've got SQL; and we very often have SQL in a context where the editor/formatter can't reasonably know it's dealing with SQL. Examples include in-line queries, programmer documentation, and text strings within the code of another language. The same is not true anywhere near as often for languages like Python or C++; yes, their code does sometimes appear in those places, but it's not routinely done the way it is with SQL code.
突出显示有时会有所帮助。但前提是荧光笔知道你有 SQL;并且我们经常在编辑器/格式化程序无法合理地知道它正在处理 SQL 的上下文中使用 SQL。示例包括内嵌查询、程序员文档和另一种语言代码中的文本字符串。对于 Python 或 C++ 等语言而言,情况并非如此。是的,他们的代码有时会出现在这些地方,但通常不会像使用 SQL 代码那样完成。
Also, the reader will commonly be using a highlighter that only knows a subset of the keywords your specific SQL implementation uses. Many of the less-common keywords won't be highlighted except by one that knows your SQL variant intimately. So the reader, even if they're using a highlighter, still needs some more direct way of distinguishing keywords in any moderately-complex SQL statement.
此外,读者通常会使用只知道特定 SQL 实现使用的关键字子集的荧光笔。许多不常见的关键字不会被突出显示,除非是非常了解您的 SQL 变体的关键字。所以读者,即使他们使用了荧光笔,仍然需要一些更直接的方法来区分任何中等复杂的 SQL 语句中的关键字。
Thus the reader will frequently – and the writer can't know ahead of time when that will be – need assistance from the content of the SQL statement itself, to know what's intended by the writer as a keyword and what's intended as an identifier. So the SQL content itselfneeds to distinguish keywords for the reader, and using uppercase keywords is the conventional and useful way to do that.
因此,读者将经常——而作者无法提前知道什么时候——需要从 SQL 语句本身的内容中获得帮助,以了解作者打算将什么用作关键字以及将什么用作标识符。因此SQL 内容本身需要为读者区分关键字,而使用大写关键字是实现此目的的常规且有用的方法。
回答by Lukas Eder
THERE WAS A TIME WHEN MOST PEOPLE DID NOT HAVE THE POSSIBILITY OF ENCODING ANYTHING BEYOND UPPER CASE LETTERS BECAUSE THE RELEVANT ENCODING (ASCII) WAS NOT YET INVENTED. ONLY SIX BITS WERE AVAILABLE. WHILE SQL IS MORE RECENT, LOWER CASE LETTERS WERE NOT COMMON PRACTICE IN PROGRAMMING YET.
曾几何时,大多数人无法编码大写字母以外的任何内容,因为相关的编码 (ASCII) 尚未发明。只有六个位可用。虽然 SQL 是较新的,但小写字母在编程中还不是常见的做法。
NOTE THAT SOME PEOPLE CLAIMTHAT THE DATABASE WILL GET A SENSE OF URGENCY AND RUN YOUR QUERIES FASTER.
需要注意的是有些人声称,该数据库将得到一种紧迫感,并运行你的查询速度更快。
回答by davidtbernal
Gordon Bell's examples are not exactly correct; generally, only the keywords are highlighted, not the entire query. His second example would look like:
Gordon Bell 的例子并不完全正确。通常,仅突出显示关键字,而不是整个查询。他的第二个例子看起来像:
SELECT name, id, xtype, uid, info, status,
base_schema_ver, replinfo, parent_obj, crdate,
ftcatid, schema_ver, stats_schema_ver, type,
userstat, sysstat, indexdel, refdate, version,
deltrig, instrig, updtrig, seltrig, category, cache
FROM sysobjects
WHERE category = 0
AND xtype IN ('U', 'P', 'FN', 'IF', 'TF')
ORDER BY 1
I find this far easier to read, since the keywords stand out more. Even with syntax highlighting, I find the uncapitalized example much harder to read.
我发现这更容易阅读,因为关键字更突出。即使使用语法高亮显示,我发现未大写的示例更难阅读。
At my company, we go a little bit farther with our SQL formatting.
在我的公司,我们在 SQL 格式方面走得更远。
SELECT name, id, xtype, uid, info, status,
base_schema_ver, replinfo, parent_obj, crdate,
ftcatid, schema_ver, stats_schema_ver, type,
userstat, sysstat, indexdel, refdate, version,
deltrig, instrig, updtrig, seltrig, category, cache
FROM sysobjects
LEFT JOIN systhingies ON
sysobjects.col1=systhingies.col2
WHERE category = 0
AND xtype IN ('U', 'P', 'FN', 'IF', 'TF')
ORDER BY 1
回答by AaronLS
Less than 10% of the letters in the text we read are upper case. Hence our brains are more keen at recognizing lower case letters than upper case ones. Studies have shown it takes longer to read upper case text. Here is just one example:
我们阅读的文本中不到 10% 的字母是大写的。因此,我们的大脑更热衷于识别小写字母而不是大写字母。研究表明,阅读大写文本需要更长的时间。这里只是一个例子:
http://www.guardian.co.uk/media/mind-your-language/2010/oct/04/new-york-street-signs-capitals
http://www.guardian.co.uk/media/mind-your-language/2010/oct/04/new-york-street-signs-capitals
The above example I think emphasizes that even when you're talking about just one or two words, it makes a difference.
我认为上面的例子强调,即使你只谈论一两个词,它也会有所不同。
回答by Bohemian
It's because SQL is such an old language (1974) that when it was conceived, most keyboards didn't have lowercase letters! The language documentation simply reflected the technology of the time.
这是因为 SQL 是一种古老的语言(1974 年),所以在构思它时,大多数键盘都没有小写字母!语言文档只是反映了当时的技术。
Research has proven ALL CAPS is harder to read, so much so that the USA Federal Highway Administration has mandated the use of mixed-case signs in their Manual on Uniform Traffic Control Devices, which states:
研究证明 ALL CAPS 更难阅读,以至于美国联邦公路管理局在其统一交通控制设备手册中强制使用混合大小写标志,其中指出:
The lettering for names of places, streets, and highways on conventional road guide signs shall be a combination of lowercase letters with initial uppercase letters.
常规道路引导标志的地名、街道名、公路名的字母应为小写字母与首字母大写的组合。
The New York Post also published:
《纽约邮报》还发表了:
Studies have shown that it is harder to read all-caps signs, and those extra milliseconds spent staring away from the road have been shown to increase the likelihood of accidents, particularly among older drivers.
研究表明,阅读全部大写的标志更难,而且已经证明,在远离道路的地方多花几毫秒的时间会增加发生事故的可能性,尤其是在老年司机中。
There is no good reason to use uppercase letters, and good reasons not to.
没有充分的理由使用大写字母,也没有充分的理由不使用。
I personally loath using uppercase for SQL keywords. I find it harder to read and absurd in this day and age.
我个人不喜欢对 SQL 关键字使用大写。我发现在这个时代更难阅读和荒谬。
The SQL language is defined as being case-insensitive. Take your finger off that shift key!
SQL 语言被定义为不区分大小写。把你的手指从那个shift键上拿开!
回答by Ovidiu Pacurar
Upper case can provide a gain in keyword visibility, but you can compensate with code highlight and indentation.
We use lower case because query editor and other tools do wonders in editing t-sql code, and we see no need to torture the little finger.
大写可以提高关键字的可见性,但您可以通过代码突出显示和缩进进行补偿。
我们使用小写是因为查询编辑器和其他工具在编辑 t-sql 代码方面做得很好,我们认为没有必要折磨小指。
回答by dkretz
Monkey see, monkey do for me. Pattern matching - if I do it the way I've seen it done, the structure of the clauses lines up mentally more easily.
猴子看,猴子为我做。模式匹配——如果我按照我看到的方式去做,从句的结构在心理上更容易排列。
回答by Lance Fisher
Uppercase is less readable. The outline of all words are shaped like boxes; there are no descenders or ascenders. Lowercase FTW!
大写的可读性较差。所有单词的轮廓都像盒子一样;没有下降或上升。小写 FTW!