SQLTeam.com | Weblogs | Forums

PK/FK Relationship question


#1

Hi Guys,

I have quick two quick questions.

  1. I have a task where I have to read the data from a flat file and compare with Source table and destination table (Update or Insert) I am using Merge statement for this.
    I am having an issue with FK relationship with my table and couple of other tables. My question is In order to keep Integrity I have to Insert the data first all others FK relational table
    and then to my table, am I right? e.g
    My Main table = Table_Main
    FK Relationship table with Table_Main = Table_FK1
    FK Relationship table with Table_Main = Table_FK2
    FK Relationship table with Table_Main = Table_FK3
    FK Relationship table with Table_FK2 = Table_FK2_OtherTable
    FK Relationship table with Table_FK3 = Table_FK3_OtherTable1
    FK Relationship table with Table_FK2_OtherTable = Table_FK2_OtherTable_OtherTable

    In order for me to Successfully insert the data into My Main table I have to first insert the data into (Table_FK1,Table_FK2,Table_FK3,Table_FK2_OtherTable,Table_FK3_OtherTable1)
    am I right?

  2. I am sure there is a way to Enable/Disable Check Constraint. Let say If I Disable before Insert the data and then Enable it after inserting the data, it will give me an error, right?

This process will run twice a week and I want to automate this process.

Please advise.

Thank You.


#2

Instead of disable check constraint and insert. You can add staging tables to load files and check the keys and insert into main table. That will be proper way to maintain data in a relationship.