Barguast
Barguast

Reputation: 6186

Mutually referencing two tables

Out of curiousity;

If I've got two tables, A and B;

A
ID int (PK)
B_ID int (FK)

B
ID int (PK)
A_ID int (FK)

A has a reference to B. B has a reference to A. A_ID and B_ID are non-nullable.

How can I add rows to these tables? I can't add a row to A without adding a row to B, nor vice versa, however if the tables contained this data;

A
ID   B_ID
1    100

B
ID   A_ID
100  1 

Then obviously that is fine. If I wanted to insert this data, would I have to first disable data integrity or can I somehow add these two rows at once?

Thanks, and apologies for the title - I've no idea what this is called, or I'd look it up myself.

Upvotes: 1

Views: 217

Answers (1)

Rikki
Rikki

Reputation: 3528

There is a well explained article on msdn:

http://blogs.msdn.com/b/sqlazure/archive/2010/07/01/10033575.aspx

and another one on wikipedia:

http://en.wikipedia.org/wiki/Circular_reference

Take a look at both and read them carefully.

Hope it helps you.

Cheers

Upvotes: 2

Related Questions