<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Good Practice / Bad Practice: Table Aliases</title>
	<atom:link href="http://openquery.com/blog/good-practice-bad-practice-table-aliases/feed" rel="self" type="application/rss+xml" />
	<link>http://openquery.com/blog/good-practice-bad-practice-table-aliases</link>
	<description>About MySQL, Drizzle, MariaDB and more!</description>
	<lastBuildDate>Thu, 02 Feb 2012 21:40:24 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: arjen</title>
		<link>http://openquery.com/blog/good-practice-bad-practice-table-aliases/comment-page-1#comment-1593</link>
		<dc:creator>arjen</dc:creator>
		<pubDate>Thu, 23 Jul 2009 21:26:45 +0000</pubDate>
		<guid isPermaLink="false">http://openquery.com/blog/?p=794#comment-1593</guid>
		<description>@nag aliases are just during a query. What you describe is more something a db abstraction layer could do.</description>
		<content:encoded><![CDATA[<p>@nag aliases are just during a query. What you describe is more something a db abstraction layer could do.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nag</title>
		<link>http://openquery.com/blog/good-practice-bad-practice-table-aliases/comment-page-1#comment-1567</link>
		<dc:creator>Nag</dc:creator>
		<pubDate>Wed, 01 Jul 2009 06:34:41 +0000</pubDate>
		<guid isPermaLink="false">http://openquery.com/blog/?p=794#comment-1567</guid>
		<description>Hi All,

Ia it possible to create a table with some alias name?

i.e. while creating a table with name &#039;ReportBuildingBlock&#039; is it possible to give &#039;rbb&#039; as its alias name?

-Nag</description>
		<content:encoded><![CDATA[<p>Hi All,</p>
<p>Ia it possible to create a table with some alias name?</p>
<p>i.e. while creating a table with name &#8216;ReportBuildingBlock&#8217; is it possible to give &#8216;rbb&#8217; as its alias name?</p>
<p>-Nag</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dalibor Karlović</title>
		<link>http://openquery.com/blog/good-practice-bad-practice-table-aliases/comment-page-1#comment-1556</link>
		<dc:creator>Dalibor Karlović</dc:creator>
		<pubDate>Mon, 29 Jun 2009 08:18:43 +0000</pubDate>
		<guid isPermaLink="false">http://openquery.com/blog/?p=794#comment-1556</guid>
		<description>I find

SELECT
    City.name,
    City.district,
    Country.name,
    City.population
FROM
    Country
    INNER JOIN City ON City.countrycode = Country.code
WHERE
    Country.continent = &#039;africa&#039; AND
    City.population BETWEEN 1000000 AND 1500000
ORDER BY
    City.population desc
LIMIT 10;

the most readable, although I always use all small caps for table and column names. For the &quot;changing the table name&quot; issue, you can always use:

FROM
    country AS country

and change the name as you&#039;d like. Have a look at this:
http://it.toolbox.com/blogs/database-soup/writing-maintainable-queries-part-i-29052</description>
		<content:encoded><![CDATA[<p>I find</p>
<p>SELECT<br />
    City.name,<br />
    City.district,<br />
    Country.name,<br />
    City.population<br />
FROM<br />
    Country<br />
    INNER JOIN City ON City.countrycode = Country.code<br />
WHERE<br />
    Country.continent = &#8216;africa&#8217; AND<br />
    City.population BETWEEN 1000000 AND 1500000<br />
ORDER BY<br />
    City.population desc<br />
LIMIT 10;</p>
<p>the most readable, although I always use all small caps for table and column names. For the &#8220;changing the table name&#8221; issue, you can always use:</p>
<p>FROM<br />
    country AS country</p>
<p>and change the name as you&#8217;d like. Have a look at this:<br />
<a href="http://it.toolbox.com/blogs/database-soup/writing-maintainable-queries-part-i-29052" rel="nofollow">http://it.toolbox.com/blogs/database-soup/writing-maintainable-queries-part-i-29052</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill Karwin</title>
		<link>http://openquery.com/blog/good-practice-bad-practice-table-aliases/comment-page-1#comment-1548</link>
		<dc:creator>Bill Karwin</dc:creator>
		<pubDate>Fri, 26 Jun 2009 17:34:50 +0000</pubDate>
		<guid isPermaLink="false">http://openquery.com/blog/?p=794#comment-1548</guid>
		<description>I suppose choosing short aliases is similar to choosing short variable names in any application code.  They&#039;re better when they&#039;re mnemonic.</description>
		<content:encoded><![CDATA[<p>I suppose choosing short aliases is similar to choosing short variable names in any application code.  They&#8217;re better when they&#8217;re mnemonic.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: walter</title>
		<link>http://openquery.com/blog/good-practice-bad-practice-table-aliases/comment-page-1#comment-1534</link>
		<dc:creator>walter</dc:creator>
		<pubDate>Mon, 22 Jun 2009 21:46:41 +0000</pubDate>
		<guid isPermaLink="false">http://openquery.com/blog/?p=794#comment-1534</guid>
		<description>Nice to see a discussion over something relatively simple!

@sheeri: I guess that kind of depends on your job. As a consultannt/dba/sysadmin, you are right. As a developerr, you will spend a lot more time making/changing queries.

Besides that, as illustrated with Roland&#039;s comment before, it is important to pick aliasses that make sense. If you are writing a one time usage query a one letter alias is fine. If you are writing queries that endd up in an application, a wisely chosen alias makes a whole lot of sense.</description>
		<content:encoded><![CDATA[<p>Nice to see a discussion over something relatively simple!</p>
<p>@sheeri: I guess that kind of depends on your job. As a consultannt/dba/sysadmin, you are right. As a developerr, you will spend a lot more time making/changing queries.</p>
<p>Besides that, as illustrated with Roland&#8217;s comment before, it is important to pick aliasses that make sense. If you are writing a one time usage query a one letter alias is fine. If you are writing queries that endd up in an application, a wisely chosen alias makes a whole lot of sense.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sheeri K. Cabral</title>
		<link>http://openquery.com/blog/good-practice-bad-practice-table-aliases/comment-page-1#comment-1532</link>
		<dc:creator>Sheeri K. Cabral</dc:creator>
		<pubDate>Mon, 22 Jun 2009 20:38:23 +0000</pubDate>
		<guid isPermaLink="false">http://openquery.com/blog/?p=794#comment-1532</guid>
		<description>As others have pointed out, table aliases are not always the best idea.

One problem with using the shortest possible alias is when you have a long query with multiple joins, and are trying to decipher the EXPLAIN plan...do you really want something like this?

mysql&gt; EXPLAIN select count(*) from cpu_stats\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: co
         type: index
possible_keys: NULL
          key: PRIMARY
      key_len: 7
          ref: NULL
         rows: 1
        Extra: Using index

for each table, in the EXPLAIN plan?  

As Roland says, be careful not to make the aliases too short.  It doesn&#039;t help to have aliases if you can&#039;t make them refer back to what they need to.

You&#039;re spot on about that &quot;what happens if you change the table name&quot; but that happens way less often in real life than finding a slow query, running the EXPLAIN plan, and getting a 7-way join of tables called &quot;r&quot;, &quot;p&quot;, and &quot;s&quot;.</description>
		<content:encoded><![CDATA[<p>As others have pointed out, table aliases are not always the best idea.</p>
<p>One problem with using the shortest possible alias is when you have a long query with multiple joins, and are trying to decipher the EXPLAIN plan&#8230;do you really want something like this?</p>
<p>mysql&gt; EXPLAIN select count(*) from cpu_stats\G<br />
*************************** 1. row ***************************<br />
           id: 1<br />
  select_type: SIMPLE<br />
        table: co<br />
         type: index<br />
possible_keys: NULL<br />
          key: PRIMARY<br />
      key_len: 7<br />
          ref: NULL<br />
         rows: 1<br />
        Extra: Using index</p>
<p>for each table, in the EXPLAIN plan?  </p>
<p>As Roland says, be careful not to make the aliases too short.  It doesn&#8217;t help to have aliases if you can&#8217;t make them refer back to what they need to.</p>
<p>You&#8217;re spot on about that &#8220;what happens if you change the table name&#8221; but that happens way less often in real life than finding a slow query, running the EXPLAIN plan, and getting a 7-way join of tables called &#8220;r&#8221;, &#8220;p&#8221;, and &#8220;s&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shlomi Noach</title>
		<link>http://openquery.com/blog/good-practice-bad-practice-table-aliases/comment-page-1#comment-1524</link>
		<dc:creator>Shlomi Noach</dc:creator>
		<pubDate>Sun, 21 Jun 2009 05:10:42 +0000</pubDate>
		<guid isPermaLink="false">http://openquery.com/blog/?p=794#comment-1524</guid>
		<description>Hi,

I actually try and not use table aliases, unless self-joining. I do make a point of using fully-qualified column names everywhere.
This leads for much longer queries, granted, but I personally don;t like the use of 2-3 letter aliases, since it&#039;s another thing to remember and associate. Instead of remembering a table&#039;s name, I also need to remember its abbreviation. When you have dozens or more of tables, this becomes difficult.
I also try and have meaningful table names. They could be long.
It is later easier for me to grep or otherwise search my application code for any reference to a specific column.

The exception is when I have a single table query. In those cases I might drop using fully qualified column names.

Point #3 is good point, but I think rare.

Regards</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I actually try and not use table aliases, unless self-joining. I do make a point of using fully-qualified column names everywhere.<br />
This leads for much longer queries, granted, but I personally don;t like the use of 2-3 letter aliases, since it&#8217;s another thing to remember and associate. Instead of remembering a table&#8217;s name, I also need to remember its abbreviation. When you have dozens or more of tables, this becomes difficult.<br />
I also try and have meaningful table names. They could be long.<br />
It is later easier for me to grep or otherwise search my application code for any reference to a specific column.</p>
<p>The exception is when I have a single table query. In those cases I might drop using fully qualified column names.</p>
<p>Point #3 is good point, but I think rare.</p>
<p>Regards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob Smith</title>
		<link>http://openquery.com/blog/good-practice-bad-practice-table-aliases/comment-page-1#comment-1523</link>
		<dc:creator>Rob Smith</dc:creator>
		<pubDate>Sat, 20 Jun 2009 16:14:43 +0000</pubDate>
		<guid isPermaLink="false">http://openquery.com/blog/?p=794#comment-1523</guid>
		<description>Howdy,

While I agree all fields should be referenced in table.field format, I&#039;m actually not for the idea of always aliasing tables with short aliases.

I feel that:
select
  City.name,
  City.District,
  Country.name,
  City.population
from
  Country
  inner join City on City.countrycode = Country.code
where
  Country.continent = &#039;africa&#039; and
  City.population between 1000000 and 1500000
order by
  City.population desc
limit 10;

Is way more readable then your &#039;best&#039; practice. I also am very much a fan of using all caps for sql keywords, it splits up what is data fields from sql keywords a lot better visually IMHO.</description>
		<content:encoded><![CDATA[<p>Howdy,</p>
<p>While I agree all fields should be referenced in table.field format, I&#8217;m actually not for the idea of always aliasing tables with short aliases.</p>
<p>I feel that:<br />
select<br />
  City.name,<br />
  City.District,<br />
  Country.name,<br />
  City.population<br />
from<br />
  Country<br />
  inner join City on City.countrycode = Country.code<br />
where<br />
  Country.continent = &#8216;africa&#8217; and<br />
  City.population between 1000000 and 1500000<br />
order by<br />
  City.population desc<br />
limit 10;</p>
<p>Is way more readable then your &#8216;best&#8217; practice. I also am very much a fan of using all caps for sql keywords, it splits up what is data fields from sql keywords a lot better visually IMHO.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Walter</title>
		<link>http://openquery.com/blog/good-practice-bad-practice-table-aliases/comment-page-1#comment-1521</link>
		<dc:creator>Walter</dc:creator>
		<pubDate>Sat, 20 Jun 2009 12:49:41 +0000</pubDate>
		<guid isPermaLink="false">http://openquery.com/blog/?p=794#comment-1521</guid>
		<description>@gregory: I don&#039;t have standards carved in stone with names and such. I like for instance and id column in a table to be called id, and not employee_id. I usually try to stick with what feels good and sane when naming columns. That means I could have a column called &#039;name&#039; in a table &#039;parts&#039;, but I would have a longer name in case there are two &#039;name&#039; attributes that need to be stored in a table. Sounds confusing, I know, but it makes sense in practice.

@Ronald: Totally agree with what you say. Back when I was programming fulltime, I usually maintained 1 big application at a time. I found myself often using the same abbreviations for the same tables, making it easy to recognise. 
Now that I see different databases every day and my queries don&#039;t end up in applications but they are just for me checking stuff out, I tend to just go with my approach described above.</description>
		<content:encoded><![CDATA[<p>@gregory: I don&#8217;t have standards carved in stone with names and such. I like for instance and id column in a table to be called id, and not employee_id. I usually try to stick with what feels good and sane when naming columns. That means I could have a column called &#8216;name&#8217; in a table &#8216;parts&#8217;, but I would have a longer name in case there are two &#8216;name&#8217; attributes that need to be stored in a table. Sounds confusing, I know, but it makes sense in practice.</p>
<p>@Ronald: Totally agree with what you say. Back when I was programming fulltime, I usually maintained 1 big application at a time. I found myself often using the same abbreviations for the same tables, making it easy to recognise.<br />
Now that I see different databases every day and my queries don&#8217;t end up in applications but they are just for me checking stuff out, I tend to just go with my approach described above.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roland Bouman</title>
		<link>http://openquery.com/blog/good-practice-bad-practice-table-aliases/comment-page-1#comment-1520</link>
		<dc:creator>Roland Bouman</dc:creator>
		<pubDate>Sat, 20 Jun 2009 07:27:22 +0000</pubDate>
		<guid isPermaLink="false">http://openquery.com/blog/?p=794#comment-1520</guid>
		<description>Hi!

yeah, I mostly agree. I find I mostly go by this convention.However, I usually end up with 3 letter table name abbreviations for the aliases. I mean, when you make each alias as short as possible, you would have some alias of 1 letter, some with 2 and so on, which I found confusing. I found that 3 letters is usually enough to get unique aliases for all tables.

So I usually make 3 letter aliases, which I derive directly from the table name. My aliasses start with the first letter of the table name, and end with the last letter of the table name. The second letter is  usually the second letter of the table name, or the first letter of the second syllable of the table name. This usually gives me good, unique aliases that are still easy recognizable and specific for their table.

Something I kind of missed in your post is reusing the same aliases for all queries in the same schema. I mean, the applicaitions I maintain tend to use the same aliases over and over in different queries. Of course when dealing with a new schema you need some time to know all tables so it may take some time to let a convention sink in. Having a rule like I just described for generating aliases helps a lot here. 

It is always a challenge to deal with self-joins. I find that in these cases, it is best to use an alias that defines in what role the table plays. Because the role names are normally not visiable as database identifiers I tend to not use 3 letter aliases here. For two way self joins I tried to use &quot;sub&quot; and &quot;sup&quot; for the child and the parent, but I found it doesn&#039;t work too well for me - it&#039;s just too similar. Even using &quot;parent&quot; and &quot;child&quot; doesnt work, it&#039;s too abstract. So I end up with full names in this case: &quot;boss&quot; and &quot;employee&quot;. 

kind regards,

Roland Bouman</description>
		<content:encoded><![CDATA[<p>Hi!</p>
<p>yeah, I mostly agree. I find I mostly go by this convention.However, I usually end up with 3 letter table name abbreviations for the aliases. I mean, when you make each alias as short as possible, you would have some alias of 1 letter, some with 2 and so on, which I found confusing. I found that 3 letters is usually enough to get unique aliases for all tables.</p>
<p>So I usually make 3 letter aliases, which I derive directly from the table name. My aliasses start with the first letter of the table name, and end with the last letter of the table name. The second letter is  usually the second letter of the table name, or the first letter of the second syllable of the table name. This usually gives me good, unique aliases that are still easy recognizable and specific for their table.</p>
<p>Something I kind of missed in your post is reusing the same aliases for all queries in the same schema. I mean, the applicaitions I maintain tend to use the same aliases over and over in different queries. Of course when dealing with a new schema you need some time to know all tables so it may take some time to let a convention sink in. Having a rule like I just described for generating aliases helps a lot here. </p>
<p>It is always a challenge to deal with self-joins. I find that in these cases, it is best to use an alias that defines in what role the table plays. Because the role names are normally not visiable as database identifiers I tend to not use 3 letter aliases here. For two way self joins I tried to use &#8220;sub&#8221; and &#8220;sup&#8221; for the child and the parent, but I found it doesn&#8217;t work too well for me &#8211; it&#8217;s just too similar. Even using &#8220;parent&#8221; and &#8220;child&#8221; doesnt work, it&#8217;s too abstract. So I end up with full names in this case: &#8220;boss&#8221; and &#8220;employee&#8221;. </p>
<p>kind regards,</p>
<p>Roland Bouman</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
