views:

45

answers:

3

I should know this but I expect it to be a simple answer...

This is my simple database so far for this question:

alt text

Am I supposed to insert the Id from Contact and Phone into Contact_Phone when I insert a record into Contact and Phone tables - or can this be done automatically?

I am using SQL Server 2008 Express.

+2  A: 

You need to insert it. And since the relationships are defined, you will need to insert it into the Contact and Phone tables first.

David Stratton
+3  A: 

You have to insert it yourself in SQL, but it depends entirely on how you're accessing the database.

The Entity Framework should be able to handle this for you automatically.

Bennor McCarthy
Playing with NHibernate for the first time.. not sure if it does automatically or not.
0A0D
This should help: http://codebetter.com/blogs/peter.van.ooijen/archive/2008/05/29/nhibernate-many-to-many-collections-or-mapping-is-not-one-table-one-class.aspx
Bennor McCarthy
+1  A: 

How could the database know which contacts and phones go together unless you insert them? Unfortunately in SQL Server you can't update more than one table at once - a limitation shared by most if not all SQL DBMSs.

dportas
Well I figured if there was a foreign key constraint then it would know the many-to-many relationship that is already graphed
0A0D
The database has no way of knowing which row is related to which other row unless you tell it. Possibly your application does know because the application uses an object model with pointers to associate one item with another. There are no pointers in the database however.
dportas
a foreign key relationship is not a pointer?
0A0D
A foreign key is a reference, but it is not a pointer. No matter. With pointers, you would still have to tell the data structure which Contact owns which Phone.
Walter Mitty