Are quotes around tables and columns in a MySQL query really necessary?


If your table name or column name are any reserved words then you need to use quotes around table name and column name in a MySQL query. You need to use backticks around table name and column name. The syntax is as follows:

SELECT *FROM `table` where `where`=condition;

Here is the query to create a table without quotes with reserved words. You will get an error since they are predefined reserved words. The error is as follows:

mysql> create table table
   -> (
   -> where int
   -> );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table
(
   where int
)' at line 1

Let us now include quotes around the name of the table and column since ‘table’ and ‘where’ are reserved words. Here is the query with quotes:

mysql> create table `table`
   -> (
   -> `where` int
   -> );
Query OK, 0 rows affected (0.55 sec)

Insert records in the table using insert command. The query is as follows:

mysql> insert into `table`(`where`) values(1);
Query OK, 1 row affected (0.13 sec)
mysql> insert into `table`(`where`) values(100);
Query OK, 1 row affected (0.26 sec)
mysql> insert into `table`(`where`) values(1000);
Query OK, 1 row affected (0.13 sec)

Display a specific record from the table with the help of where condition. The query is as follows:

mysql> select *from `table` where `where`=100;

The following is the output:

+-------+
| where |
+-------+
| 100   |
+-------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

349 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements