Can we use reserved word ‘index’ as MySQL column name?

MySQLMySQLi Database

Yes, but you need to add a backtick symbol to the reserved word (index) to avoid error while using it as a column name.

Let us first create a table −

mysql> create table DemoTable
(
   `index` int
);
Query OK, 0 rows affected (0.48 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(1000);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(1020);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(967);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(567);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values(1010);
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| index |
+-------+
| 1000  |
| 1020  |
| 967   |
| 567   |
| 1010  |
+-------+
5 rows in set (0.00 sec)

Now let us display some records with our column name `index`. Here, we are displaying 3 records −

mysql> select *from DemoTable order by `index` DESC LIMIT 3;

This will produce the following output −

+-------+
| index |
+-------+
| 1020  |
| 1010  |
| 1000  |
+-------+
3 rows in set (0.00 sec)
raja
Published on 27-Sep-2019 10:04:34
Advertisements