Why should I capitalize my SQL keywords? [duplicat

2019-01-17 02:10发布

Possible Duplicate:
Is there a good reason to use upper case for T-SQL keywords?

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?

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.

16条回答
Evening l夕情丶
2楼-- · 2019-01-17 02:36

It's just a matter of readability. Helps you quickly distinguish SQL keywords.

Btw, that question was already answered: Is SQL syntax case sensitive?

查看更多
Rolldiameter
3楼-- · 2019-01-17 02:37

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.

I believe lowercase is easier to read and that given that sql keywords are highlighted in blue anyway.

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?

Like someone else has said - capitals are SHOUTING!

查看更多
来,给爷笑一个
4楼-- · 2019-01-17 02:38

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.

查看更多
你好瞎i
5楼-- · 2019-01-17 02:39

I agree with you - to me, uppercase is just SHOUTING.

I let my IDE handle making keywords stand out, via syntax highlighting.

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.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-17 02:39

From Joe Celko's "SQL Programming Style" (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 bouma for 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 bouma of an uppercase word is always a simple, dense rectangle, and it is easy to pick out of a field of lowercase words.

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 :)

查看更多
贪生不怕死
7楼-- · 2019-01-17 02:41

Its just a question of readability. Using UPPERCASE for the SQL keywords helps make the script more understandable.

查看更多
登录 后发表回答