1

I am making a database plan, and I am a little confused on Normalising to the Second Normal Form. I am also worried about the large number of columns, and I can't rightly figure out what to do with them.

This is my Table that I am focusing on MatchDetails:

Idea 1

Attempt with Compound Key

img link

The Player_ID is a Unique Primary Key for another table Users. MatchID is a Unique Primary Key for the table Matches. The relationship between Matches and Players is many to many.

Would this work as a compound key? In the sense that 1 player can only have taken part in a particular Match once? Do the columns to the right of MatchID Have a functional dependency on the Compound key, in the sense that they are unique TO that Compound key?

Idea 2

Table with Primary Key img link

In this example, the Participation_ID is a Unique Primary Key for the table, since there can be multiple instances of the same Player_ID and the same MatchID for various combinations of Players and Matches.

In this example, I would guess that this column is in Second Normal Form because there is only one Primary Key, and the Match Values are unique, and are thus functionally dependent? I am a little confused on Functional Dependency despite trying to read about it here.

Oh and another small thing...

The final thing that I am a little in doubt about, is the huge number of columns. All of the information to the right of MatchID are details about HOW the player (Player_ID) performed in the match (MatchID). Should they be in another table?

Link to other tables if you would like to see the layout so far: https://i.stack.imgur.com/5GIUG.png

Please ignore that MatchID doesn't have an underscore and the other ID's do, It's only an excel plan!

manlio
  • 18,345
  • 14
  • 76
  • 126
k4kuz0
  • 1,045
  • 1
  • 10
  • 24

2 Answers2

2

The columns {PlayerID, MatchID} seem to work as a compound key.

The columns to the right of MatchID do have a functional dependency on the (compound) primary key, as long as they represent that player's statistics in one particular match.

If those columns instead represent the players overall statistics, then they're dependent only on PlayerID, and this design is not in 2NF.

The normal forms take into account every candidate key, not just the primary key. The fact that you later add an integer row identifier, ParticipationID, doesn't change anything in my previous paragraphs--the columns {PlayerID, MatchID} still seem to be a (compound) candidate key, and you have to take them into account.

There's no such thing as "I don't have too many columns" normal form. If you need 20 attributes that are functionally dependent on every candidate key, then you need 20 attributes that are functionally dependent on every candidate key.

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
  • Thanks a lot for the answer! "There's no such thing as "I don't have too many columns" normal form." Haha that is true. However I have seen a lot of horror stories about having too many columns, and therefore am trying to see how I can stop that before it becomes a problem! I posed a question about this to the above answerer, if you get a chance I'd greatly appreciate a reply from someone! – k4kuz0 Feb 26 '14 at 08:23
  • @k4kuz0 _"I have seen a lot of horror stories.."_ - Some of that comes from using ORMs that are [not very flexible](http://use-the-index-luke.com/blog/2013-04/the-two-top-performance-problems-caused-by-ORM-tools) when you need just a subset of columns. That's not really a problem in the database itself, but a friction introduced by the tools on top of it. – Branko Dimitrijevic Feb 26 '14 at 11:35
2

Unless the same player can participate in the same match more than once, you'll have to have a composite key {Player_ID, MatchID}, whether you add another key (such as {Participation_ID}) or not.

Adding {Participation_ID} key only makes sense if you have some other tables that reference it1 and you want to make their foreign keys slimmer, or if you use a particularly hostile ORM that requires a non-composite primary key.

Do the columns to the right of MatchID Have a functional dependency on the Compound key

Yes.

You can think of a "functional dependency" simply as a way for saying that the relation (a set of tuples) is a function. For a relation to be function (in the mathematical sense of that word), it must always produce same "result" for same "arguments".

If the attributes of the given key are the "arguments" and the rest of the attributes are the "result", then no two different results can ever be produced from the same arguments, simply because the key is unique and therefore any particular combination of values of key attributes2 cannot identify more than one tuple.

So all attributes are always functionally dependent on the key. That is always true for any key, otherwise it wouldn't be a key.

The only question is whether some non-key attribute is also dependent on the proper subset of the key attributes. If it is, you have violated the 2NF.3

In your case, if any of the attributes depends on Player_ID alone (or MatchID alone), that would violate the 2NF.

The final thing that I am a little in doubt about, is the huge number of columns. All of the information to the right of MatchID are details about HOW the player (Player_ID) performed in the match (MatchID). Should they be in another table?

Looks like they are where they need to be from the logical standpoint. It is unlikely, but possible, that you might have some physical reasons for vertically partitioning the data.4

Some unrelated suggestions:

  • Use consistent naming: if there is Player_ID, there should be Match_ID, not MatchID (or vice-verse). Whops, I missed your last sentence.
  • Use singular for table names, for the same reason singular is typically used for class names in OOP.

1 Which you don't as far as I can see.

2 Aka. "prime" attributes. Strangely enough, a prime attribute does not have to belong to a "primary" key (it can belong to an alternate key), so just saying "key attributes" is probably a better terminology, IMHO.

3 Obviously, this is only a concern for composite keys, because if a key has only one attribute, its proper subset is empty.

4 DBMSes can typically handle hundreds or even thousands of columns these days, and this doesn't really qualify as "huge number of columns".

Community
  • 1
  • 1
Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
  • Thank you for the in depth reply. I am feeling a lot clearer on my problem. If you see this I'd like to ask something quickly on top of my main question. When I asked about moving some of the data to another table, it is because It seems to me that a general consensus is "more rows rather than more columns". With that in mind, would [this](http://i.imgur.com/15qMFcm.png) be better than having all the data in 1 table? Even if all that data is dependent on the same compound key. – k4kuz0 Feb 26 '14 at 08:21
  • @k4kuz0 Well, Item_0, Item_1 etc. are new - they were not in your original question. This looks suspiciously like a 1:N relationship, which is typically modeled through a separate table. The 1:1 relationship, on the other hand, is typically modeled as just one table. BTW, I've never heard of "more rows rather than more columns" consensus. Nor I understand how can splitting to separate table remove columns - if anything, you'll duplicate the key and just create _more_ columns, albeit spread-out over more tables. – Branko Dimitrijevic Feb 26 '14 at 11:10
  • The Item 1 through 6 represents the slots that a player has (6 item slots), and the values represent the items in each slot. There is, and will always be, 6 items only. No more no less. That does not violate 1:N does it? Or am I wrong? I see what you mean about the columns. So in that case, I would have a table with 23 columns, and that is ok? – k4kuz0 Feb 26 '14 at 11:17
  • @k4kuz0 _"There is, and will always be, 6 items only."_ - in that case, this is not a real 1:N and you can just keep explicit columns. 23 columns is OK. – Branko Dimitrijevic Feb 26 '14 at 11:29