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条回答
ら.Afraid
2楼-- · 2019-01-17 02:57

Some SQL developers here like to lay it out like this:

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

They claim this is easier to read unlike your one field per line approach which I use myself.

查看更多
\"骚年 ilove
3楼-- · 2019-01-17 02:59

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.

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.

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.

Finally, "De gustibus non disputandum est".

查看更多
唯我独甜
4楼-- · 2019-01-17 03:00

I prefer lower case keywords. Management Studio color codes the keywords, so there is no problem distinguishing them from the identifiers.

And upper case keywords feels so... well... BASIC... ;)

-"BASIC, COBOL and FORTRAN called from the eighties, they wanted their UPPERCASE KEYWORDS back." ;)

查看更多
Anthone
5楼-- · 2019-01-17 03:01

I capitalize SQL to make it more "contrasty" to the host language (mostly C# these days).

It's just a matter of preference and/or tradition really...

查看更多
登录 后发表回答