If we change a primary key value, why don't we have to change a dependent column value?

折月煮酒 提交于 2021-02-08 08:43:20

问题


I watched one tutorial on youtube about database normalization.

The table looks like this:

|Item(PK) | Supplier | Supplier Phone | Price|
---------------------------------------------
| Xbox One| Microsoft| 1234           | 250  |
---------------------------------------------
| PS4     | Sony     | 4321           | 300  |
---------------------------------------------
| PS Vista| Sony     | 4321           | 400  | 
---------------------------------------------

According to the tutorial this table is in 2NF and every column depends on the PK.

What I don't understand is how column Supplier depends on the PK and has the same value for different rows. If the logic is that B (supplier) depends on A (PK), and we change A, should B be changed? Why in this case would the Supplier for the changed PK remain the same?


回答1:


TL;DR Item/A being PK (primary key) implies that there's only one Supplier/B value per Item/A value. It doesn't say that there's only one value per table.

That video is nonsense. They are suffering from the same misconceptions about "depends" & FDs (functional dependencies) & PKs as your question (so maybe that's where you got them from) and many other misconceptions and they don't know what they are talking about. Find an college/university textbook, slides and/or course (of which many are free online).


We can talk about the FDs, superkeys, unique column sets, CKs (candidate keys) & PK of a table value or a table variable. A table variable has an instance of one of those things if every table value that can arise in the given business/application has it as an instance.

What I don't understand is how column Supplier depends on PK and has the same value for different rows.

A column set is functionally dependent on another when the determined set can only have one value per given value of the determining set.

But that doesn't imply that different determining values have different determined values.

A superkey is a set of columns that has a different subrow value in every row. A CK is superkey containing no smaller superkey. A PK is some CK that you chose to be PK. Every column set functionally depends on every superkey. Some of which are CKs. One of which can be PK.

But that doesn't imply that some subrow other than a superkey/CK/PK (and every subrow must be functionally dependent on them by definition) is unique.

If the logic is that B (supplier) depends on A (PK), and we change A, should B be changed?

Not on the basis of A being PK/CK. A being PK/CK implies that A and every superset of A is unique. If you "change A" in the sense of looking at a different row or looking at a different table value that has a different A value for that row (which can't be in any other row of either table), that doesn't restrict what B can be.

Some other constraint might restrict what B can be. Eg if the FD {Supplier} -> {Supplier Phone} holds then that restricts what Supplier Phone can be in other rows even though Item being PK does not.

Whether any particular FD holds depends on what rows are supposed to go into a table in a given situation & what situations can arise. Then what FDs hold determines what the superkeys & CKs are. Then you can pick a CK as PK.




回答2:


The confusion might have arisen from the misunderstanding that the phone number belongs to a customer, where in reality it is a Supplier Number.

In this case if you have a product X, you know it has to come from supplier Y, and that supplier an only have 1 phone number Z. It shows you that Z is directly dependent on X. If you have an XboX the phone number will be 1234, if you have a PS4 it will be 4321.

Of course item doesn't depend on the phone, because you can have many items for the same phone but that's for a different step in normalisation.

So ideally we would have supplier table with name + phone, and in product table only reference to supplier_id



来源:https://stackoverflow.com/questions/46839375/if-we-change-a-primary-key-value-why-dont-we-have-to-change-a-dependent-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!