SQL foreign key references

瘦欲@ 提交于 2019-12-10 22:24:47

问题


Does

FOREIGN KEY (a) REFERENCES A(a),FOREIGN KEY (b) REFERENCES A(b)

Has the same meanning as in :

FOREIGN KEY (a,b) REFERENCES A(a,b)?

回答1:


No.

Having two references statement means that both a and b appear in A independently. That is, they are valid values, but they don't have to appear together. Think of "February 30". It has a valid month and a valid day of month, when each are checked separately.

Having a single references statement means that a and b appear in A together. Hence, "February 30" would fail this test.

Traditionally, a foreign keys refer to a primary or unique key. This is not enforced in MySQL, but it is a useful guideline (and also useful in practice). So, in the example with two references, this would usually mean that both a and b are unique. Although this is not enforced, it gives a good indication of how the keys will be used. In the second case, the combination of a and b is unique.



来源:https://stackoverflow.com/questions/22702528/sql-foreign-key-references

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