
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- Fix ERROR 1064 (42000) while creating a database in MySQL?
- MySQL syntax error (in SELECT query) while using ‘group’ as table name
- What is the MySQL syntax error in this query – Creating a table with reserved keyword?
- Error occurs when table name “references” is set while creating a table
- Fix Drop table view #1051 unknown table error in MySQL
- Fix MySQL Error #1064 - You have an error in your SQL syntax… near 'TYPE=MyISAM?
- How to resolve the error that occurs while using a reserved word as a table or column name in MySQL?
- Can we use {} while creating a MySQL table?
- What is MySQL GENERATED COLUMN and how to use it while creating a table?
- Fix ERROR 1093 (HY000): You can't specify target table for update in FROM clause while deleting the lowest value from a MySQL column?
- Set AUTO_INCREMENT in a table while creating it in MySQL?
- Fix MySQL Database Error #1064?
- Set DEFAULT values for columns while creating a table in MySQL
- Declare syntax error in MySQL Workbench?
- While creating a MySQL table use the reserved keyword ‘Key’

Advertisements