P
P
protsey2018-07-25 15:34:34
Database
protsey, 2018-07-25 15:34:34

What is the sequence of actions for filling tables with such a relationship with data?

Good afternoon. There is a database of about 100 tables, which is now manually filled with data from several databases of the same structure. In this structure, I came across a place that I just can’t figure out how to fill in, in what sequence and what manipulations to perform. In the picture, I visualize the relationship between two tables that refer to each other with a one-to-many relationship in different columns.
5b586cce44a7d053879115.png
Whatever table I start filling with, swears at "The INSERT statement conflicted with the FOREIGN KEY constraint ...". Each table requires data to be populated that can be referenced in another table. How to be in this situation? Please do not judge strictly, the theory is very lacking, you have to learn practically in production.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Y, 2018-07-25
@x_shader

In general, if the FOREIGN KEY field of the first table allows you to insert NULL - insert records with zeros in this field.
Then complete the second table.
Later (when there are already records in the second table to refer to), it will be possible to do an UPDATE of the zero values ​​of the first table.
Well, or you can temporarily disable
CONSTRAINTs: -- disable: -- Enable back:
I don’t even know which of these is worse in production.
UPD: It's better to disable a specific CONSTRAINT:

ALTER TABLE Purchasing.PurchaseOrderHeader  
NOCHECK CONSTRAINT CK_PurchaseOrderHeader_Freight;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question