Question

I have the following tables:

Users
_____
UserID
Data
...
Roles
_____
RoleID
Data
...
UsersRoles
_____
UserID
RoleID

Users and Roles are in a M:N relationship. As such I have the UsersRoles table to connect them.

Not connected to the previously mentioned tables, I also created the following tables:

Invoices
_____
InvoiceID
Data
...
Items
_____
ItemID
InvoiceID
Data
...

In this case I did not create an additional table since I am realizing a 1:N relationship here. My question is, if there is any reason for me to remove the InvoiceID from Items and create an InvoicesItems table similar to the UsersRoles table to connect them, even though I would not need it.

Était-ce utile?

La solution

even though I would not need it.

No. If you add it without any expectation of needing it, then it only serves to:

  • Add overhead when querying the data
  • Add overhead when adding or updating data
  • Cause confusion for other people working on the DB
  • Be a potential source of inconsistent data and other bugs

On the other hand if you think it is likely that there will be a need for the relationships to become many-to-many in future, for instance a line item being on multiple invoices due to partial payments or refunds, then there may be a case for adding the extra structure now to save on refactoring effort later but I would be wary of adding it from the start because of the above reasons unless I was very sure it was going to be needed at some point soon.

If you do add the extra structure but don't want to allow many-to-many to be a thing just yet, you have extra work to define constraints that enforce the desired one-to-many behaviour until it is no longer needed.

Autres conseils

There are two reasons why a few designers make this choice.

  • Because they think the relationship may be modeled as M:N in the near future.
  • Because the relationship is optional, and they wish to avoid using NULLS.

Whether either of these reasons are sound ones or not is somewhat subjective.

Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange
scroll top