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
,您的程式碼將正常運作。