Is there a way to create a MySQL “alias” while creating a VIEW?


Yes, use the AS keyword to create a MySQL alias. Let us first create a table −

mysql> create table DemoTable
(
   FirstName varchar(100)
);
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Chris');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable values('Sam');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+
| FirstName |
+-----------+
| Chris     |
| Robert    |
| Sam       |
+-----------+
3 rows in set (0.00 sec)

Following is the query to create a MySQL “alias” −

mysql> create view load_data AS select *from DemoTable;
Query OK, 0 rows affected (0.22 sec)

Let us check the records −

mysql> select *from load_data;

This will produce the following output −

+-----------+
| FirstName |
+-----------+
| Chris     |
| Robert    |
| Sam       |
+-----------+
3 rows in set (0.04 sec)

Updated on: 01-Oct-2019

164 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements