消息2627违反PRIMARY KEY约束

时间:2014-06-22 12:33:07

标签: mysql sql database duplicates primary-key

尝试为电视节目中的团队制作数据库。 但当我尝试将数据插入tblShowteam时 出现以下错误。

Msg 2627, Level 14, State 1, Line 2
Violation of PRIMARY KEY constraint 'PK__tblShowt__F693078C03317E3D'. Cannot insert duplicate key in object 'dbo.tblShowteam'.

-- tabbellen aanmaken
create table tblShow(
setId       int,
Datum       date,
teams       int
primary key (setId));

create table tblShowteam(
SetId           int,
datum           date,
teams           int,
primary key (teams));


create table tblTeam(
TeamId      int,
Coach       varchar(35),
CoachId     int,
teams       int
primary key (CoachId));


-- participant table
create table tblDeelnemer(
DeelnemerId     int identity(1, 1),
DeelnemerV      varchar(35),
deelnemerT_V    varchar(10),
DeelnemerA      varchar(35),
CoachId         int,
datum_optreden  date
primary key (DeelnemerId));


--table for the public viewers
create table tblKijker(
Kijkerv         varchar(35),
KijkerT_V       varchar(10),
KijkerA         varchar(35),
Stoelnummer     int identity(1,3),
ShowId          int Not null,
Email           varchar(35)
primary key (Email));

我的插入将如下所示:

insert into tblShowteam values (1, '2014-06-28', 1)
insert into tblShowteam values (2, '2014-06-05', 1)
insert into tblShowteam values (3, '2014-06-12', 1)
insert into tblShowteam values (4, '2014-06-19', 1)
insert into tblShowteam values (5, '2014-06-26', 1)

所有其他插入(在不同的表中)都正常工作。

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

你的问题在这里

   primary key (teams));

我想你必须这样做

  primary key (setId));
像那样:

 create table tblShowteam(
 SetId           int,
 datum           date,
 teams           int,
 primary key (setId));

因为您在使用1团队时插入了相同的团队primary key,这意味着没有重复项。

答案 1 :(得分:0)

你的插页:

insert into tblShowteam values (1, '2014-06-28', 1)
insert into tblShowteam values (2, '2014-06-05', 1)
...

DB将其翻译成:

insert into tblShowteam (SetId, datum, teams) values (1, '2014-06-28', 1);

因为第三列是您的主键,您收到此错误。