A Composite Primary Key is a primary key that is made of multiple columns. It’s also often to hear the term Candidate Key, which refers to the same thing.
Partial Dependency occurs when one of your non-key columns is functionally dependent on only a part of the Composite Primary Key, and not the whole thing. Let’s look at an example.
Suppose we have a table with various fitness test results for different players.
|1||1||87/100||John Stewart||30m Sprint|
|1||3||56/100||Alicia Madison||Vertical Jump Test|
|3||2||90/100||John Stewart||One Rep Max|
|2||2||71/100||Isaiah Gray||One Rep Max|
The primary key in this case is a Composite Primary Key that is a made up of player_id and test_id.
Both the points and the supervisor_name columns are directly dependent on the entire primary key (player_id + test_id). The test_type column, on the other hand, is only dependent on test_id0 and has nothing to do with player_id.
This is Partial Dependency. It happens when an attribute in a table depends only on a part of the primary key and not on the whole key.
Why and how to fix it?
Partial Dependency leads to duplicate entries, and if we have a lot of duplicate entries in our tables, our database will take up a lot more memory than it should. Partial Dependency, unless fixed, can also lead to insertion, deletion and updation anomalies. What do these mean?
Say we had to insert a 100 new results into the table. Many players would do the same types of tests (eg. One Rep Max), so this would lead to plenty of unnecessary duplicate entries in the test_type column. This is a typical example of an insertion anomaly.
What would happen if we wanted to change the name of a single test type? For example, we want to change the ’30m Sprint’ to the ’30m Dash’. Well, then you would have to find each row in the table that contains ’30m Sprint’ entry and manually replace each one of those with ’30m Dash’. It is obviously a lot of unnecessary hassle and we call this updation anomaly.
What if, for some reason, we decided to delete this entire table? Than, we would automatically delete all the information about the different tests as well, which is not something we want. We want the specific test type information to be completely standalone and independent of this table. We call this problem a deletion anomaly.
But fortunately, it’s really easy to remove partial dependencies from your database. In this example, all we have to do is split the original table into two new tables, like this.
|2||One Rep Max|
|3||Vertical Jump Test|
Now the test_type column if fully dependent on the primary key of that table, and nothing else. By splitting up the table we take care of all three already mentioned anomalies.
Transitive Dependency occurs when you have a column that is functionally dependent on another column that is not part of the primary key. Lets look at an example.
Say we had a table with information on each specific football player:
The columns height, weight and club_id are directly dependent on the primary key, while club_country is not. club_country is only dependent on the club_id column, and not on the primary key, which is player_id.
This is an example of transitive dependency. It occurs when a column is functionally dependent on another column which is not a part of the primary key.
It’s really easy to take care of transitive dependency. All you have to do is split the original table into two new tables, where each column of the tables will only be dependent only on the primary key of that table.
The new club table only contains information on the different clubs. As you can see here, club_country is now completely dependent on the club_id column, which is the primary key in the new club table.
Just like with Partial Dependency, by doing this we take care of insertion, deletion and updation anomalies. Also, by reducing the amount of duplicate entries, we reduce the amount of memory required to store the database.
Partial and Transitive Dependencies need to be removed in order to have your database in the Second and the Third Normal Form, respectively. Now, whether you want to have your data in the Second or the Third Normal Form is a question for another topic. You can read more on that topic here.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Did I miss something or make a mistake somewhere?
Let me know in the comments below.