The insert statement conflicted with the foreign key constraint lỗi năm 2024

lúc mình tạo form code vẫn chạy bình thường nhưng khi thêm vào csdl mới thì nó hiện thị lỗi : "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Áp Dụng Giá_Ngày". The conflict occurred in database "CsdlHangkhong", table "dbo.Ngày", column 'Ngày'.The statement has been terminated." không biết là lỗi gì có ai biết lỗi này chỉ mình cách khắc phục với!

Chào anh Long, em đang làm CSDL phân quyền cho người dùng bằng linq. Em tạo 2 bảng: Role và UserRole. Lúc em thêm dữ liệu vào thì nó báo lỗi:

CREATE DATABASE LOGIN_ROLE  
GO  
USE LOGIN_ROLE  
GO  
CREATE TABLE ROLE  
(  
  USERROLE VARCHAR(50) PRIMARY KEY  
)  
GO  
CREATE TABLE USERROLE  
(  
  USERID VARCHAR(50)  
  FOREIGN KEY(USERID) REFERENCES dbo.ROLE(USERROLE)  
)  
GO  
INSERT dbo.USERROLE  
(  
    USERID  
)  
VALUES  
(  
'1'-- USERID - varchar(50)  
    )  
  INSERT dbo.USERROLE  
(  
    USERID  
)  
VALUES  
(  
'2'-- USERID - varchar(50)  
    )  
GO  
INSERT dbo.ROLE  
(  
    USERROLE  
)  
VALUES  
('Admin' -- USERROLE - varchar(50)  
    )  
  INSERT dbo.ROLE  
(  
    USERROLE  
)  
VALUES  
('User' -- USERROLE - varchar(50)  
    )  
GO
Msg 547, Level 16, State 0, Line 16  
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__USERROLE__USERID__25869641". The conflict occurred in database "LOGIN_ROLE", table "dbo.ROLE", column 'USERROLE'.  
The statement has been terminated.  
Msg 547, Level 16, State 0, Line 24  
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__USERROLE__USERID__25869641". The conflict occurred in database "LOGIN_ROLE", table "dbo.ROLE", column 'USERROLE'.  
The statement has been terminated.

Lỗi này là sao anh và cách khắc phục?

To add to what Richard has said, a foreign key is a way to tell SQL that two tables are related: a value in a Confectionary table row is related to a single value in the Category table, in much the same way as an Invoice Line describing a sold product is related by a Foreign key to the Invoices table by the Invoice Number: an invoice will describe several sold items, but there is only ever one Invoice related to that entire group of Invoice Lines.

A foreign key can only be set to a value that exists in the other table, or your data become inconsistent: you have to create the Invoice before you add lines to it!

Similarly, your Category needs to be setup before you start adding items to the Confectionary table that refer to them.

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_PHANCONG_NHANVIEN". The conflict occurred in database "quanlinhansu", table "dbo.NHANVIEN", column 'manv'.

Bình luận

The insert statement conflicted with the foreign key constraint lỗi năm 2024

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập

The insert statement conflicted with the foreign key constraint lỗi năm 2024

emenpyđã bình luận 05:27 07-04-2017

Mình mới bắt đầu học WPF và đã xem qua tuts WPF của Kteam mà ko thấy có hướng dẫn cách tạo custom control. Mình muốn tạo 1 cái custom control trong đó có Image thay đổi theo status change. Vd: khi giá trị là true thì hiện icon bubble màu xanh còn nếu là false thì hiện icon màu đỏ. Làm sao để thay đổi image source với dependencyproperty? Bạn có thể làm clip hướng dẫn cụ thể cách làm custom control đc ko? Cám ơn bạn nhiều lắm!!

Ps: tại sao mình tạo câu hỏi ko đc nhỉ? cứ bị error

The insert statement conflicted with the foreign key constraint lỗi năm 2024

K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 17:32 06-04-2017

Cái này có nghĩa là khóa ngoại của bạn nó trùng hoặc không tồn tại giá trị tương ứng của khóa chính trong bảng kia

The INSERT statement conflicted with the FOREIGN KEY constraint "fk_Orders_Customers". The conflict occurred in database "tempdb", table "dbo.Customers", column 'CustomerID'.

That is, you are try to insert a row into the table f_sales with an id that is not available in the table d_DocType.

Since I don't see your query, but only images of which I don't really know the relevance, I will have to guess. But I see that in f_sales there is a value 1110 in the DocTypeId column. In the first table (I guess this is d_DocType?), there is no id 1110. There is a value 1110 in the DocumentId column, but we can tell from the error message the foreign key is set up against the column id.

A tip for the next time when you have a problem: include any query you are running, so that we have to read your mind.