Background
I have two tables, one called issues
and the other called status_checks
, which both contain (among others) the field comment
(VARCHAR
).
An issue
is raised by a user when something needs to be looked at by the support staff. A status_check
on the other hand is something that is done every so often by support staff and can (but doesn't have to) result in an issue
being raised.
So, for instance, a status_check
might look like this:
+----+---------+---------+--------------+---------+------------+
| id | user_id | date | satisfactory | comment | issue_id |
+----+---------+---------+--------------+---------+------------+
| 1 | 23 | 2014... | 1 | blah... | NULL |
+----+---------+---------+--------------+---------+------------+
or like this:
+----+---------+---------+--------------+-------------------+------------+
| id | user_id | date | satisfactory | comment | issue_id |
+----+---------+---------+--------------+-------------------+------------+
| 2 | 24 | 2525... | 0 | It all started... | 25 |
+----+---------+---------+--------------+-------------------+------------+
(Typically, an issue will only be raised if it's unsatisfactory (0
), but that's not required.)
Then, the issue for that second one would look something like this:
+----+---------+---------+-------------------+---------+--------------+
| id | user_id | date | comment | status | close_reason |
+----+---------+---------+-------------------+---------+--------------+
| 2 | 24 | 2525... | It all started... | open | NULL |
+----+---------+---------+-------------------+---------+--------------+
The Problem
So the question is... how can I avoid duplicating the comment field when an issue is raised by a status_check
? I could leave that field NULL
when an issue is raised, and simply put the comment contents in the issue
table (or vice versa)... but then that makes the SQL retrieval logic more complex. Having to optionally join to another table based on a field being possibly null seems like overkill, but I can't think of a better solution.
Normally, one would normalize by putting the comment in an issue and always having an issue_id
in the status_checks
table, but obviously that doesn't make sense here, since most status_checks
are not issues.
Possible solution?
The other way I thought of doing things seems like normalization gone wrong to me, but maybe it's actually better. That solution is to have a comments table:
+----+---------+
| id | comment |
+----+---------+
| .. | ....... |
+----+---------+
Then I would just include a comment_id in both tables, rather than the comment itself. It's still duplicating the data, but it's only duplicating an integer.
Can some other way of normalizing help me here? Or is one of the above the best I'm going to get?
EDIT: Looking at it now, I see that the user_id
and date
are also both duplicated, so maybe it's natural that the comment
be duplicated too? I guess it's only the size of the comment that makes me queasy about duplicating it. The comments-only table looks ugly, since a comment is such a general concept and the comments by themselves would just be floating in there with no context.