views:

1198

answers:

2

Imagine I have two tables: Manager, Player.This is for a football team, where several players can play for one manager only and a manager manages several (11) players.

When designing the relationship in Sql Server, at the time the popup window comes up with the properties for the relationship, does it matter which side the tables are?

So in other words is there a difference in connecting the key from Player to Manager or from Manager to Player?

And how do I specify the relationship as 1:n or is it automatically decided as 1:n or 1:1?

Thanks

A: 

There is a difference. The player needs to have a foreign key constraint that references the primary key (or another unique key) of the manager.

  • source table: foreign key constraint
  • target table: primary/unique key constraint

The primary key constraint needs to be in place before you can create a foreign key (constraint). It is automatically 1:n (or 0:n if your foreign key is nullable, i.e. there are players without a manager).

cdonner
So to design a 1:1 relationship, do I add a constraint to simply say a manager cannot belong to two players?
dotnetdev
See the comment to the other answer. You have a 0:n or 1:n situation, not 1:1. A manager can have zero/one or more players. 1:1 would be if a manager has 1 and only 1 player, which is very rare and you would just store that player in the manager table entirey.
cdonner
+1  A: 

You connect Player to Manager, which will create a FOREIGN KEY Player (manager_id) REFERENCES Manager (id)

A relationship will be 1:n unless there there is a UNIQUE CONSTRAINT on manager_id in Player table, which is not your case.

As a rule, 1:1 relationships are stored in one table.

Quassnoi
So I assume the table to be 1: will be the Primary Key table, which I connect from, right?
dotnetdev
1 manager (to which is the reference) can have N players (from which is the reference)
Quassnoi