What is the MySQL syntax error in this query – Creating a table with reserved keyword?


Let’s say we tried creating a table with name “groups”, which is a reserved keyword in MySQL You cannot use “groups” because groups is a reserved keyword in MySQL.

Following error occurred while creating a table with name “groups” −

mysql> create table groups
−> (
−> id int,
−> name varchar(40)
−> );
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 'groups
(
id int,
name varchar(40)
)' at line 1

In order to create a table with reserved keyword, you need to use the concept of backticks(``).

Let us create a table −

mysql> create table `groups`
-> (
−> id int,
−> name varchar(40)
−> )
−> ;
Query OK, 0 rows affected (3.08 sec)

Insert some records into the table with the help of insert command −

mysql> insert into `groups` values(10,'John');
Query OK, 1 row affected (0.30 sec)

mysql> insert into `groups` values(11,'Bob');
Query OK, 1 row affected (0.32 sec)

mysql> insert into `groups` values(12,'Mike');
Query OK, 1 row affected (0.40 sec)

Display records from the table using select statement

mysql> select *from `groups`;

This will produce the following output −

+------+------+
| id   | name |
+------+------+
|   10 | John |
|   11 | Bob  |
|   12 | Mike |
+------+------+
3 rows in set (0.04 sec)

Updated on: 19-Nov-2020

361 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements