Reputation: 22760
So there's a question about MySQL aliases for table names, and it has raised me to ask this question here:
Why is the use of aliases in naming tables in MySQL queries treated as a pseudo- standard behaviour rather than as behaviour needed only in certain situations?
Take the following example from the question linked above:
SELECT st.StudentName, cl.ClassName
FROM StudentClass sc
INNER JOIN Classes cl ON cl.ClassID = sc.ClassID
INNER JOIN Students st ON st.StudentID = sc.StudentID;
From my experience the alias for the tables is usually unneeded (some might say pseudo-random, spacing filling) letters and can just as easily, and more readably be:
SELECT Students.StudentName, Classes.ClassName
FROM StudentClass
INNER JOIN Classes ON Classes.ClassID = StudentClass.ClassID
INNER JOIN Students ON Students.StudentID = StudentClass.StudentID;
Obviously it may -in some situations- be better to use shortened naming convention, perhaps for a large query with many tables each of long names, but that's no reason (as far as I can see) for the absolute over the top prevelance of this methodology of forming an alias for each table, regardless of need.
I have googled this, but the majority of useful results state that it makes "the SQL more readable". That's as maybe for many or long-named tables in a Query as I've already state, but as an apparant standard??
Also, without the alias, it's clear to see the source table of each of the columns in the exampled SQL above.
I just want to see if there's some key methodology I'm completely missing here?
To qualify why I feel the need to ask if I'm missing something:
I see it an aweful lot on StackOverflow (as referenced here), which in itself means jack nothing, but then I see that there are no responses from knowledgable (high scoring) answerers that aliases are not needed (such as in the referenced post above), whereas other topics on SO those who deem themselves to know better (high scorers) are all over teling people how things should be done.
It leaves me unsure If I'm missing something, hence this question.
A comment by A MySQL authorized instructor and DBA circa 2010.
Upvotes: 0
Views: 85
Reputation: 108651
I think you may be mistaken that giving aliases to tables is somehow the standard.
There are two situations where it's required.
When the same table is joined more than once in a query. Aliases are needed to distinguish the two usages of the table.
A derived table (a subquery) needs an alias.
Other than that you can omit aliases to your heart's content.
Upvotes: 3