Can we use MySQL keyword as alias name for a column?

MySQLMySQLi Database

Yes, we can use a keyword as alias name for a column. Following is the syntax −

select yourColumnName AS `yourKeywordAsAliasName` from yourTableName;

Above, yourKeywordAsAliasName is the MySQL keyword.

Let us first create a table −

mysql> create table DemoTable (UserId int);
Query OK, 0 rows affected (0.74 sec)

Example

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(11);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(12);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(13);
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+--------+
| UserId |
+--------+
| 10     |
| 11     |
| 12     |
| 13     |
+--------+
4 rows in set (0.00 sec)

Here is the query to use MySQL keyword as alias name for a column. We have used the keyword ‘PRIMARY KEY’ as the alias name for a column −

mysql> select UserId AS `PRIMARY KEY` from DemoTable;

Output

+-------------+
| PRIMARY KEY |
+-------------+
| 10          |
| 11          |
| 12          |
| 13          |
+-------------+
4 rows in set (0.00 sec)
raja
Published on 20-Aug-2019 14:48:30
Advertisements