Table of Contents
This step-by-step guide explains how to create a temporary name using alias in SQL. It covers the required setup, the main actions, and the checks that help prevent common problems.
- TABLE ALIAS Is used to shorten SQL for easier readability or when you need to manually connect (for example, listing the same table more than once in the FROM clause).
- COLUMN ALIAS Is used to create column headers in the results for easy viewing or meeting the purpose of a particular SQL query.
Changing this name is temporary and does not change the original table name in the database.
Syntax
The syntax of TABLE ALIAS (table alias) is as follows:
SELECT cot1, cot2.FROM ten_bang AS ten_tamthoiWHERE [dieu_kien];
The syntax of COLUMN ALIAS (alias column) is as follows:
SELECT ten_cot AS ten_tamthoiFROM ten_bangWHERE [dieu_kien];
Example of Using ALIAS
Suppose the two tables are NHANVIEN And TIENTHUONG With the following records:
Table 1: NHANVIEN
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
Table 2: TIENTHUONG
+-----+---------------------+-------------+--------+ |TT_ID| NGAY | NHANVIEN_ID | SOTIEN | +-----+---------------------+-------------+--------+ | 102 | 2019-01-08 00:00:00 | 3 | 3000 | | 100 | 2019-01-08 00:00:00 | 3 | 1500 | | 101 | 2019-02-20 00:00:00 | 2 | 1560 | | 103 | 2018-12-20 00:00:00 | 4 | 2060 | +-----+---------------------+-------------+--------+
Here's how to use TABLE ALIAS In SQL:
SQL> SELECT C.ID, C.TEN, C.TUOI, O.SOTIENFROM NHANVIEN AS C, TIENTHUONG AS OWHERE C.ID = O.NHANVIEN_ID;
The result is:
+----+----------+-----+--------+ | ID | NAME | AGE | AMOUNT | +----+----------+-----+--------+ | 3 | Nga | 23 | 3000 | | 3 | Nga | 23 | 1500 | | 2 | Loan | 25 | 1560 | | 4 | Manh | 25 | 2060 | +----+----------+-----+--------+
And the usage of COLUMN ALIAS In SQL is as follows:
SQL> SELECT ID AS NHANVIEN_ID, TEN AS NHANVIEN_TENFROM NHANVIENWHERE LUONG IS NOT NULL;
The above example will return the result:
+-------------+---------------+ | NHANVIEN_ID | NHANVIEN_NAME | +-------------+---------------+ | 1 | Thanh | | 2 | Loan | | 3 | Nga | | 4 | Manh | | 5 | Huy | | 6 | Cao | | 7 | Lam | +-------------+---------------+
In the next articles, TipsMake will discuss with you the index (INDEX) in SQL. Have you remembered to it!
Previous article: NULL value in SQL
Next article: Index (INDEX) in SQL
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.