MySQL:密钥 2 的重复条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2121663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL: Duplicate Entry for Key 2
提问by Nick Heiner
I'm not sure what I'm doing to cause this error. The query:
我不确定我在做什么导致这个错误。查询:
INSERT INTO node (type, language, title) VALUES ('bout', 'en', 'the title 3')
The error:
错误:
#1062 - Duplicate entry '0' for key 2
The table:
桌子:
CREATE TABLE `node` (
`nid` int(10) unsigned NOT NULL auto_increment,
`vid` int(10) unsigned NOT NULL default '0',
`type` varchar(32) NOT NULL default '',
`language` varchar(12) NOT NULL default '',
`title` varchar(255) NOT NULL default '',
`uid` int(11) NOT NULL default '0',
`status` int(11) NOT NULL default '1',
`created` int(11) NOT NULL default '0',
`changed` int(11) NOT NULL default '0',
`comment` int(11) NOT NULL default '0',
`promote` int(11) NOT NULL default '0',
`moderate` int(11) NOT NULL default '0',
`sticky` int(11) NOT NULL default '0',
`tnid` int(10) unsigned NOT NULL default '0',
`translate` int(11) NOT NULL default '0',
PRIMARY KEY (`nid`),
UNIQUE KEY `vid` (`vid`),
KEY `node_changed` (`changed`),
KEY `node_created` (`created`),
KEY `node_moderate` (`moderate`),
KEY `node_promote_status` (`promote`,`status`),
KEY `node_status_type` (`status`,`type`,`nid`),
KEY `node_title_type` (`title`,`type`(4)),
KEY `node_type` (`type`(4)),
KEY `uid` (`uid`),
KEY `tnid` (`tnid`),
KEY `translate` (`translate`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2586 ;
What am I doing wrong? By not specifying a nid for the new entry, it will auto-increment, right?
我究竟做错了什么?通过不为新条目指定 nid,它将自动递增,对吗?
回答by Mike
You have vid
as a unique key. However you never set a value for it so the default 0 is always used. Second entry into the table will violate uniqueness.
你有vid
一个唯一的键。但是,您从未为其设置值,因此始终使用默认值 0。第二次进入表将违反唯一性。
回答by Max Shawabkeh
The problem looks to be with vid
not being specified. The first entry you put will have had a vid
of 0 (the default) and the next will try 0 again and fail on the UNIQUE
index. Not that only the primary key, nid
, will auto-increment.
问题似乎是vid
没有被指定。您放置的第一个条目vid
的值为 0(默认值),下一个将再次尝试 0 并在UNIQUE
索引上失败。不仅是主键nid
, 会自动递增。
回答by Dave Sims
I think nid isn't the problem, vid is. It defaults to 0 and is designated unique.
我认为 nid 不是问题,vid 是。它默认为 0 并被指定为唯一的。