Question

I am wondering why when I use MySQL Query Browser and double click table names the sql statements look like this:

SELECT * FROM database.table t;

where t = the first letter of the table... What is the purpose of this letter? I am just curious

Was it helpful?

Solution

The t is an alias for the table. It helps when writing queries with :

  • many columns in the select list

  • (many) joins where writing the full table name would be unreadable

    Foo f INNER JOIN Customers c on c.ID = f.CustomerID LEFT JOIN BAR b on b.ID=f.ID

  • if you wanted 2+ copies of the same table, you could alias them with different names:

    Invoices i LEFT JOIN Invoices i2 on i.ID = i2.MasterInvoiceID

  • long table/view names that would be cumbersome to keep writing/reading. Naming conventions sometimes are the culprit. Imagine a data warehouse table like:

    InvoicesThatAreOverdue_Temp_Holding_20101128

It's not required, but the MySQL Query Browser is helping promote the use of aliases. Here's hoping it helps developers write readable code!

OTHER TIPS

It is an alias that will allow you to shorten your references

For example

Select * from table1 t1
Inner Join table2 t2 on t1.PK = t2.FK

Instead of this

Select * from table1 
Inner Join table2 on table1.PK = table2.FK

It is known as alias :)

In SQL, an alias name can be given to a table or to a column. You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table names or column names.

It's a table alias. Here is a short tutorial on using aliases.

You're actually using a shortcut (alias) to give a new name to the table.

// this is the full command but you can leave out AS if you want
SELECT * FROM database.table AS t;

Table aliasing is pretty useful for larger queries when you're joining multiple tables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top