Content deleted Content added
m source lang="sql" |
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags |
||
Line 4:
In SQL, you can alias tables and [[Column (database)|columns]]. A table alias is also called a '''correlation name'''.<ref>ANSI Standard SQL – Foundation Document – Date: 2010-10-14</ref> A programmer can use an alias to temporarily assign another name to a table or column for the duration of a [[Select (SQL)|SELECT query]]. Assigning an alias does not actually rename the column or table. This is often useful when either tables or their columns have very long or complex names. An alias name could be anything, but usually it is kept short. For example, it might be common to use a table alias such as "pi" for a table named "price_information".
The general syntax of an alias is <
{| class="wikitable" style="text-align:center; float:left; margin-left:5px"
Line 22:
Using a table alias:
<
SELECT D.DepartmentName FROM Department AS D
</syntaxhighlight>
We can also write the same query like this (Note that the AS clause is omitted this time):
<
SELECT D.DepartmentName FROM Department D
</syntaxhighlight>
A column alias is similar:
<
SELECT d.DepartmentId AS Id, d.DepartmentName AS Name FROM Department d
</syntaxhighlight>
In the returned [[result set]]s, the data shown above would be returned, with the only exception being "DepartmentID" would show up as "Id", and "DepartmentName" would show up as "Name".
Line 42:
Also, if only one table is being selected and the query is not using [[Join (SQL)|table joins]], it is permissible to omit the table name or table alias from the column name in the SELECT statement. Example as follows:
<
SELECT DepartmentId AS Id, DepartmentName AS Name FROM Department d
</syntaxhighlight>
==References==
|