Dwarves
Memo
Type ESC to close search bar

Database design Circular

In the database solution design, there is the bad practices is called circular references.

What’s circular in the database?

Imagining one type of circular reference in SQL is made when a first table references a second and that second table references the first table. Simple example of how this would look in a model: students and professors in which students has a reference to professor as advisor and professor has a reference to student as advising student.

The principal problem on circular references is that when we start inserting data into the tables we won’t be able to since none of the references can accept nulls and we can’t insert one record without having another on the other table. A more complex example of circular reference is simple task management system. There are 4 entities in the solution: users, tasks, projects, project_assignments, project_teams:

So, a project has 2 ways to get the project_assignments. The problem occurs when we query the data. If we start at projects, tasks and then project_assignments, there the link ends, since project_assignments does not serve as the primary key for any other table. Then we do the same process with the other link, start at projects, go to project_teams, end up in project_assignments.

An easy way to identify a circular reference is to start on a table which is serving as the primary table for two or more foreign keys. Some database designs look like a circular references, but not true. An example is the purchasing system. There are some entities: products, purchases, commissions, customers, retailers. The products has many customers by 2 connections: purchases or commissions.

The concept of a circular reference can sometimes be confused with a diagram model that forms a circle, but as we saw in the example before, the model forms a circle, but there is no circular reference.

Types of circular

There are 3 types of circular references: self-reference, a circle, multi-table circular-references.

Challenges of Circular References

While circular references can be necessary, they come with significant challenges, including:

Circular in the real world

Circular references in database design are generally avoided due to complexity but can be useful in certain scenarios to model real-world relationships:

Solution and migrate from the existing database design

Solution for the database practice:

Conclusion

Circular references can be useful or even necessary in certain scenarios where relationships are bidirectional or complex, such as social networks, graph-like data structures, or cross-referencing entities. However, they should be used with caution due to the potential performance, integrity, and maintenance challenges they introduce. Where possible, other approaches (like intermediate tables or soft references) should be considered to avoid the pitfalls associated with circular references.

References