Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 12th, 2005, 01:25 PM
Registered User
 
Join Date: Feb 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Avoiding multiple Relationships

Hello everyone,

I'm somewhat new to SQL and i'm facing a problem that I'm sure could be of an easier solution.

Right now I have a table with players names and stats for each player. I have a second table called 'Teams' that will have, among other things, 22 fields, each one with a player code referred by the 'Players' table.

How do I establish the relationship? It's a 1 to many, mind you. It will actually be possible for a player to be in more than one team. Each team is a person owned AFL (Australian Football League) "dream team". But do I really have to establish 22 relationships between the players table and the team table? Or the problem is in the players team and I shouldn't be having 22 fields, one for each player? In that case how do I go about changing it?

Thank you,
Marden Lee

 
Old February 12th, 2005, 06:48 PM
Authorized User
 
Join Date: Apr 2004
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Normally, you would would not create the TEAMS table with 22 player columns. You would create a PLAYER table with player information, a TEAM table with team information, and a TEAM_PLAYER table that maps players to teams. Something like

Code:
CREATE TABLE team (
  team_id   NUMBER PRIMARY KEY,
  name      VARCHAR2(100),
  owner     VARCHAR2(100)
);

CREATE TABLE player (
  player_id  NUMBER PRIMARY KEY,
  name       VARCHAR2(100)
);

CREATE TABLE team_player (
  team_id    NUMBER REFERENCES team( team_id ),
  player_id  NUMBER REFERENCES player( player_id )
);
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
 
Old February 15th, 2005, 09:15 AM
Authorized User
 
Join Date: Feb 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to deniscuba
Default

In this case, if each player belong to team player,then the perfect diagram will be :

team (
       team_id <sqlType> PRIMARY KEY,
       name <sqlType>,
       owner <sqlType>
      )

player (
         player_id <sqlType> PRIMARY KEY,
         name <sqlType>,
         team_id <sqlType>
)
 where team_id is foreing key that references to team table

 
Old February 15th, 2005, 12:00 PM
Authorized User
 
Join Date: Apr 2004
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Except that the original requirement envisions one player on an arbitrarily large number of teams. That's the reason that you need the mapping table TEAM_PLAYER (which would probably be better named ROSTER).

Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
 
Old February 16th, 2005, 08:30 AM
Registered User
 
Join Date: Feb 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes that is indeed the case.
Thank you Justin. That was exactly what I was missing; a 3rd table to establish the relationships.






Similar Threads
Thread Thread Starter Forum Replies Last Post
avoiding duplications josephx XSLT 5 January 5th, 2008 04:29 PM
Multiple relationships from table Vince_421 Access 2 May 30th, 2007 11:34 AM
Multiple tables with relationships Please Help Lesviper Access VBA 6 February 3rd, 2005 08:20 PM
Avoiding Duplicates fixitman Access 1 April 27th, 2004 10:43 AM
Avoiding Displaying duplicate data saleaf10 Classic ASP Databases 1 November 26th, 2003 10:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.