我有一个如下所示的表架构:
mysql> desc category;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| create_date | datetime | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
我试图插入数据,但我仍然遇到此错误:
mysql> INSERT INTO category (id, name, create_date) VALUES (NULL, Family Events, 2016-25-12 00:00:00), (NULL, Work Events, 2016-25-12 00:00:00);
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 'Events, 2016-25-12 00:00:00), (NULL, Work Events, 2016-25-12 00:00:00)' at line 1
我尝试了其他几种相同的结果,但我找不到任何好的文档。
我试过了:
mysql> INSERT INTO category(id, name, create_date) VALUES(NULL, 'Family Events', 'NOW()');
并收到此错误:
ERROR 1292 (22007): Incorrect datetime value: 'NOW()' for column 'create_date' at row 1
答案 0 :(得分:1)
作为OP agreed with my answer,我正在添加它。您遵循的程序存在的问题是,您应该使用'
:
-- If the column is a `DATETIME`, then use:
VALUES (NULL, 'Family Events', NOW()),
//------------^-------------^
-- If the column is a `TIMESTAMP`, then use:
VALUES (NULL, 'Family Events', CURRENT_TIMESTAMP),
//------------^-------------^
此外,我正在使用CURRENT_TIMESTAMP
替换NOW()
时间戳。
其他人指出的另一件事是,您不需要在id
中添加额外的INSERT
,因为它会根据PRIMARY KEY
自动执行此操作它需要NULL
个值,你可以完全摆脱它。
您的最终SQL应如下所示:
INSERT INTO category(name, create_date) VALUES('Family Events', CURRENT_TIMESTAMP);