3

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

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Richard
  • 15,152
  • 31
  • 85
  • 111

5 Answers5

7

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!

p.campbell
  • 98,673
  • 67
  • 256
  • 322
3

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
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
2

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.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1

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.

Frankie
  • 24,627
  • 10
  • 79
  • 121
1

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

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174