Jump to content

1 table or multiple tables


brown2005

Recommended Posts

say for instance i have

 

sports_players 1345353 entries

 

football_players 334255 entries

rugby_players 145345 entries

tennis_players 345243 entires

and so on....

 

obviously i know the separate entries in each red bits would be quicker to read but if i wanted a website which can read the same as sports players, is this possible or would putting them all in the same table be the best way?

Link to comment
Share on other sites

If you wish to normalise your data and make it easier to work with from a queries point of view then I suggest you have a "players" table and a "sports" table, where you link the sport to the player. This way you don't have to alter you schema to have a new sport added, you just insert a new row into the "sports" table.

e.g.

CREATE TABLE sports (
sports_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
sports_name VARCHAR(64) NOT NULL
) Engine = InnoDB;

CREATE TABLE players (
player_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
player_name VARCHAR(64) NOT NULL,
sports_id INT UNSIGNED,
FOREIGN KEY fk_sports_id (sports_id) REFERENCES sports(sports_id) ON UPDATE CASCADE ON DELETE SET NULL
) Engine=InnoDB;

 

Unfortunately you're incorrect in your assumption that reading would be quicker from the separate tables. Firstly there will be a code overhead to decide which table you're supposed to be working with, and Secondly because the tables are indexed the seek time will be identical.

 

To find all players that play "rugby" you will execute the following statement:

SELECT p.player_name 
FROM players p 
JOIN sports s ON p.sports_id = s.sports_id
WHERE s.sports_name = 'rugby';

 

Easy as pie, and nice and fast to boot.

 

If you want the sport that a particular player plays then you can do the following:

SELECT p.players_name
      ,s.sports_name
FROM players p
JOIN sports s ON p.sports_id = s.sports_id
WHERE p.player_name = 'Henrik Larson'

Link to comment
Share on other sites

sorry what i meant was wouldnt it be quicker looking, say if i was on website

 

www.myfootballwebsite.com

 

then i would only be coding to look at the football tables anyway wouldnt that be faster than using one table for all sports and looking up just the football records in that table...

 

but i will go the way of using like you above, which is what i was thinking of doing, if using the one table, is fast enough.

Link to comment
Share on other sites

In answer to your question: "no" , using one table per sport is not going to be any quicker realistically with indexing, and more to the point it makes maintenance appalling.

 

As I mentioned in my previous post, inserting a new sport is going to require building a whole new table, whereas using the structure I supplied you merely have to insert 1 record into the sports table.

 

It will probably also make cross-sports statistical analysis easier too.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.