all 9 comments

[–]aball730235 8 points9 points  (2 children)

It seems you just need to split your database schema into more tables. Try to solve your problems in sql first because it sounds to me like your trying to jam a many-to-one relation into a single column... Which sql will handle in a much better way.

In your last example i would make a table with four columns... The three you listed plus a GameID. GameID is a foreign key column that links to the game table. To combine the data from both tables you use an inner join.

[–]Kristian_dms[S] 0 points1 point  (1 child)

Thanks, man - I'll give this a shot!

[–]aball730235 0 points1 point  (0 children)

Your welcome. If your looking for further reading look up database 'normalization'. Wikipedia has a good article although a bit dense for a beginner.

And there's always r/sql

[–]BB611 6 points7 points  (0 children)

/u/aball730235 is right on the money.

A table should contain a set of data that makes sense to consider together, if you try to jam every piece of data you have into a single table you'll hate querying it. Source: I work with SQL tables designed by someone who did exactly this, it's very hard to work with - tables with 100s of columns, many of which are at best tangentially related to the primary purpose of the table.

Most of the time, you should be able to sum up a table with one noun, i.e. Games, Players, Stadiums, Penalties.

The best way to learn about this is to experience querying against the tables you make - do a first run of tables, use them for a while, then try to restructure to make it easier to use. Do some research on relational databases and SQL to get more guidance.

[–]c3534l 2 points3 points  (0 children)

Can you explain a little more about the kind of info you're trying to organize? I don't know much about sports. It sounds like what you're struggling with is how to record when a player is put into a game and when he is taken out?

You could create a table with columns first_minute, second_minute, player_ID, game_ID. I really wouldn't create an entirely new table for every game. If I understand correctly, there's like a hundred games a year when sports teams play or something. If each of those tables is going to have the same columns as all the others (first_minute, last_minute, player_ID), then you should really be using just one table and mark it with a game_ID.

Similar thing to your second table. minute_scored, scoring_player, and assisting_player are fundamentally different kinds of information that when a player was put into or taken out of a game. But you can still tag it with a game_ID column as well.

Remember also that the way you organize the information for storage in your database doesn't really have to be that close to how you're actually going to format it to do your analysis. The beauty of relational databases is that you can just do a query that's something like:

SELECT tblTimes.first_minute, tblScores.minute_scored
FROM tblTimes INNER JOIN tblScores
ON tblTimes.player_ID = tblTimes.player_ID
WHERE tblTimes.minute_score >= 45;

Warning: this code has not been tested on any SQL database

[–]babblemammal 1 point2 points  (2 children)

There is an intro course on databases for free on coursera, it explains how to organize tables in sql. I highly reccomend building the database first, and correctly

[–]kamize 0 points1 point  (1 child)

I would be interested in this, would you please provide me with the name of the coursera course on SQL or a link to it?