英文:
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预订表,但在代码中
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 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论