Should I Always Fully Qualify Column Names In SQL?

后端 未结 11 1135
小鲜肉
小鲜肉 2021-02-14 02:48

Out of interest when working with SQL statements should I always use the fully qualifed column name (tablename.columnname) even if only working with one table e.g.



        
相关标签:
11条回答
  • 2021-02-14 03:18

    I prefer to use a table alias when more than 1 table is in play.

    0 讨论(0)
  • 2021-02-14 03:19

    I would put this as personal preference. It would only make a difference if you started joining tables which contain duplicate column names.

    Also, rather than write out the table name in full, use an alias:

    SELECT t.column1, t.column2 FROM table as t

    0 讨论(0)
  • 2021-02-14 03:20

    If you're only querying one table - I'd say no. It's more readable that way.

    0 讨论(0)
  • 2021-02-14 03:21

    It's better if you do - it doesn't add any complexity, and it can prevent errors in the future.

    But in a well-defined system, you shouldn't have to - it's like namespaces in programming languages. The ideal is not to have conflicts, but it can clutter the code with the superfluous use of explicit names.

    -Adam

    0 讨论(0)
  • 2021-02-14 03:28

    If you are only selecting from one table I do not see the overall usefulness. If you are selecting from multiple tables, qualifying the column names would certainly make it easier to read for any other developer who may not be familiar with your database schema.

    0 讨论(0)
  • 2021-02-14 03:29

    Even the most well defined systems are subject to change. A new field could quite easily be introduced to a table causing ambiguity in an existing query.

    Fully qualifying them protects you from this scenario.

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