Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to design 1:n relationships in Sql Server Management Studio? Expand / Collapse
Author
Message
Posted Sunday, March 1, 2009 4:02 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, November 14, 2012 5:01 AM
Points: 42, Visits: 145
Imagine I have two tables: Manager and 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? In Access you can choose between 1:1 and 1:n due to the connector style (crows feet and all that), but not in Sql Server.

Thanks
Post #666297
Posted Sunday, March 1, 2009 5:56 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, December 13, 2013 4:35 AM
Points: 386, Visits: 211
Have a look at what it generates.

You will have two tables.
One will have a foreign key that references the other

create table manager
(
manager_id int
)

create table player
(
player_id int ,
manager_id int ,
foreign key (manager_id) references manager (manager_id)
)

(something like that).

So yes - it does matter which way round the tables are as you want the foreign key to on player not manger.

The 1-n part is enforced by a unique index on manager.manager_id and player.player_id


Try doing this using scripts - you'll proably find it easier to understand what is going on.



Cursors never.
DTS - only when needed and never to control.
Post #666308
Posted Monday, March 2, 2009 4:52 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 3:50 AM
Points: 13,991, Visits: 28,373
info (3/1/2009)
Imagine I have two tables: Manager and 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? In Access you can choose between 1:1 and 1:n due to the connector style (crows feet and all that), but not in Sql Server.

Thanks


It matters, but it's the structure that makes the difference. If you have a Manager, let's say the PK is the ManagerID, and you have a Player, who's PK is PlayerID (maybe not the best choices, I'm not arguing that, they're easy for references). In your tables, if neither has a field that represents the PK of the other, then you can't establish a relationship. But if the Player has a ManagerID field, then you can't ever have 1:n from the Player to the Manager. Think about it. Only a single Manager can possibly be associated with the player in that situation. But look at it from the other point of view. A manager can have any number of players with his ID in the associated field, making it 1:n.

To make it go the other way for the player to the manager, the manager would have to have a field that associates to the player, but, that's going to make for very small football teams.


----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server Query Performance Tuning
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #666476
Posted Monday, March 2, 2009 3:15 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, November 14, 2012 5:01 AM
Points: 42, Visits: 145
I found an article which said the following:

Manager ---> Player

You will link the manager table to the player table because the player table will only need 1 manager for each player row. Whereas the manager table would need 11 players per manager row. It kind of ties in with what you say about only being able to have 1 manager associated to 1 player.

This will create an FK in the Player table.

Is this the right way of thinking about this? I will post the link for reference.
Post #666936
Posted Monday, March 2, 2009 6:02 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 3:50 AM
Points: 13,991, Visits: 28,373
That's probably the right way to think about it. By the way, I wasn't talking about 1 player to 1 manager. I was attempting to describe the two directions that physical relationship can go. If you understand the physical layout, the logical construct should be easy.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server Query Performance Tuning
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #666987
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse