mysql> CREATE TABLE keys (id INT(10), key VARCHAR(100));
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 'keys (id INT(10), key VARCHAR(100))' at line 1
mysql> CREATE TABLE `keys` (id INT(10), `key` VARCHAR(100));
Query OK, 0 rows affected, 1 warning (0.97 sec)
mysql> show create table keys;
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 'keys' at line 1
mysql>
mysql>
mysql> show create table `keys`;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| keys | CREATE TABLE `keys` (
`id` int DEFAULT NULL,
`key` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
你应该非常小心地命名表和列,因为你将来可能会面临很多问题。
我建议找到那些不是MySQL保留字的名称。
如果你还想保留这些名称,你应该用反引号将其括起来。
请注意,在使用表时必须使用反引号
所以表名
keys
和字段key
都是在Mysql命名空间中保留的。如果您选择一个不同的表名(例如keys_tbl
)并将第二个字段重命名为key_id
,您的代码将正常工作。