Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql #1062-重复输入';0';对于键';初级';_Mysql_Sql_Mysql Error 1064 - Fatal编程技术网

Mysql #1062-重复输入';0';对于键';初级';

Mysql #1062-重复输入';0';对于键';初级';,mysql,sql,mysql-error-1064,Mysql,Sql,Mysql Error 1064,尝试在数据库中插入值时,mysql表出现问题 我遵循了这个教程 并创建了与教程中的表格类似的表格 表代码 错误 SQL查询:编辑 INSERT INTO `mydb`.`categories` ( `id` , `name` , `parentid` ) VALUES ( '', 'groceries', NULL ), ( '', 'snacks', NULL ) MySQL said: Documentation #1062 - Duplicate entry '0' for key '

尝试在数据库中插入值时,mysql表出现问题

我遵循了这个教程

并创建了与教程中的表格类似的表格

表代码

错误 SQL查询:编辑

INSERT INTO `mydb`.`categories` (
`id` ,
`name` ,
`parentid`
)
VALUES (
'', 'groceries', NULL
), (
'', 'snacks', NULL
)

MySQL said: Documentation
#1062 - Duplicate entry '0' for key 'PRIMARY'

请帮助我解决此问题。

将值声明为自动递增,不要插入它。因此:

create table categories (
    id       integer     not null  auto_increment primary key,
    name     varchar(37) not null,
    parentid integer     null,
    foreign key parentid_fk (parentid) references categories (id)
);
然后:

INSERT INTO `mydb`.`categories` (`name`, `parentid`)
    VALUES ('groceries', NULL),
           ('snacks', NULL);

您想在字段中插入两次空值(0),您说它是主键。定义中的主键没有重复项。

您需要将主键指定为自动增量,并且不需要在查询中插入“id”值

每个主键都需要是唯一的。 您插入了主键为“0”的两行。 您应该插入一个ID而不是“”


编辑:Sry my bad,ID not Index。

这本身并不能解决问题[Edit answer fixes this issue]我已经评论了其他人的答案,所以我觉得不评论你的答案有点糟糕:这个答案一针见血
:-)
插入索引?你能解释一下你在说什么吗?
INSERT INTO `mydb`.`categories` (`name`, `parentid`)
    VALUES ('groceries', NULL),
           ('snacks', NULL);