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


  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

最佳答案

这是因为您试图在预订表中插入一个之前未插入的值。

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

关于c# - INSERT语句与FOREIGN KEY约束错误冲突,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49367960/

10-11 03:21