【问题标题】:SQL Server Management: The INSERT statement conflicted with the FOREIGN KEY constraintSQL Server 管理:INSERT 语句与 FOREIGN KEY 约束冲突
【发布时间】:2015-07-25 13:43:45
【问题描述】:

我正在使用 SQL Server Management 创建数据库(在大学工作),我正在尝试将新值插入到表中但收到错误。

我创建的两个相关表是:

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(以及表格“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);

但我收到一个错误:

消息 547,第 16 级,状态 0,第 1 行
INSERT 语句与 FOREIGN KEY 约束“FK__votes__bno__1920BF5C”冲突。冲突发生在数据库“投票”、表“dbo.ballotBox”、列“bno”中。声明已终止。

【问题讨论】:

  • 您正在尝试为bno 插入一个值为0 的行,但这在投票箱表中不存在。错误行是 (1, 0, 0.0, 361), 投票结束此问题,因为这是数据错误而不是编程问题。
  • 还有另一个问题:您尝试在votes.pid 中插入的值溢出numeric(3,0) 列。它可能应该是一个整数。

标签: sql sql-server foreign-keys sql-insert


【解决方案1】:

你正在尝试插入

 (1, 0, 0.0, 361),

votes 表中有FK bno ballotbox表中没有对应0的记录 请从投票声明中删除此行,然后插入

【讨论】:

    【解决方案2】:

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

    这样做:

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

    【讨论】:

    • 不,语法是正确的。在引用子句中可以省略列名。另外,你拼错了foreign
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多