为什么要大写我的 SQL 关键字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/608196/
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
Why should I capitalize my SQL keywords?
提问by Samantha Branham
Possible Duplicate:
Is there a good reason to use upper case for T-SQL keywords?
可能的重复:
是否有充分的理由对 T-SQL 关键字使用大写?
Simple question. I personally find a string of lowercase characters to be more readable than a string of uppercase characters. Is some old/popular flavor of SQL case-sensitive or something?
简单的问题。我个人认为一串小写字符比一串大写字符更易读。是一些旧的/流行的 SQL 区分大小写还是什么?
For reference:
以供参考:
select
this.Column1,
case when this.Column2 is null then 0 else this.Column2 end
from dbo.SomeTable this
inner join dbo.AnotherTable another on this.id = another.id
where
this.Price > 100
vs.
对比
SELECT
this.Column1,
CASE WHEN this.Column2 IS NULL THEN 0 ELSE this.Column2 END
FROM dbo.SomeTable this
INNER JOIN dbo.AnotherTable another ON this.id = another.id
WHERE
this.Price > 100
The former just seems so much more readable to me, but I see the latter way more often.
前者对我来说似乎更具可读性,但我更经常看到后者。
采纳答案by Trent
I think the latter is more readable. You can easily separate the keywords from table and column names, etc.
我认为后者更具可读性。您可以轻松地将关键字与表名和列名等分开。
回答by Blorgbeard is out
I agree with you - to me, uppercase is just SHOUTING.
我同意你的看法 - 对我来说,大写只是呼喊。
I let my IDE handle making keywords stand out, via syntax highlighting.
我让我的 IDE 通过语法高亮处理使关键字脱颖而出。
I don't know of a historical reason for it, but by now it's just a subjective preference.
我不知道它的历史原因,但现在这只是一种主观偏好。
Edit to further make clear my reasoning:
编辑以进一步阐明我的推理:
Would you uppercase your keywords in any other modern language? Made up example:
你会用任何其他现代语言大写你的关键字吗?组成示例:
USING (EditForm form = NEW EditForm()) {
IF (form.ShowDialog() == DialogResult.OK) {
IF ( form.EditedThing == null ) {
THROW NEW Exception("No thing!");
}
RETURN form.EditedThing;
} ELSE {
RETURN null;
}
}
Ugh!
啊!
Anyway, it's pretty clear from the votes which style is more popular, but I think we all agree that it's just a personal preference.
无论如何,从投票中很清楚哪种风格更受欢迎,但我想我们都同意这只是个人喜好。
回答by User
One thing I'll add to this which I haven't seen anyone bring up yet:
我要添加的一件事我还没有看到有人提出:
If you're using ad hoc SQL from within a programming language you'll have a lot of SQL inside strings. For example:
如果您在编程语言中使用即席 SQL,您将在字符串中使用大量 SQL。例如:
insertStatement = "INSERT INTO Customers (FirstName, LastName) VALUES ('Jane','Smith')"
In this case syntax coloring probably won't work so the uppercasing could be helping readability.
在这种情况下,语法着色可能不起作用,因此大写可能有助于提高可读性。
回答by onedaywhen
From Joe Celko's "SQL Programming Style" (ISBN 978-0120887972):
来自 Joe Celko 的“SQL 编程风格”(ISBN 978-0120887972):
Rule:
Uppercase the Reserved Words.
Rationale:
Uppercase words are seen as a unit, rather than being read as a series of syllables or letters. The eye is drawn to them, and they act to announce a statement or clause. That is why headlines and warning signs work.
Typographers use the term boumafor the shape of a word. The term appears in paul Saenger's book (1975). Imagine each letter on a rectangular card that just fits it, so that you see the ascenders, descenders, and baseline letters as various "Lego blocks" that are snapped together to make a word.
The boumaof an uppercase word is always a simple, dense rectangle, and it is easy to pick out of a field of lowercase words.
规则:
大写保留字。
理由:
大写单词被视为一个单元,而不是被视为一系列音节或字母。眼睛被他们吸引,他们采取行动宣布声明或条款。这就是标题和警告标志起作用的原因。
排版员使用术语bouma来表示单词的形状。该术语出现在保罗·桑格 (paul Saenger) 的书 (1975) 中。想象一下矩形卡片上的每个字母刚好适合它,这样您就可以将上升、下降和基线字母视为各种“乐高积木”,它们拼凑在一起构成一个单词。
大写单词的bouma总是一个简单的、密集的矩形,很容易从一个小写单词的字段中挑选出来。
What I find compelling is that this is the only book about SQL heuristics, written by a well-known author of SQL works. So is this the absolute truth? Who knows. It sounds reasonable enough and I can at least point out the rule to a team member and tell them to follow it (and if they want to blame anyone I give them Celko's email address :)
我觉得引人注目的是,这是唯一一本关于 SQL 启发式的书,由著名的 SQL 著作作者撰写。那么这是绝对真理吗?谁知道。这听起来很合理,我至少可以向团队成员指出规则并告诉他们遵守它(如果他们想责怪任何人,我会给他们 Celko 的电子邮件地址:)
回答by Paul Tomblin
Mostly it's tradition. We like to keep keywords and our namespace names separate for readability, and since in many DBMSes table and column names are case sensitive, we can't upper case them, so we upper case the keywords.
主要是传统。我们喜欢将关键字和命名空间名称分开以提高可读性,并且由于在许多 DBMS 中表和列名称区分大小写,我们不能将它们大写,因此我们将关键字大写。
回答by Instance Hunter
Code has punctuation which SQL statements lack. There are dots and parentheses and semicolons to help you keep things separate. Code also has lines. Despite the fact that you can write a SQL statement on multiple physical lines, it is a single statement, a single "line of code."
代码具有 SQL 语句所缺乏的标点符号。有点、括号和分号可以帮助您将事物分开。代码也有线条。尽管您可以在多条物理行上编写 SQL 语句,但它是一条语句,一条“代码行”。
IF i were to write english text without any of the normal punctuation IT might be easier if i uppercased the start of new clauses THAT way itd be easier to tell where one ended and the next began OTHERWISE a block of text this long would probably be very difficult to read NOT that id suggest its easy to read now BUT at least you can follow it i think
如果我要写没有任何正常标点符号的英文文本,如果我将新条款的开头大写可能会更容易,这样就更容易分辨出一个在哪里结束,下一个从哪里开始否则这么长的文本块可能会非常难以阅读 不是那个 id 表明它现在很容易阅读但至少你可以遵循它我认为
回答by Guffa
I prefer lower case keywords. Management Studio color codes the keywords, so there is no problem distinguishing them from the identifiers.
我更喜欢小写关键字。Management Studio 对关键字进行颜色编码,因此将它们与标识符区分开来没有问题。
And upper case keywords feels so... well... BASIC... ;)
大写关键字感觉如此......好吧......基本......;)
-"BASIC, COBOL and FORTRAN called from the eighties, they wanted their UPPERCASE KEYWORDS back." ;)
-“BASIC、COBOL 和 FORTRAN 从 80 年代开始,他们想要回他们的大写关键字。” ;)
回答by WW.
I like to use upper case on SQL keywords. I think my mind skips over them as they are really blocky and concentrates on what's important. The blocky words split up the important bits when you layout like this:
我喜欢在 SQL 关键字上使用大写。我想我的思绪会跳过它们,因为它们真的很笨拙,专注于重要的事情。当您像这样布局时,块状词将重要的部分分开:
SELECT
s.name,
m.eyes,
m.foo
FROM
muppets m,
muppet_shows ms,
shows s
WHERE
m.name = 'Gonzo' AND
m.muppetId = ms.muppetId AND
ms.showId = s.showId
(The lack of ANSI joins is an issue for another question.)
(缺少 ANSI 连接是另一个问题的问题。)
There is a psychology study that shows lowercase was quicker to read than uppercase due to the outlines of the words being more distinctive. However, this effect can disappear about with lots of practice reading uppercase.
有一项心理学研究表明,由于单词的轮廓更加独特,小写字母比大写字母更容易阅读。但是,通过大量练习阅读大写字母,这种效果可能会消失。
回答by Steve
What's worse it that as the majority of developers at my office believe in capitals for sql keywords so I have had to change to uppercase. Majority rules.
更糟糕的是,由于我办公室的大多数开发人员都相信 sql 关键字的大写,所以我不得不改为大写。多数规则。
I believe lowercase is easier to read and that given that sql keywords are highlighted in blue anyway.
我相信小写字母更容易阅读,而且 sql 关键字无论如何都以蓝色突出显示。
In the glory days keywords were in captials because we were developing on green screens!
在辉煌的日子里,关键字是大写的,因为我们是在绿屏上开发的!
The question is: if we don't write c# keywords in uppercase then why do I have to write sql keywords in uppercase?
问题是:如果我们不写大写的c#关键字,那为什么我必须写大写的sql关键字?
Like someone else has said - capitals are SHOUTING!
就像其他人所说的 - 首都在呼喊!
回答by Walter Mitty
Back in the 80s, I used to capitalize database names, and leave sql keywords in lower case. Most writers did the opposite, capitalizing the SQL keywords. Eventually, I started going along with the crowd.
早在 80 年代,我曾经将数据库名称大写,而将 sql 关键字保留为小写。大多数作者反其道而行之,将 SQL 关键字大写。最终,我开始跟着人群走。
Just in passing, I'll mention that, in most published code snippets in C, C++, or Java the language keywords are always in lower case, and upper case keywords may not even be recognized as such by some parsers. I don't see a good reason for using the opposite convention in SQL that you use in the programming language, even when the SQL is embedded in source code.
顺便提一下,在大多数已发布的 C、C++ 或 Java 代码片段中,语言关键字总是小写的,而某些解析器甚至可能无法识别大写的关键字。我没有看到在 SQL 中使用与编程语言中使用的相反约定的充分理由,即使 SQL 嵌入在源代码中也是如此。
And I'm not defending the use of all caps for database names. It actually looks a little like "shouting". And there are better conventions, like using a few upper case letters in database names. (By "database names" I mean the names of schemas, schema objects like tables, and maybe a few other things.) Just because I did it in the 80s doesn't mean I have to defend it today.
而且我不是在为数据库名称使用所有大写字母辩护。它实际上看起来有点像“喊叫”。还有更好的约定,比如在数据库名称中使用几个大写字母。(我所说的“数据库名称”是指模式的名称、模式对象(如表)以及其他一些东西。)仅仅因为我在 80 年代这样做并不意味着我今天必须捍卫它。
Finally, "De gustibus non disputandum est".
最后,“De gustibus non disputandum est”。