SQL 服务器管理:INSERT 语句与 FOREIGN KEY 约束冲突

SQL Server Management: The INSERT statement conflicted with the FOREIGN KEY constraint

我正在使用 SQL 服务器管理创建数据库(在大学工作),我试图将新值插入 table 但收到错误。

我创建的两个相关 table 是:

create table ballotBox
(
    bno integer,
    cid numeric(4,0) references city,
    street varchar(20),
    hno integer,
    totvoters integer,
    primary key (bno)
);

create table votes
(
    cid numeric(4,0) references city,
    bno integer references ballotBox,
    pid numeric(3,0) references party,
    nofvotes integer
);

我首先输入了 ballotBox 的值(以及 table 'city' 和 'party'):

insert into ballotBox values 
    (1, 1, 'street1', 10, 1500),
    (2, 1, 'street2', 15, 490),
    (3, 1, 'street2', 15, 610),
    (4, 1, 'street2', 15, 650),
    (5, 2, 'street3', 10, 900),
    (6, 2, 'street3', 55, 800),
    (7, 2, 'street4', 67, 250),
    (8, 2, 'street4', 67, 990),
    (9, 2, 'street5', 5, 600),
    (10, 3, 'street1', 72, 1000),
    (11, 3, 'street6', 25, 610),
    (12, 3, 'street6', 25, 600),
    (13, 4, 'street2', 3, 550),
    (14, 4, 'street7', 15, 500),
    (15, 5, 'street8', 44, 1100),
    (16, 5, 'street9', 7, 710),
    (17, 5, 'street10', 13, 950);

然后我尝试输入值来投票:

insert into votes values
    (1, 1, 200, 100),
    (1, 11, 210, 220),
    (1, 1, 220, 2),
    (1, 1, 230, 400),
    (1, 1, 240, 313),
    (1, 1, 250, 99),
    (2, 1, 200, 55),
    (2, 10, 210, 150),
    (2, 10, 220, 2),
    (2, 1, 230, 16),
    (2, 1, 240, 210),
    (2, 9, 250, 54),
    (3, 9, 200, 234),
    (3, 9, 210, 123),
    (3, 1, 220, 8),
    (3, 1, 230, 87),
    (3, 1, 240, 76),
    (3, 1, 250, 6),
    (4, 1, 200, 135),
    (4, 1, 210, 246),
    (4, 17, 220, 7),
    (4, 1, 230, 18),
    (4, 1, 240, 44),
    (4, 1, 250, 66),
    (1, 2, 200, 373),
    (1, 2, 210, 12),
    (1, 2, 220, 3),
    (1, 2, 230, 74),
    (1, 2, 240, 58),
    (1, 2, 250, 272),
    (2, 6, 200, 139),
    (2, 6, 210, 2580),
    (2, 2, 220, 6),
    (2, 2, 230, 73),
    (2, 2, 240, 7),
    (2, 2, 250, 99),
    (3, 2, 200, 15),
    (3, 2, 210, 68),
    (3, 2, 220, 12),
    (3, 2, 230, 12),
    (3, 2, 240, 15),
    (3, 2, 250, 25),
    (4, 2, 200, 7),
    (4, 2, 210, 245),
    (4, 2, 220, 8),
    (1, 0, 0.0, 361),
    (4, 2, 240, 67),
    (4, 2, 250, 144),
    (5, 2, 200, 123),
    (5, 2, 210, 76),
    (5, 2, 220, 15),
    (5, 2, 230, 158),
    (5, 2, 240, 76),
    (5, 2, 250, 132),
    (1, 3, 200, 152),
    (1, 3, 210, 517),
    (1, 3, 220, 0),
    (1, 3, 230, 267),
    (2, 3, 200, 87),
    (2, 3, 210, 134),
    (2, 3, 220, 4),
    (2, 3, 230, 11),
    (2, 3, 240, 256),
    (2, 3, 250, 76),
    (3, 3, 200, 105),
    (3, 3, 210, 132),
    (3, 3, 3220, 3),
    (3, 3, 230, 24),
    (3, 3, 240, 254),
    (3, 3, 250, 12),
    (1, 4, 200, 61),
    (1, 4, 210, 54),
    (1, 4, 220, 5),
    (1, 4, 230, 19),
    (1, 4, 240, 1),
    (1, 4, 250, 47),
    (2, 4, 200, 17),
    (2, 4, 210, 23),
    (2, 4, 220, 0),
    (2, 4, 230, 64),
    (2, 4, 240, 11),
    (2, 4, 250, 149),
    (1, 5, 0200, 187),
    (1, 5, 210, 88),
    (1, 5, 220, 1),
    (1, 5, 230, 255),
    (1, 5, 240, 12),
    (1, 5, 250, 373),
    (2, 2, 500, 245),
    (2, 5, 210, 120),
    (2, 5, 220, 9),
    (2, 5, 230, 19),
    (2, 5, 240, 234),
    (2, 5, 250, 5),
    (3, 5, 200, 107),
    (3, 5, 210, 18),
    (3, 5, 220, 11),
    (3, 5, 230, 54),
    (3, 5, 240, 378),
    (3, 5, 250, 243);

但是我收到一个错误:

Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__votes__bno__1920BF5C". The conflict occurred in database "Voting", table "dbo.ballotBox", column 'bno'. The statement has been terminated.

您正在尝试插入

 (1, 0, 0.0, 361),

votestable有FKbno ballotboxtable中没有0对应的记录 请从投票声明中删除这一行,然后插入

两个表之间的关系不正确。

这样做:

create table votes
(
    cid numeric(4,0),
    bno integer,
    pid numeric(3,0),
    nofvotes integer
    foreing key cid reference city(cid)
    foreing key bno reference ballotbox (bno)
    foreing key pid reference party(pid)
);