1

I would like to create a ticketing PHP application, but I stucked at the planning of database structure.

I would have completely different type of ticket templates (user / shared folder / end-device processes etc..) which require different inputs.

The question is that, shoud I store them in one table, or should I create a separate table for every ticket group?

If I store them in one table, there will be many empty fields in the table because different tickets use different fields.

I think the common fields would be the unique ID, status and responsible person only, the others are different.

My preferred solution would be that if I store them in separate tables with the required fields only.

But this would cause troubles in a simple SELECT query when I would like to list every tickets. If I join them together during the query it will create a result table with many empty fields because of the difference between the structure of the tables which is not optimal. To avoid this I should run separate queries for every table which is not so simple.

What way would you choose? Could you please give me an advice? Thank you in advance!

Perfect Square
  • 1,368
  • 16
  • 27
  • You should look up data normalization. At the very least, you should show the structure of the different ticket data sets. I'm sure there are things in common between them. – Sloan Thrasher Jan 21 '18 at 08:24

1 Answers1

0

Never put different entities in the same table. Yes, they have some common fields, but a bunch of empty columns is a clear signal that such design should be avoided, and good sign is that you are aware of it :) If we jump at the moment from the database world into application world with OO patterns and classes, this would be typically solved by inheritance (is-a relationship). How to enforce this in SQL (specifically, MySQL)?

Divide data in separate tables with one common table, let's call it ticket_template, that will contain common fields (you mentioned template_id, status and responsible person). Other tables, such as user_template will be created like this:

CREATE TABLE user_template(
    user_template_id int PRIMARY KEY REFERENCES ticket_template(template_id)
    ...
);

and they should keep fields specific to that particular entity. If you want a common report, write multiple SQL selects and show their results one after another. If you want all of them together, just list common fields together.

Caveat - this design does not prevent confused user to insert a ticket_template that is not related to any of concrete templates (such as user_template), or that is related to many of them (more than 1). If the database supports deferred constraints (MySQL unfortunately doesn't), that can be enforced too, read more here.

Miljen Mikic
  • 14,765
  • 8
  • 58
  • 66
  • Thank you! This was very useful for me and I will follow your advices. – user2645964 Jan 27 '18 at 11:15
  • `a bunch of empty columns is a clear signal that such design should be avoided` That's not true. That's a sound and common approach for implementing inheritance in relational databases, it's called table-per-hierarchy, or TPH. – Arad Alvand May 01 '21 at 21:34