当表名"match"没有用单引号括起来时,MySQL 会抛出错误?

mysqlmysqli database

不要使用单引号。您需要在表名 match 周围使用反引号,因为它是 MySQL 中的保留名称。以下是发生的错误:

mysql> select *from match;
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 'match' at line 1

让我们首先创建一个表,并使用保留字 match 周围的反引号(此处用作表名)修复上述错误的发生 −

mysql> create table `match`
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   PlayerName varchar(20)
);
Query OK, 0 rows affected (0.62 sec)

使用 insert 命令在表中插入一些记录。现在,无论使用保留字在哪里,都用反引号括起来 −

mysql> insert into `match`(PlayerName) values('Chris');
Query OK, 1 row affected (0.13 sec)
mysql> insert into `match`(PlayerName) values('Bob');
Query OK, 1 row affected (0.16 sec)
mysql> insert into `match`(PlayerName) values('David');
Query OK, 1 row affected (0.24 sec)
mysql> insert into `match`(PlayerName) values('Mike');
Query OK, 1 row affected (0.15 sec)
mysql> insert into `match`(PlayerName) values('Sam');
Query OK, 1 row affected (0.14 sec)

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

mysql> select *from `match`;

这将产生以下输出 −

+----+------------+
| Id | PlayerName |
+----+------------+
|  1 | Chris      |
|  2 | Bob        |
|  3 | David      |
|  4 | Mike       |
|  5 | Sam        |
+----+------------+
5 rows in set (0.00 sec)

相关文章