Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Table Aliases - Good or Bad? [closed]

Tags:

alias

sql

What are the pros and cons of using table aliases in SQL? I personally try to avoid them, as I think they make the code less readable (especially when reading through large where/and statements), but I'd be interested in hearing any counter-points to this. When is it generally a good idea to use table aliases, and do you have any preferred formats?

like image 332
tbreffni Avatar asked Aug 14 '08 13:08

tbreffni


People also ask

Does table aliases improve performance?

The alias doesn't affect performance in any practical or measurable way at all (italics added on edit). That is, it would add a barely (if it all) measurable delay to query compilation. Once compiled (and re-used), it has no effect.

What is the advantage of alias in SQL?

Benefits of SQL AliasesAllows client applications to refer to a calculated field by name where no column name exists. Allows you to reduce code and make your queries more concise. Can be used as an obfuscation technique to protect the names of the underlying columns of a query.

Why would you want to use a table alias in a query?

TABLE ALIASES are used to shorten your SQL to make it easier to read or when you are performing a self join (ie: listing the same table more than once in the FROM clause).

How should an alias be defined if it has a space in it SQL?

Double quotes for alias with a space.


2 Answers

Table aliases are a necessary evil when dealing with highly normalized schemas. For example, and I'm not the architect on this DB so bear with me, it can take 7 joins in order to get a clean and complete record back which includes a person's name, address, phone number and company affiliation.

Rather than the somewhat standard single character aliases, I tend to favor short word aliases so the above example's SQL ends up looking like:

select person.FirstName       ,person.LastName       ,addr.StreetAddress       ,addr.City       ,addr.State       ,addr.Zip       ,phone.PhoneNumber       ,company.CompanyName from tblPeople person left outer join tblAffiliations affl on affl.personID = person.personID left outer join tblCompany company on company.companyID = affl.companyID 

... etc

like image 73
Rob Allen Avatar answered Oct 15 '22 00:10

Rob Allen


Well, there are some cases you must use them, like when you need to join to the same table twice in one query.

It also depends on wether you have unique column names across tables. In our legacy database we have 3-letter prefixes for all columns, stemming from an abbreviated form from the table, simply because one ancient database system we were once compatible with didn't support table aliases all that well.

If you have column names that occur in more than one table, specifying the table name as part of the column reference is a must, and thus a table alias will allow for a shorter syntax.

like image 35
Lasse V. Karlsen Avatar answered Oct 15 '22 00:10

Lasse V. Karlsen