我们可以使用"rank"作为 MySQL8 的列名吗?

mysqlmysqli database更新于 2024/8/11 7:40:17

rank 是 MySQL 版本 8.0.2 中定义的 MySQL 保留字。因此,您不能使用 rank 作为列名。您需要在 rank 周围使用反引号。

让我们首先检查我们正在使用的 MySQL 版本。在这里,我使用的是 MySQL 版本 8.0.12 −

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

使用“rank”作为列名的问题如下 −

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

上面,由于我们使用保留字作为列名,因此可以看到错误。

让我们首先创建一个表,并在"rank"两边使用反引号以避免错误 −

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 插入命令在表中插入一些记录 −

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)

使用 select 语句显示表中的所有记录 −

mysql> select * from DemoTable1596;

这将产生以下输出 −

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


相关文章


有用资源