Can we use “rank” as column name with MySQL8?

MySQLMySQLi Database

The rank is a MySQL reserved word defined in MySQL version 8.0.2. Therefore, you cannot use rank as a column name. You need to use backticks around the rank.

Let us first check the MySQL version we are working on. Here, I am using MySQL version 8.0.12 −

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.12    |
+-----------+
1 row in set (0.00 sec)

The issues by using “rank” as column name are as follows −

mysql> create table DemoTable1596
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(20),
   -> rank 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 'rank int
)' at line 5

Above, an error is visible since we used reserved word as column name.

Let us first create a table and use backticks around “rank” to avoid error −

mysql> create table DemoTable1596
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(20),
   -> `rank` int
   -> );
Query OK, 0 rows affected (0.51 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1596(StudentName,`rank`) values('Bob',4567);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1596(StudentName,`rank`) values('David',1);
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1596;

This will produce the following output −

+----+-------------+------+
| Id | StudentName | rank |
+----+-------------+------+
|  1 | Bob         | 4567 |
|  2 | David       |    1 |
+----+-------------+------+
2 rows in set (0.00 sec)
raja
Published on 16-Dec-2019 11:48:15
Advertisements