Fix Error in MySQL syntax while creating a table column with name “index”?


You can’t use the index as a column name because it is a reserved word. For this, you need to use backticks around the column name.

If you will use a reserved word as the column name, you can see the following error−

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> index int
   -> )ENGINE=MyISAM;
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 'int )ENGINE=MyISAM' at line 4

Let us first create a table. Here, we are using a reserved word index as the column name, but enclosing it with backticks won’t give an error−

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> `index` int
   -> )ENGINE=MyISAM;
Query OK, 0 rows affected (0.28 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(`index`) values(4);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable(`index`) values(8);
Query OK, 1 row affected (0.07 sec)
mysql> insert into DemoTable(`index`) values(12);
Query OK, 1 row affected (0.06 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+-------+
| Id | index |
+----+-------+
|  1 |     4 |
|  2 |     8 |
|  3 |    12 |
+----+-------+
3 rows in set (0.00 sec)

Updated on: 12-Dec-2019

635 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements