#1062 - Duplicate entry '0' for key 'PRIMARY'

℡╲_俬逩灬. 提交于 2019-12-06 13:22:10

Declare the value to be auto incrementing and don't insert it. So:

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)
);

And then:

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

You need to specify the primary key as AUTO_INCREMENT and no need to insert value for 'id' in the query

You want to insert an empty value (0) twice to field, that you said is PRIMARY KEY. Primary key from definition has no duplicate.

Every Primary Key needs to be unique. You inserted 2 rows with the Primary key '0'. Instead of ' ' you should insert an ID.

Edit: Sry my bad, ID not Index.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!