English 中文(简体)
Can an attribute designate one table over another?
原标题:

I m creating a sports statistics database. With it, I d like to catalog game/match statistics for many types of sports. For example, this database would be able to tell you how many touchdowns the Carolina Panthers scored in the 09-10 season (football), or how many free throws were made by the Miami Heat in their last game (basketball).

I m having trouble designing one of the more fundamental tables called Matches. The Matches table has columns for:

  • ID (PK match_id)
  • date of play (play_date)
  • IDs referring to the performances of the teams (FK team_1_performance_id and team_2_performance_id) in table Performances.

The Performances table holds:

  • ID (PK perf_id)
  • team ID (FK team_id)
  • And most importantly, all the other stats like: number of strikes (*)
  • average rushing yards per play (*)
  • percent of 3-pointers made (*)

(*)The problem is, how can I make the Performances table relevant to the respective sport? For example, baseball games have strikes, but soccer and hockey do not (nor does any other sport I can think of). I don t want my Performance table to have a column for strikes when its only going to be relevant for a portion of records.

Or do I? Perhaps my design should be different all together? How would you go about this?

Now, I don t know if this is possible, but one idea I had was to maybe include some kind of perfomance table ID column in Matches that refers to different performance tables. So that when I query a match s performances, it will look at a specific table. This is where the title of this question comes from (Can an attribute designate one table over another?). Imagine "SELECT team_1_performance.strikes FROM Matches INNER JOIN appropriate_performance_table AS team_1_performance WHERE Matches.performance_table_id = Baseball " How could I designate appropriate_performance_table, if that s even possible?

And another idea I had was to create matches tables for all the sports, like Rugby_Matches or Football_Matches, and then respective performance tables for those sports, like Rugby_Perfomances or Football_Performances. This just seems like a lot of tables that represent somewhat similar things.

If you can, try to keep your responses MySQL specific.

Thanks!

最佳回答

If you go with Astander here s the query you d need for all the wins for Carolina when the scored more touchdowns then interceptions.

If you did it the right way, in columns, you d see

SELECT * FROM Football_stats  fs
WHERE fs.team_fk = (something that resolves to Carolina)
fs.outcome =  Win  And fs.touchdowns > fs.interceptions

in the EAV world you d get

SELECT game_id FROM football_stats WHERE fs.team_fk = [Carolina] and stat_type =  Outcome  and stat_value =  Wins )
INTERSECT
SELECT game_ID FROM 
  (SELECT game_id, stat_value FROM football_stats WHERE fs.team_fk = [Carolina] and stat_type =  Touchdown  ) tds,
  (SELECT game_id, stat_value FROM football_stats WHERE fs.team_fk = [Carolina] and stat_type =  Interceptions ) ints
WHERE
  tds.stat_value > ints.stat_value

And all that did was give you a list of game_ids that satisfy the query, if you want the rest of the values, like points for and against, it s whole new rounds through the data.

问题回答

Instead of accross, create the data going down.

So you would have

The Performances table holds:

  • ID (PK perf_id)
  • team ID (FK team_id)
  • Performance Stat Type
  • Performance Stat Value

Or something like that.

You will then also have to create a Rules table, that will link specific Performance Stat Types to specific Sport Types.

This will then also allow you to easily add new Performance Stat Types without majorly impacting your database schema.

You can then also implement display orders, or even display groupings if you like.

Create a "Metrics" (or "Stats") table, that defines the different things you will measure.

  Table Metrics
    MetricId int,
    MetericName (Runs Batted In, Touchdowns, FreeThrows, etc.)
    MetricAbbreviation Nullable?
    Sport (That Metric belongs to )

Then your MatchStatistics table will have

  Table MatchStatistics
    MatchId   
    MetricId
    MetricValue Decimal

The PK on this table would be MatchId and MetricId. You could also have a PlayerStatistics Table that would look similar, except it would have PlayerId instead of MatchId

Your idea to create sports-specific tables is generally what is done.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签