Question : How do I temporarily ignore foreign key constraints during a transaction?

I have an Access front end that has been conneted to Access tables but we are converting our large sites to SQL. we run a number of transactions that insert records in multiple tables, some of which have foreign key constraints against each other. For example Insert a record in Table A with field NAME, then insert a reocrd in table B with field NAME and there is a foreign key constraint requiring NAME be in table A before inserting the reocrd in table B.
Access handles this fine inside a transaction, but SQL fails because the new record in table A is not inserted yet so it fails the insert to table B.
I hate to write my own transactions if it is avoidable. Is there a way to turn off the constraint checking (or any other simple solution) until the transaction is written and then turn it back on and verify the records so the are not marked as not trusted?

Answer : How do I temporarily ignore foreign key constraints during a transaction?

The problem is that you have too many transactions.  Let's follow your example where B is dependent on A.  When A is inserted it can be "seen" within the SAME transaction and does not need to be committed before B can be inserted within that transaction.  But OTHER transactions cannot see A until it is committed.

The solution is to do all the related updates in the proper order and within the same transaction.  You only commit after everything is done and if any of it rolls back, it all rolls back.  

Your hesitance to turn off RI is right on the mark. It is expensive to turn back on and leaves your data vulnerable while it is off.




   
Random Solutions  
 
programming4us programming4us