An app I\'m currently designing allows users to create custom competitive leagues (think of it kind of like fantasy sports) and each user can join different leagues and each
[Paraphrased] Because the point settings could vary from round-to-round, should I have:
- one table for the default league-level point setting
- and then another for round-level point setting
that will record the point setting actually implemented for each round?
Yes. What you are implicitly declaring is, a round has one setting, which is:
That is a typical OR Gate in Logic, and there is a correct method to implement that in a RElational database: an Exclusive Subtype.
I currently have one table for league player and one for round player
That is correct: they are two discrete Facts, the latter is Dependent on the former. That means:
and then table that tracks the points earned for each player that basically has foreign keys linking to a bunch of other tables
Your data model goes off the rails at that point.
Evidently, you have learned the value of data integrity. Eg. you are attempting to ensure that a player in a round in a league is actually a player who is registered in that league; etc. Excellent. You are trying to achieve Relational Integrity, which is logical (and distinct from Referential Integrity, which is a physical feature of SQL).
The second problem, however, is that you don't have keys, you have physical Record IDs
... declared as "keys". Therefore the logical thing (data is logical) that you are trying to constrain isn't constrained. And that effort, that attempt, results in scores of relationships ... that do not achieve the desired result.
Relational Key
Record ID
. You are using Record IDs
declared as "keys" (that will confuse the hell out of you, because it is not a Key, and it has none of the properties of a Key). And then trying to get some Relational Integrity (which you intuitively know only the Relational Model provides) through Composite Keys ... but you are using the declared Non-Keys, so it fails, and makes a complex model in the attempt.
Record IDs
Also, your CPK
is a great attempt to overcome the limitations of the "theoreticians", but it does not specify precisely what columns make it up. That is easily corrected if you use IDEF1X (the standard for modelling Relational data): the columns that make up the Key, Primary or Alternate, are explicit.
The next problem is, your logical rows (as distinct from physical records) are not unique, and the RM demands that logical rows are unique.
User
, username
is not uniqueusername
is actually the logical Key (which would make the rows unique)(first_name, last_name)
, which is a second logical Keyuser_id
is 100% useless (achieves nothing, it is merely an additional column and an additional index, which is to be avoided)username
is the real, logical, PRIMARY KEY
, which is migrated as FOREIGN KEY
wherever it is referenced.Likewise, you can get rid of all the Record IDs
.
You have been schooled in the physical (falsely named "relational"), and you have tried to 'move up' into the logical. Great idea, but that does not work. The logical comes first, and when ready, one 'moves down' into the physical. The physical serves the logical, not the reverse.
Try this.
All my data models are rendered in IDEF1X, the Standard for modelling Relational databases since 1993
My IDEF1X Introduction is essential reading for beginners
The IDEF1X Anatomy is a refresher for those who have lapsed.
Relational Key
round_player
must be registered in the league
that the round
is inround_default_weight
must be one of the valid league_weights
that have been set up for the league
that the round
is inExclusive Subtype
round
has either one round_default_weight
xor one round_custom_weight
I do not understand what you mean precisely by point_setting
. I understand it to be a weight that is applied to the score, which is modelled.
I do not understand why you have Point Earned
as a separate file (ie. separate to the issue of multiple parents). That appears to be one record per point scored. Assuming that only players can score points, you can instead accumulate points into the round_player
row.
Your design allows multiple admins per league
, not one. Please confirm.
Enjoy. Please feel free to ask specific questions. There may be clarifications: when identified, I will issue a progressed data model.
If I track points in the
round_player
table would I only be able to track the total points earned during around
?
Yes.
For the points earned in each round I wanted to keep track of each point accrued by each player during each round so that you can look back and see the specific types of points and in what quantities they were earned for a player.
Ok. Try this.
There is no need to maintain one row per round_player
per round
per point
. We can maintain a row per point_type
containing total points per point_type
instead.
You need to specify the point_types
(I have given rugby point types as an example).
It is a normal table, not a Reference or "look-up" table, because the relation is Identifying.