Create a temporary name using ALIAS in SQL
SQL ALIAS is used to create a temporary name (called an alias) for a column or table. This command allows you to change the temporary name for a table or column with a different name in the database.
- 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_tamthoi
WHERE [dieu_kien];
The syntax of COLUMN ALIAS (alias column) is as follows:
SELECT ten_cot AS ten_tamthoi
FROM ten_bang
WHERE [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.SOTIEN
FROM NHANVIEN AS C, TIENTHUONG AS O
WHERE 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_TEN
FROM NHANVIEN
WHERE 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, Quantrimang 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
You should read it
- How to Create a Shortcut (Alias) for a File or Folder on Mac
- ALIAS in SQL Server
- String (String) in C #
- 13 important SQL statements Programmer needs to know
- Create 5 virtual accounts from a primary account in Hotmail
- Laptop screen OLED carries 'alias' Rolltop
- Reference in C ++
- Sub-commands in nslookup Windows command (Part 2)
May be interested
- Index (INDEX) in SQLsql index (index) is a special lookup table that database search engines can use to quickly increase the time and performance of data retrieval.
- ALTER TABLE statement in SQLthe alter table statement in sql is used to add, delete, and modify columns in an existing table.
- Adjust performance in SQL Server: find slow queriesadjusting sql performance is a never-ending battle. this article will provide some tips for you to find slow sql queries and perform performance tuning in sql server.
- Use code to quickly create T-SQL scripts in SQL Operations Studio (preview)the code in sql operations studio (preview) is the template that makes it easier to create databases and database objects.
- Manage servers and databases with detailed utilities in SQL Operations Studio (preview)detailed utilities retrieve the transact-sql (t-sql) queries you use to monitor servers and databases, then turn them into visual images.
- UPDATE command in SQLupdate is the query used to edit existing records in the table. you can use the where clause with the update statement to update selected rows, if not all rows in the table are affected.