Selecting a column that is also a keyword in MySQL?

MySQLMySQLi Database

To select a column that is also a keyword in MySQL, you need to use backticks around the column name. As you know select is a keyword in MySQL, consider column name as select when creating a new table.

Let us create a table:

mysql> create table DemoTable (`select` varchar(100));
Query OK, 0 rows affected (0.53 sec)

Following is the query to insert some records in the table using insert command:

mysql> insert into DemoTable values('Records');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('All Data');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Information');
Query OK, 1 row affected (0.32 sec)

Following is the query to display records from the table using select command:

mysql> select *from DemoTable;

This will produce the following output:

+-------------+
| select      |
+-------------+
| Records     |
| All Data    |
| Information |
+-------------+
3 rows in set (0.00 sec)

Following is the query to select a column that is also a keyword in MySQL:

mysql> select `select` from DemoTable;

This will produce the following output:

+-------------+
| select      |
+-------------+
| Records     |
| All Data    |
| Information |
+-------------+
3 rows in set (0.00 sec)
raja
Published on 29-Apr-2019 11:51:10
Advertisements