【问题标题】:INSERT statement conflicted with the FOREIGN KEY constraint errorINSERT 语句与 FOREIGN KEY 约束错误冲突
【发布时间】:2018-03-19 16:34:03
【问题描述】:

尝试在表中插入值时出现以下错误:

INSERT 语句与 FOREIGN KEY 约束“FK_BookingCustomer_Booking”冲突。冲突发生在数据库“C:\USERS\B00711882\SOURCE\WORKSPACES\SAILAWAY\SAILAWAY FINAL\SAILAWAYV2\SAILAWAYV2\APP_DATA\SAILAWAY.MDF”,表“dbo.Booking”,列“ID”中。声明已终止。

我的插入语句:

CREATE PROCEDURE [dbo].[inserBookingCustomer]
    @customerID INT,
    @bookingID INT
AS
    INSERT INTO BookingCustomer (CustomerID, BookingID)
    VALUES (@customerID, @bookingID)

    RETURN 0

表定义:

Booking:

CREATE TABLE [dbo].[Booking] 
(
    [ID]        INT   IDENTITY (1, 1) NOT NULL,
    [CharterID] INT   NOT NULL,
    [TotalCost] MONEY NOT NULL,
    [StartDate] DATE  NOT NULL,
    [EndDate]   DATE  NOT NULL,
    [TotalDays] INT   NOT NULL,

    CONSTRAINT [PK_Booking] 
        PRIMARY KEY CLUSTERED ([ID] ASC),
    CONSTRAINT [FK_Booking_CharterID] 
        FOREIGN KEY ([CharterID]) REFERENCES [dbo].[Charter] ([ID])
);

BookingCustomer(应该插入信息的地方):

CREATE TABLE [dbo].[BookingCustomer] 
(
    [ID]         INT IDENTITY (1, 1) NOT NULL,
    [BookingID]  INT NOT NULL,
    [CustomerID] INT NOT NULL,

    CONSTRAINT [PK_BookingCustomer] 
        PRIMARY KEY CLUSTERED ([ID] ASC),
    CONSTRAINT [FK_BookingCustomer_Booking] 
        FOREIGN KEY ([BookingID]) REFERENCES [dbo].[Booking] ([ID]),
    CONSTRAINT [FK_BookingCustomer_Customer] 
        FOREIGN KEY ([CustomerID]) REFERENCES [dbo].[Customer] ([CustomerID])
);

这是一个 ASP.Net 应用程序。

编辑:我的 c# 代码:https://pastebin.com/5a8seRh4

【问题讨论】:

  • 您的 bookid 和 customerid 值已经存在于数据库中?
  • @ChetanRanpariya 是的,它们已经存在了。
  • 错误是说 Booking 表中没有与您插入 BookingCustomer 的 BookingID 匹配的 ID。
  • 尝试使用相同的 customerid 和 bookingid 值直接在数据库上执行存储过程?它在那里插入价值吗?
  • 检查您没有插入两次值。

标签: c# sql asp.net


【解决方案1】:

那是因为您试图在 Booking 表中插入一个之前没有插入的值。

您需要先插入图书,然后再将图书的参考文献与客户一起插入

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多