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

  • 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

  • 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.

  • 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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • 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.

  • 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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply