Home > Database > Mysql Tutorial > Can we use 'rank' as column name in MySQL8?

Can we use 'rank' as column name in MySQL8?

王林
Release: 2023-09-07 13:41:02
forward
1128 people have browsed it

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)
Copy after login

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
Copy after login

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)
Copy after login

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)
Copy after login

Use The select statement displays all the records in the table -

mysql> select * from DemoTable1596;
Copy after login

This will produce the following output -

+----+-------------+------+
| Id | StudentName | rank |
+----+-------------+------+
|  1 | Bob         | 4567 |
|  2 | David       |    1 |
+----+-------------+------+
2 rows in set (0.00 sec)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template