What's the purpose of SQL keyword “AS”?

前端 未结 9 1653
我在风中等你
我在风中等你 2020-11-30 20:37

You can set table aliases in SQL typing the identifier right after the table name.

SELECT * FROM table t1;

You can even use the keyword

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 21:01

    When you aren't sure which syntax to choose, especially when there doesn't seem to be much to separate the choices, consult a book on heuristics. As far as I know, the only heuristics book for SQL is 'Joe Celko's SQL Programming Style':

    A correlation name is more often called an alias, but I will be formal. In SQL-92, they can have an optional AS operator, and it should be used to make it clear that something is being given a new name. [p16]

    This way, if your team doesn't like the convention, you can blame Celko -- I know I do ;)


    UPDATE 1: IIRC for a long time, Oracle did not support the AS (preceding correlation name) keyword, which may explain why some old timers don't use it habitually.


    UPDATE 2: the term 'correlation name', although used by the SQL Standard, is inappropriate. The underlying concept is that of a ‘range variable’.


    UPDATE 3: I just re-read what Celko wrote and he is wrong: the table is not being renamed! I now think:

    A correlation name is more often called an alias, but I will be formal. In Standard SQL they can have an optional AS keyword but it should not be used because it may give the impression that something is being renamed when it is not. In fact, it should be omitted to enforce the point that it is a range variable.

提交回复
热议问题