SQL更新外键冲突

huangapple go评论75阅读模式
英文:

SQL update foreign key conflicted

问题

以下是您要翻译的内容:

"It's giving me an UPDATE error while trying to update the foreign's key value"

SQL查询

data = "UPDATE Reservations SET id_room = @idRoom FROM Reservations WHERE id_reservation = @idReservation";

using (SqlCommand cmd = new SqlCommand(data, con))
{
    cmd.Parameters.AddWithValue("@idRoom", idRoom);
    cmd.Parameters.AddWithValue("@idReservation", idReservationTxtBox.Text);
    cmd.ExecuteNonQuery();
}

SQL错误

SQL预订表

SQL房间表

预订表数据

房间表数据

idRoom变量定义

SQL预订表,但在代码中

CREATE TABLE [dbo].[Reservations] (
    [id_reservation]  INT          IDENTITY (1, 1) NOT NULL,
    [n_card]          VARCHAR (16) NOT NULL,
    [cardholder_name] TEXT         NOT NULL,
    [expiration_date] DATE         NOT NULL,
    [cvc]             VARCHAR (3)  NOT NULL,
    [id_room]         INT          NOT NULL,
    [reserva_price]   FLOAT (53)   NOT NULL,
    [check-in]        DATETIME     NOT NULL,
    [check-out]       DATETIME     NOT NULL,
    CONSTRAINT [PK_Reservas] PRIMARY KEY CLUSTERED ([id_reservation] ASC),
    CONSTRAINT [FK_Reservations_Rooms] FOREIGN KEY ([id_room]) REFERENCES [dbo].[Rooms] ([id_room])
);

SQL房间表,但在代码中

CREATE TABLE [dbo].[Rooms] (
    [id_room]   INT  IDENTITY (1, 1) NOT NULL,
    [n_room]    INT  NOT NULL,
    [type_room] TEXT NOT NULL,
    [available] BIT  NOT NULL,
    CONSTRAINT [PK_Rooms] PRIMARY KEY CLUSTERED ([id_room] ASC)
);

我尝试在Google上查找错误,但没有显示任何结果。

我期望通过将id_room更改为代码中的值(idRoom),从而更新表格Reservations。

英文:

It's giving me an UPDATE error while trying to update the foreign's key value

SQL Query

data = "UPDATE Reservations SET id_room = @idRoom FROM Reservations WHERE id_reservation = @idReservation";

            using (SqlCommand cmd = new SqlCommand(data, con))
            {
                cmd.Parameters.AddWithValue("@idRoom", idRoom);
                cmd.Parameters.AddWithValue("@idReservation", idReservationTxtBox.Text);
                cmd.ExecuteNonQuery();
            }

SQL Error

SQL Reservations table

SQL Rooms table

Reservations table data

Rooms table data

idRoom variable definition

SQL Reservations table BUT IN CODE

CREATE TABLE [dbo].[Reservations] (
    [id_reservation]  INT          IDENTITY (1, 1) NOT NULL,
    [n_card]          VARCHAR (16) NOT NULL,
    [cardholder_name] TEXT         NOT NULL,
    [expiration_date] DATE         NOT NULL,
    [cvc]             VARCHAR (3)  NOT NULL,
    [id_room]         INT          NOT NULL,
    [reserva_price]   FLOAT (53)   NOT NULL,
    [check-in]        DATETIME     NOT NULL,
    [check-out]       DATETIME     NOT NULL,
    CONSTRAINT [PK_Reservas] PRIMARY KEY CLUSTERED ([id_reservation] ASC),
    CONSTRAINT [FK_Reservations_Rooms] FOREIGN KEY ([id_room]) REFERENCES [dbo].[Rooms] ([id_room])
);

SQL Rooms table BUT IN CODE

    CREATE TABLE [dbo].[Rooms] (
    [id_room]   INT  IDENTITY (1, 1) NOT NULL,
    [n_room]    INT  NOT NULL,
    [type_room] TEXT NOT NULL,
    [available] BIT  NOT NULL,
    CONSTRAINT [PK_Rooms] PRIMARY KEY CLUSTERED ([id_room] ASC)
);

I tried to find the error on google but it didn't show any results

I was expecting to update the table Reservations by changing the id_room to the value on the code which is (idRoom)

答案1

得分: 0

您已经设置了一个约束,防止将在reservations_rooms表中不存在的ID插入Reservations表的id_room列中。

您要插入到Reservations中的@idRoom的值必须作为主键存在于reservations_rooms表中。

根据您在原始问题中添加的截图,您只能在id_room列中“插入”值1、2或3,因为这是reservations_rooms表中存在的唯一行。

英文:

You have a constraint set up that prohibits inserting an ID into the Reservations table's id_room column that does not exist in reservations_rooms.

The value of @idRoom that you are inserting into Reservations needs to exist as a record (PK) in the reservations_rooms table.

Based on your added screenshots in the Original Question, you can only "insert" values of (1, 2 or 3) in the id_room column, as that is the only rows that exist in the reservations_rooms table.

huangapple
  • 本文由 发表于 2023年3月15日 18:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定