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条回答
我命由我不由天
2楼-- · 2019-01-17 02:46

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:

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.

查看更多
疯言疯语
3楼-- · 2019-01-17 02:49

Apropos of nothing perhaps, but I prefer typesetting SQL keywords in small caps. That way they look capitalized to most readers, but they aren't the same as the ugly ALL CAPS style.

A further advantage is that I can leave the code as is and print it in the traditional style. (I use the listings package in LaTeX for pretty-printing code.)

查看更多
Bombasti
4楼-- · 2019-01-17 02:52

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:

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

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.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-17 02:52

I prefer using upper case as well for keywords in SQL.

Yes lower case is more readable but for me having to take extra second to scan through the query will do you good most of the time. Once it's done and tested you should rarely ever see it again anyway (DAL, stored proc or whatever will hide it from you).

If you are reading it for the first time, capitalized WHERE AND JOIN will jump right at you, as they should.

查看更多
虎瘦雄心在
6楼-- · 2019-01-17 02:54

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."

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

查看更多
爷的心禁止访问
7楼-- · 2019-01-17 02:56

I think the latter is more readable. You can easily separate the keywords from table and column names, etc.

查看更多
登录 后发表回答