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

前端 未结 9 1441
我在风中等你
我在风中等你 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:08

    There is no difference between both statements above. AS is just a more explicit way of mentioning the alias

    0 讨论(0)
  • 2020-11-30 21:12

    The use is more obvious if you don't use 'SELECT *' (which is a bad habit you should get out of):

    SELECT t1.colA, t2.colB, t3.colC FROM alongtablename AS t1, anotherlongtablename AS t2, yetanotherlongtablename AS t3 WHERE t1.colD = t2.colE...
    
    0 讨论(0)
  • 2020-11-30 21:13

    The AS in this case is an optional keyword defined in ANSI SQL 92 to define a <<correlation name> ,commonly known as alias for a table.

    <table reference> ::=
                <table name> [ [ AS ] <correlation name>
                    [ <left paren> <derived column list> <right paren> ] ]
              | <derived table> [ AS ] <correlation name>
                    [ <left paren> <derived column list> <right paren> ]
              | <joined table>
    
         <derived table> ::= <table subquery>
    
         <derived column list> ::= <column name list>
    
         <column name list> ::=
              <column name> [ { <comma> <column name> }... ]
    
    
         Syntax Rules
    
         1) A <correlation name> immediately contained in a <table refer-
            ence> TR is exposed by TR. A <table name> immediately contained
            in a <table reference> TR is exposed by TR if and only if TR
            does not specify a <correlation name>.
    

    It seems a best practice NOT to use the AS keyword for table aliases as it is not supported by a number of commonly used databases.

    0 讨论(0)
提交回复
热议问题