I am a bit indecisive about a certain database table structure, here is the scenario:
I have an “invoices” table, and an invoice can be for a “product” or for a “service” (products are in a table, and services in a separate table).
What is the best way to link an invoice to either of those, but also allow adding more options? If it was an invoice for only a product, the table design would be:
id, product_id, amount
But what is the best way for this scenario? I was thinking something like:
id, source_id, invoice_type (product or service), amount
But I thought that it might not be a good idea.
If there aren’t any differences between
productandserviceinvoices, just use one table with aninvoice_typefield.After reading your entire question, what you listed at the end is right. What you should look at is examples of a subclass and superclass.
Basically, all your fields that are the same can be grouped into one table, then fields that only pertain to a specific type of service can be grouped into its own table. Instead of having 1 or 2 tables, you’ll end up with a max of 3 (a superclass and 2 subclasses where needed), and they will be more normalized.
Given your example (Note: this is just an example):
Superclass:Invoice
Subclass:Product
Subclass:Services