为什么我的(MySQL)INSERT操作失败?

时间:2012-07-18 12:14:58

标签: mysql insert syntax-error

我有一个具有以下结构的表:

--------------
SHOW COLUMNS FROM versions
--------------

+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| project_id      | int(11)      | NO   | MUL | 0       |                |
| name            | varchar(255) | NO   |     |         |                |
| description     | varchar(255) | YES  |     |         |                |
| effective_date  | date         | YES  |     | NULL    |                |
| created_on      | datetime     | YES  |     | NULL    |                |
| updated_on      | datetime     | YES  |     | NULL    |                |
| wiki_page_title | varchar(255) | YES  |     | NULL    |                |
| status          | varchar(255) | YES  |     | open    |                |
| sharing         | varchar(255) | NO   | MUL | none    |                |
+-----------------+--------------+------+-----+---------+----------------+

为什么以下查询失败?

INSERT INTO versions (10, 'Unplanned', ' ', null, null, null, null, 'open', 'none');

这是我收到的错误消息:

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 '10, 'Unplanned', ' ', null, null, null, null, 'open', 'none')' at line 1

3 个答案:

答案 0 :(得分:4)

如果您未指定列列表,则需要添加关键字VALUES

INSERT INTO versions VALUES (10, 'Unplanned', ' ', null, null, null, null, 'open', 'none');

答案 1 :(得分:3)

无需插入默认值,如none,null等。

只需使用以下查询插入数据: 插入版本(project_id,name,sharing)值(10,'unplanned','none');

我正在插入的共享字段,因为根据您的表格设计,它不能为空。

答案 2 :(得分:1)

您也可以使用此格式

  

INSERT INTO    versions(id,project_id,name,description,effective_date,created_on,updated_on,wiki_page_title,status,sharing) VALUES (10, 'Unplanned', ' ', null, null, null, null, 'open', 'none');