Relational database design - “cyclic” graphs

放肆的年华 提交于 2019-12-01 01:07:11

A FK (foreign key) constraint is directed. A FK declaration is a statement that subrow values for a table & column list appear as subrow values for some other "referenced" table & column list. When people talk about FK "cycles" they mean cycles of FK references all put head to tail.

You don't appear to have any such cycles here.

(Tables represent application relationships/associations. FK constraints are often called "relationships" although they are actually just statements about tables that are true in every database state. Although every FK has an associated query-expressible table representing an associated relationship/association.)

There's no logical problem with such cycles. When that happens, the tables all have exactly the same set of subrow values for those superkey/UNIQUE column lists. (Indeed there is a 2-way constraint between every pair of tables.) In the simple case where all the FK columns lists are the same (same names, same order) and all non-FK columns are different, it means that instead of the separate tables you could use just the one table that is their join. Otherwise, after suitable column renamings you could still use just one table.

But many DBMSs can't handle FK reference cycles being declared, because FK declarations do double duty for cascades on update, and the DBMS designers haven't offered a facility for the designer to say what order cascades should happen in when there's a cycle. So if you don't want a one-table design then you are forced drop the cycle by dropping one of the declarative FK constraints. Although you can enforce the constraint via a trigger, which is the only general constraint facility available in SQL DBMSs.

PS Since your first design probably has T3 (T1_Id_Fk, T2_Id_Fk) references T2 (T1_Id_Fk, T2_Id) and T2 (T1_Id_Fk) references T1(T1_Id), your 2nd design is probably not properly constraining.

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