user9969
user9969

Reputation: 16060

Noddy question on Relationship sql server between 2 tables.Can you clarify?

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!

Upvotes: 3

Views: 134

Answers (3)

Ron Tuffin
Ron Tuffin

Reputation: 54648

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.

Upvotes: 1

Jagmag
Jagmag

Reputation: 10366

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

Upvotes: 0

Adriaan Stander
Adriaan Stander

Reputation: 166486

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

Upvotes: 0

Related Questions