tags:

views:

34

answers:

3

Hi, apologise for silly question but there you go.

Sql server 2 tables Customer and Order A customer can only have one order.

When using the Sql server diagram and you want to create a relationship between the customer.CustomerId and order.CustomerID ,which way do you drag the arrow? From the customer to the order or from the order to the customer?

Generally speaking does it matter which way you do it? Where can I read about it? Or can you clarify it.Thanks!

A: 

Costomer to Order

That would be the same as having a LEFT JOIN

SELECT *
FROM Customer c LEFT JOIN
Order o ON c.CustomerID = o.CustomerID

You cannot have an order entry without the corrosponding customer entry, but you can have an customer with no order.

You should also maybe have a look at FOREIGN KEY Constraints

astander
Thanks for quick reply. I just get confused when drawing a diagram in sql server which way you drag the arrow.Still not clear to me
+1  A: 

The customer.customerID field should be a Primary Key for the customer table.

Then it does not matter which way you drag the arrow. SQLServer is clever enough to figure it out.

As per a comment that was added to this answer... You should drag the 'arrow' from the referenced table to the referencing table (in your case from the Customer to the Order table). Then check the relationship that SQLServer automatically assigns.

Ron Tuffin
What about when you want to reference a `UNIQUE` constraint? What if the same column(s) comprise a `UNIQUE`/`PRIMARY KEY` in both tables i.e. a one-to-one relationship?
onedaywhen
@onedaywhen. good points but those were not the questions that were asked.
Ron Tuffin
@Ron Tuffin: Are you sure? "A customer can only have one order" suggests to me a one-to-one relationship (admittedly is doesn't necessarily infer that an order can have only one customer but it seems likely). Also there is no mention of `PRIMARY KEY`, multiple candidate keys, etc so why assume "customer.customerID field should be a Primary Key"?
onedaywhen
A: 

The foreign key is in the Order table.

Hence i instinctively always drag from order (table with the foreign key) to customer (the table having the primary key / unique key).

However, the only reason why it would matters which way you drag the arrow is based on whatever the tool you are doing the modeling allows you to do

In the end, what you are doing is creating a foreign key relationship from Order table to Customer Table on the column CustomerId

InSane
Thanks for your reply.Are you saying if using Sql Server diagram functionality it does not matter which way you drag the arrow as Sql server will figure it out?