Migrating a self referencing table using Azure Data Factory - sql-server

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

Related

Error insert statement conflicted with the foreign key constraint

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

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.

Update even though using constraints

I'm trying to update a value (small spelling error) in the SQL Server 2014 database.
I do like this:
update t_Table set funID = 'References' where funID = 'Referencies'
When doing this I get the error
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_t_Table_Language_t_Table". The conflict occurred in database "db", table "dbo.t_Table".
If updating the foreign key, I get a similar error.
Is there any way to update all values at the same time?
I have a vague memory of someone showing a way to do this in management studio but I do not recall how.
Enable update cascade on the foreign key
ALTER TABLE t_Table_Language DROP CONSTRAINT FK_t_Table_Language_t_Table
ALTER TABLE t_Table_Language ADD CONSTRAINT FK_t_Table_Language_t_Table
FOREIGN KEY (funID) REFERENCES t_Table(funID)
ON UPDATE CASCADE
EDIT:
Or the other way around, i'm not sure which table has the foreing key
ALTER TABLE t_Table DROP CONSTRAINT FK_t_Table_Language_t_Table
ALTER TABLE t_Table ADD CONSTRAINT FK_t_Table_Language_t_Table
FOREIGN KEY (funID) REFERENCES t_Table_Language(funID)
ON UPDATE CASCADE
Then do the update on the referenced table (the master table).
UPDATE t_Table_Language
SET funID = 'References'
WHERE funID = 'Referencies'

Foreign key linked with primary key in same table

I have a table Categories with columns Id, ParentId (for "subcategories" whom can have any level of nesting) and some other. Using SQL Server 2012 I can't make foreign key in same table, FK_Categories_Categories (Id -> ParentId).
Error message is
'Categories' table
- Unable to create relationship 'FK_Categories_Categories'. The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_Categories_Categories". The conflict occurred in database "pokupaykadb", table "dbo.Categories", column 'Id'.
That needs for cascade deletion of subcategories. What solution can be? It's desirable to be a some property, like cascade deletion from another table by foreign key
http://i.stack.imgur.com/kXiMS.png
If there are orphaned records that does not meet your constraint criteria - delete them before creating the foreign key.
Usually there are few records which doesn't go by the new constraint and that the DBMS doesn't allow to create the constraint.
In the case of orphaned values, the first occurrence is provided in the error label with the value that is orphaned.
It would certainly have helped to see what code you have tried to execute.
Below is a valid table definition :
CREATE TABLE dbo.Categories
(
Id int NOT NULL IDENTITY(-2147483648, 1)
CONSTRAINT PK_Categories PRIMARY KEY
, ParentId int NOT NULL
CONSTRAINT FK_Categories_ParentId
FOREIGN KEY (ParentId) REFERENCES dbo.Categories
)

How to recheck primary/foreign key constraint for data already in the table in sql server?

I have a table in SQL Server 2005 with a foreign key and it was disable for huge data loading, and then re-enabled:
Example:
alter table table1 nocheck constraint fk_1
go
lots of inserts...
go
alter table table1 check constraint fk_1
go
Now, the question: is there a way to re-check this just inserted data?
The syntax looks a little silly with the word "check" repeated, but what you want is:
alter table table1 with check check constraint fk_1
go
Adding the "with check" option will validate existing data against the constraint. Doing this will also prevent the constraint from becoming untrusted.
If any existing data violates the constraint, you'll get an error that will look like this:
The ALTER TABLE statement conflicted with the CHECK constraint "fk_1".

Resources