Comments on: Proper SQL table alias use conventions https://shlomi-noach.github.io/blog/mysql/proper-sql-table-alias-use-conventions Blog by Shlomi Noach Fri, 22 Feb 2013 20:20:34 +0000 hourly 1 https://wordpress.org/?v=5.3.3 By: Log Buffer #182, A Carnival of the Vanities for DBAs https://shlomi-noach.github.io/blog/mysql/proper-sql-table-alias-use-conventions/comment-page-1#comment-183987 Fri, 22 Feb 2013 20:20:34 +0000 https://shlomi-noach.github.io/blog/?p=2156#comment-183987 […] is no consistent convention as for how to write [about table aliases in] an SQL query” in proper sql table alias use conventions. Noach also gives us a tip: faster than […]

]]>
By: PHP-help » Log Buffer #182, a Carnival of the Vanities for DBAs https://shlomi-noach.github.io/blog/mysql/proper-sql-table-alias-use-conventions/comment-page-1#comment-11422 Fri, 12 Mar 2010 22:48:05 +0000 https://shlomi-noach.github.io/blog/?p=2156#comment-11422 […] is no consistent convention as for how to write [about table aliases in] an SQL query” in proper sql table alias use conventions. Noach also gives us a tip: faster than […]

]]>
By: Sheeri Cabral https://shlomi-noach.github.io/blog/mysql/proper-sql-table-alias-use-conventions/comment-page-1#comment-11414 Fri, 12 Mar 2010 12:53:40 +0000 https://shlomi-noach.github.io/blog/?p=2156#comment-11414 I personally hate single or 2-character table aliases, because when using EXPLAIN, you see the table alias only……

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/proper-sql-table-alias-use-conventions/comment-page-1#comment-11393 Fri, 12 Mar 2010 04:19:03 +0000 https://shlomi-noach.github.io/blog/?p=2156#comment-11393 Hi Hed,

Indeed, the XML repository makes for more readable queries, plus it concentrates queries in one place. Which is why Springframework, Ibatis etc. support and encourage XML files.
One can argue whether this is the better method. I confess I still like to see my queries next to the relevant code. But I believe this is mostly a development-tool need-to-solve problem.

Shlomi

]]>
By: hed https://shlomi-noach.github.io/blog/mysql/proper-sql-table-alias-use-conventions/comment-page-1#comment-11383 Thu, 11 Mar 2010 20:19:26 +0000 https://shlomi-noach.github.io/blog/?p=2156#comment-11383 As the semi proud originator of the aliasing convention post.
(not because of the well behaved queries)

I want to add
that we used to hold our queries in the Java code, and it was horrible.
most annoying was the time it took to switch from code to shell execute. (the query formatting)
So we moved to an XML repository which is much better.
Queries are much more readable and extractable.

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/proper-sql-table-alias-use-conventions/comment-page-1#comment-11378 Thu, 11 Mar 2010 12:08:37 +0000 https://shlomi-noach.github.io/blog/?p=2156#comment-11378 Hmmm… something still missing in the query…

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/proper-sql-table-alias-use-conventions/comment-page-1#comment-11376 Thu, 11 Mar 2010 11:38:54 +0000 https://shlomi-noach.github.io/blog/?p=2156#comment-11376 @Bill

Thank you. Yes, I completely agree. Having a convention is the most important part; I only gave my recommendations.
Indeed I did not tackle the self join issue. Thank you for your example! (I hope I fixed your code correctly?)

]]>
By: Bill Karwin https://shlomi-noach.github.io/blog/mysql/proper-sql-table-alias-use-conventions/comment-page-1#comment-11372 Thu, 11 Mar 2010 09:14:43 +0000 https://shlomi-noach.github.io/blog/?p=2156#comment-11372 Er, part of my example query above was eaten. 🙁

]]>
By: Bill Karwin https://shlomi-noach.github.io/blog/mysql/proper-sql-table-alias-use-conventions/comment-page-1#comment-11371 Thu, 11 Mar 2010 09:13:33 +0000 https://shlomi-noach.github.io/blog/?p=2156#comment-11371 As with any other coding language, the specific details of your coding conventions are not as important as *having* coding conventions, and following them more or less consistently.

I think your recommendations are fine. You are, however, missing any recommendations for how to name aliases when you use self-joins.

I find that sometimes, the alias may even be longer than the table name:

SELECT later_customer.*
FROM 
  customers AS first_customer
  JOIN customers AS later_customer ON (first_customer.cust_id = hypothetical_earlier_customer.cust_id)
WHERE 
  hypothetical_earlier_customer.cust_id IS NULL;
]]>