#1062-键"PRIMARY"的条目"0"重复 [英] #1062 - Duplicate entry '0' for key 'PRIMARY'

查看:56
本文介绍了#1062-键"PRIMARY"的条目"0"重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with mysql table while try to insert values in database.

我遵循了本教程

http://sqllessons.com/categories.html

并创建了表格(类似于教程中的表格)

and created table like the table from tutorial

表代码

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

错误 SQL查询:编辑编辑

error SQL query: Edit Edit

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

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

帮我解决这个问题.

推荐答案

将值声明为自动递增,请勿插入.所以:

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

然后:

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

这篇关于#1062-键"PRIMARY"的条目"0"重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆