SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to design 1:n relationships in Sql Server Management Studio?


How to design 1:n relationships in Sql Server Management Studio?

Author
Message
info-849042
info-849042
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 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
nigelrivett
nigelrivett
SSC-Addicted
SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)SSC-Addicted (480 reputation)

Group: General Forum Members
Points: 480 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.

Grant Fritchey
Grant Fritchey
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39149 Visits: 32616
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 and SQL Server Execution Plans
Product Evangelist for Red Gate Software
info-849042
info-849042
Valued Member
Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)Valued Member (72 reputation)

Group: General Forum Members
Points: 72 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.
Grant Fritchey
Grant Fritchey
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39149 Visits: 32616
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 and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search