If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Doubt designing a database

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-05-11, 13:34
frigopie frigopie is offline
Registered User
 
Join Date: Sep 2011
Posts: 10
Doubt designing a database

I'm creating a system where users of it can create football teams and introduce players in these teams. One player can belong to more than one team. I would like to know if the solution I have thought is the best one, database is implemented with MySQL.

Using three tables, one to store the players name,a second one which links these teams with the players. And the third one to store the football teams name.

· Names of the columns in table "players" are:

Id(int) player_name(varchar) team_relation(int)


· Names of the columns in table "relations":

team_relation(int) team_name01(boolean) team_name02(boolean)


· Names of the columns in table "teams" are:

Id(int) team_name(varchar)

If I create teams Arsenal and Celtic and introduce the player Bob in the two teams, tables "players", "relations" and "teams" would be:


#Id(int) player_name(varchar) team_relation(int)
01 Bob 01

#team_relation(int) Arsenal(boolean) celtic(boolean)
01 true true

#Id(int) team_name(varchar)
01 Arsenal
02 Celtic

So, is this the best solution? Each time I create a fotball team I have to add a colum in table "relations"

Tahnk you very much,
Frigo
Reply With Quote
  #2 (permalink)  
Old 09-05-11, 17:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
how can a player belong to two teams????
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-06-11, 03:44
frigopie frigopie is offline
Registered User
 
Join Date: Sep 2011
Posts: 10
Because system is supposed to work like this. I have found a different solution.

One table storing each team you create:

Id Team_name
0 Arsenal
1 Celtic

One table storing players
Id Player_name
0 Bob
1 Peter

as many table as teams you created, for example "table_arsenal" (In case: Bob belongs to Arsenal)

Id Player_Id
0 0

"table_celtic" (Bob and Peter belong to Celtic).
Id Player_Id
0 0
1 1

What do you think?

Thank you very much in advance,
Reply With Quote
  #4 (permalink)  
Old 09-06-11, 04:23
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
a table per club is frankly a pants idea
why
each and every time some wants to add a new team you have to create a new table and almost certainly modify the application code.

so the solution would be to have an entity (table) for teams, and then the associate a player with a team, look up intersection tables. if a player could only play for one team once you could have TeamID & PlayerID as the primary key, however your rules stop that. arguably you could add the dates the player played for that team to the above PK, however thats messy, So Id suggest retaining the autonumber ID column as your PK but havign 4 columns PlayerID (Foreign Key to players), TeamID (FK to teams), DateJoined, DateLeft.

you will need to build in some code in the applciation to make certain the dates dont' overlap. you need to ensure
1 dateleft isn't before datejoined
2 the dates don't overlap ie registered a date left Team A as after the date joined Team B
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 09-06-11, 04:53
frigopie frigopie is offline
Registered User
 
Join Date: Sep 2011
Posts: 10
healdem ,thank you very much for you response. It's very useful, I understand it now. My two earlier solutions, creating a column for each club and creating a table for each club weren't ok.

Players and teams are an analogy of my system. It was my mistake, because it has been confusing instead of making it clear. The point is player can be playing for different teams at the same time.

Using your solution. What's up if I say that a player can play for all the teams and then create a new team? this new team won't have this player, player should join the new team automatically when team is created, so I have to add somewhere in the database that a player plays for all the teams. What do you think?

Thank you very much in advance,
Frigo
Reply With Quote
  #6 (permalink)  
Old 09-06-11, 05:45
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
how can a player play for more than one team at any one time?
if your analogy isn't a good representation of the real world then its not very useful. if it masks the real world problem then it becomes part of the problem itself

if you want to enroll a player for a new team then do that as part of your new player registration. wheter you implement that as a stored procedure (ie adding a new player triggers an automatic registration in the playersteams entity) or do that as part of your application logic, IE after inserting a new row then insert a new row into each team.

it does be the question of why you would need a player / team entity if a player is a member of both / all teams automatically.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #7 (permalink)  
Old 09-06-11, 06:07
frigopie frigopie is offline
Registered User
 
Join Date: Sep 2011
Posts: 10
I don't know if I explained to you right.

Players can play for any team, several teams, all teams or even not playing for any team. My database is strctured like this now:

entity players:
Id Player_name
01 Bob
02 Peter
03 Stephen
04 Michael

entity teams:
Id team_name
01 Arsenal
02 Celtic
03 Manchester
04 Fulham


entity relations:
Id_player Id_team
01 01
01 02
02 02
04 01
04 02
04 03
04 04

Michael plays for all teams. If somebody adds a new team in the teams entity (05 Liverpool), I have to check somewhere which players are playing for all teams and now there is nowhere to do that.

Solution would be, adding a second column in players entity storing a boolean value which says if they play for all teams or not. And when somebody adds a new team, check this values to add players in this new team. Is it a good solution?
Reply With Quote
  #8 (permalink)  
Old 09-06-11, 06:25
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
...sorry you've lost me
Your analogy is masking the problem
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #9 (permalink)  
Old 09-06-11, 06:40
frigopie frigopie is offline
Registered User
 
Join Date: Sep 2011
Posts: 10
Let me appologise.

Without analogy:

There are websites and advertisments. Websites contain advertisments. In my analogy websites are football teams and advertisments are players. Advertisments can be added to one, several or all websites, even not to be added to any website.

entity advertisments:
Id advertisment_name
01 microsoft
02 sony
03 ericsson
04 mercedes

entity teams:
Id website_name
01 web01
02 web02
03 web03
04 web04

entity relations:
Id_advertisment Id_website
01 01
01 02
02 02
04 01
04 02
04 03
04 04

Microsoft advertisment is in all websites. If somebody adds a new website in the websites entity (05 web05), I have to check somewhere which advertisments are in all websites and now there is nowhere to do that.

Solution would be, adding a second column in advertisments entity storing a boolean value which says if they are published in all websites or not. And when somebody adds a new website, check this values to add advertisments in this new website. Is it a good solution?

Sorry for the misunderstanding.
Reply With Quote
  #10 (permalink)  
Old 09-06-11, 07:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by frigopie View Post
There are websites and advertisments.
ahahahahahaha

arsenal and celtic, indeed

no wonder you got crappy answers, when you ask crappy questions

please, don't waste people's time
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On