column order in SELECT * statement - guaranteed?

前端 未结 1 928
眼角桃花
眼角桃花 2020-12-18 22:49

I am using an ORM (sqlalchemy) to fetch data from a PG database. I want to avoid specifying all the table column names in my hand crafted SQL statements*.

My assumpt

1条回答
  •  [愿得一人]
    2020-12-18 23:40

    Let's consider the SQL standard, section 7.9 as specified here:

    http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt

     ::=
              SELECT [  ]  ::=
                
              |  }... ]
    
    [...]
    Syntax Rules
    1) Let T be the result of the .
    3) Case:
           a) [...]
           b) Otherwise, the 
    consists of several tables involving JOIN .. USING or NATURAL JOIN clauses. However, when selecting from a simple table, you're probably fine assuming that the order is as expected.

    For completeness, the meaning of an ordinal position within T for tables is explained further down in 11.4 :

    General Rules
         5) [...] The ordinal position included
            in the column descriptor is equal to the degree of T. [...]
    

    And then in 11.11 (for ALTER TABLE statements)

    General Rules
         4) [...] In particular, the degree of T
            is increased by 1 and the ordinal position of that column is
            equal to the new degree of T as specified in the General Rules
            of Subclause 11.4, "".
    

    There are quite a few other SQL statements and clauses that depend on the formal specification of ordinal positions within

    . Some examples:

    13.8  
         (when omitting the ``)
    20.2 
         (when `` contains an ``)
    

    Postgres, in particular, is quite standards-compliant, so if you really want to SELECT *, go ahead!

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