SQL Aliases
SQL alias는 table, column에 일시적인 이름을 부여할 수 있다.
An alias only exists for the duration of the query.(?)
Alias Column Syntax
SELECT column_name AS alias_name
FROM table_name;
Alias Table Syntax
SELECT column_name(s)
FROM table_name AS alias_name;
Alias for Columns Examples
- CustomerID를 ID로, CustomerName을 Customer로 하는 aliases생성하기
SELECT CustomerID as ID, CustomerName AS Customer
FROM Customers;
- CustomerName을 Customer로, ContactName을 Contact Person으로 하는 aliases 생성하기
SELECT CustomerName AS Customer, ContactName AS [Contact Person]
FROM Customers;
- Address, PostalCode, City, Country를 포함하는 Adress alias 생성하기
SELECT CustomerName,
Address + ', ' + PostalCode + ' ' + City + ', ' + Country AS Address
From Customers;
MySQL에서는 다음과 같이 사용
SELECT CustomerName,
CONCAT(Address,', ',PostalCode,', ',City,', ',Country) AS Address
FROM Customers;
Alias for Tabe Example
- CustomerName이 Around the Horn이고, Customer 테이블의 ID와 Order 테이블의 ID가 같을 때 Order테이블에서 OrderID, OrderDate, Customer테이블에서 CustomerName을 가져옴
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName="Around the Horn" AND c.CustomerID=o.CustomerID;
aliases없이 사용했다면
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName="Around the Horn" AND Customers.CustomerID=Orders.CustomerID;
- There are more than one table involved in a query
- Functions are used in the query
- Column names are big or not very readable
- Two or more columns are combined together
Comments