Search code examples
database-designormdecoupling

Low coupling and SQL joins


Say I have a table people (id, firstname, lastname).

There are two other tables that should contain these fields, so we'll just reuse the people table: users (id, username, person_id) and companies (id, name, contact_person_id).

Now to get companies or users we must join the people table. If we change the people table, we must rewrite all queries, and probably lots of code.

Is this a real problem? Is my DB structure flawed? Is there a solution to maintain low coupling, like maybe ORM?

Thank you for all anwers.


Solution

  • Most of the time, the kind of modifications that would be made would not be disruptive, such as adding new columns. Breaking changes, like modifying column names or data types, is hardly ever done.

    Relational database management systems allow the creating of special data types which make certain modifications much easer. If FirstName and LastName were defined as a user-defined type PersonName, then changing the type would make the same change appear in all the queries and stored procedures that use the columns. Unfortunately, hardly anyone ever uses user-defined data types.

    If, conceptually, the thing called "Person" that is part of the things User and Company really represents a coherent idea, then changes to Person will not be disruptive, because any changes that are needed are needed everywhere. If, on the other hand, this is hacking conceptually dissimilar things together for convienience sake, then you are likely to encounter problems down the road.