Error insert statement conflicted with the foreign key constraint - sql-server

Issue
I have to table "Account_Manager" and "Partner" and both table contains foreign Key of each other and now I want to add data and it show the error Foreign Key conflict issue.
Guide me how I can insert data into these two table.
Table 1
Table 2

Make both columns (designated as FKs) nullable. Thats only way you can make circular reference/FK

Related

Migrating a self referencing table using Azure Data Factory

I am trying to migrate a table which has 2 colums.
Id - Primarky key
Parent ID - Foreign key populated by the value above (Id).
(So basically the FK is in the same table)
When I migrate this, I get the following error.
"The UPDATE statement conflicted with the FOREIGN KEY SAME TABLE constraint"
Please let me know how to deal with this.
Thanks in advance
Disable the foreign key constraint first. Then migrate your data. And afterwards you can enable the constraint again.
ALTER TABLE Purchasing.PurchaseOrderHeader
NOCHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;
ALTER TABLE Purchasing.PurchaseOrderHeader
CHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;
Disable foreign key constraints with INSERT and UPDATE statements

Table with multiple relations to a single primary key

Is it possible to create multiple relations from one table, to another table?
I have a table containing purchases, each of these purchases have a origin_country and a destination_country.
I would like to have relations (as foreign keys) to a single PK on a table from these two columns from the same table.
i have tried the following queries:
alter table Purchases
add constraint FK_Purchases_OriginCountries
foreign key (FK_OriginCountryCode) references dbo.countries
go
alter table Purchases
add constraint FK_Purchases_DestinationCountries
foreign key (FK_DestinationCountryCode) references dbo.countries
go
But end up getting a conflict, I can't however find documentation that this is not possible...
[23000][547] The ALTER TABLE statement conflicted with the FOREIGN KEY
constraint "FK_Purchases_DestinationCountries". The conflict occurred
in database "Market", table "dbo.countries", column 'ID'.
Is this relationship intentionally not possible, or did i just make a mistake?
Thank you
Yes you can.
The error is not a result of trying to create two foreign keys back to a single table, that's perfectly fine. Try running this to see it work:
create table t(i int primary key);
create table u
(
j int foreign key references t(i),
k int foreign key references t(i)
);
The problem you have is that you have some data in your Purchases table where the value in the column on which you are trying to create the foreign key does not exist in the countries table's ID column.
To find them run a query like this:
select p.*
from dbo.purchases p
where not exists
(
select *
from dbo.countries
where ID = p.FK_DestinationCountryCode
)
Note that I think your column names are a little weird here, You shouldn't call a column FK_DestinationCountryCode just because it has a foreign key on it, and a "code" is not the same kind of thing as an "ID". Your purchases table's columns should probably be called DestinationCountryID and OriginCountryID.

how to add foreign key to Azure SQL DB Table

I am stumped by an Add Constraint Foreign Key error I am encountering.
I just created TableA with a Primary Key on PLAN_ID. The table contains zero records. I am attemtpting to execute:
alter table TableB
add constraint FK_TASK_PLAN Foreign Key (TASK_PLAN_ID)
references TableA (PLAN_ID)
Which keeps returning
"Msg 547, Level 16, State 0, Line 5. The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_TASK_PLAN". The conflict occurred in database "DT_Worklist", table "dbo.TableA", column 'PLAN_ID'.
This essentially seems to be telling me I cannot create an FK on the specified column.
What am I doing wrong which is preventing me from creating this Foreign Key? Is it OK my Parent Table is empty or does it need at least one record? What about the FK column; does it need to be Not Null or does it need to have at least 1 record?
Actually, I think I figured this out... My PARENT Table which I just created is empty while my CHILD Table which was created * populated about a week ago already has values in the FK column. So there is no way to create the PK/FK relationship when the FK already contains values.....

Delete foreign key with primary key reference

I tried many forums but was not satisfied - I have a table that has a primary key and foreign key relation.
I have to delete table rows with primary key so I need to remove the constraints before deleting.
I used:
delete from [docd_metadata].[docd_metadata].[STATEMENT_IMAGES]
where [statement_image_id]= 05291520275
I got error:
The DELETE statement conflicted with the REFERENCE constraint "fk_stmnt_image_StmntImageId". The conflict occurred in database "docd_metadata", table "docd_metadata.STATEMENT_CAMPAIGN", column 'STATEMENT_IMAGE_ID'.
so I tried :
ALTER TABLE[docd_metadata].[docd_metadata].[STATEMENT_IMAGES]
DROP CONSTRAINT [fk_stmnt_image_StmntImageId]
Now I am getting :
Constraint 'fk_stmnt_image_StmntImageId' does not belong to table 'STATEMENT_IMAGES'
Schema:
Also:
Any suggestion please?
If you really closely read the error message, it's clear that the FK constraint is on table docd_metadata.STATEMENT_CAMPAIGN and not on STATEMENT_IMAGES - so therefore, you must use this SQL to drop the FK constraint:
ALTER TABLE [docd_metadata].[STATEMENT_CAMPAIGN]
DROP CONSTRAINT [fk_stmnt_image_StmntImageId]
The FK goes from table [docd_metadata].[STATEMENT_CAMPAIGN] (column STATEMENT_IMAGE_ID) to [docd_metadata].[STATEMENT_IMAGES] - one table has the primary key, which another table references via its foreign key.

primaykey and foreign key exception?

i get this error when i inserting the data in a table this is my error
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_playlistvideos_videos". The conflict occurred in database "tpvnew", table "dbo.videos", column 'videoid'.
You didn't provide a valid key for your foreign key column.
Just check if the videoid in the new record you insert into playlistvideos actually exists in videos table.
The INSERT into the target table referenced a key that doesn't exist in the foreign table.

Resources