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 ,w开发者_Go百科hich 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!
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.
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
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
精彩评论