Column Alias
In MySQL, a column alias is a temporary name assigned to a column in a query result, providing a more readable or meaningful identifier. The AS keyword is optional when assigning column aliases.
Syntax:
SELECT column_name AS alias_name FROM table_name;
Here, column_name is the column in a table, alias_name is a temporary alias name to be used in replacement of the original column name, and table_name is a table name.
1. Create Table
Create a table using CREATE TABLE command.
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
salary DECIMAL(10, 2)
);
2. Insert data into a table
Insert data in the table created in the previous step, using the following command:
INSERT INTO employees VALUES (1,'A','J',10000000);
3. Query the table
Now query the table using the following command:
SELECT first_name AS 'First Name', last_name AS 'Last Name', salary AS 'Monthly Salary'
FROM employees;
This SQL statement creates three aliases, one for the first_name, another for the last_name, and the other one for salary. The resulting query will display this column with specified aliases which will make the output more readable.
MySQL Aliases
MySQL server is an open-source relational database management system that is a major support for web-based applications. Databases and related tables are the main components of many websites and applications as the data is stored and exchanged over the web. Even all social networking websites mainly Facebook, Twitter, and Google depend on MySQL data which are designed and optimized for such purpose. For all these reasons, MySQL servers become the default choice for web applications.
In MySQL, aliases are used to make the content of your database more readable. Aliases are used to give columns or tables a temporary name.
- Aliases are often used to make column names more readable.
- This makes the result of the query more clear.
- There are two aliases in MySQL, which are column alias and table alias.
- In column alias, you give a name to a column using the ‘AS’ keyword.
- Table aliases are mostly used in performing complex joins where you need to write table names many times, by using aliases this can be minimized.
- The renaming is just a temporary change and the table name does not change in the original database.