"/> ">
Home >Database >Mysql Tutorial >Can we use 'rank' as column name in MySQL8?
Rank is a MySQL reserved word defined in MySQL 8.0.2 version. Therefore, you cannot use rank as column name. You need to use backticks around the ranking.
Let's first check the version of MySQL we are developing 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 problem with using "rank" as column name is 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
In the above, since we are using reserved words as column names, it is ok See the error.
Let's first create a table and use backticks around "rank" to avoid errors -
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)
Use insert command to insert some records in the table -
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)
Use The select statement displays all the records in the table -
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)
The above is the detailed content of Can we use 'rank' as column name in MySQL8?. For more information, please follow other related articles on the PHP Chinese website!