问题
I am creating a database table for investigations and i need to log the person who reported the incident, this could be a record from the supplier or user tables. The easiest way to do this would be to have both a suppleir and a user id column in my investigations table but that seems wrong, what's a better way to do this?
Thank you.
回答1:
You could have another two tables - IncidentsReportedBySupplier (IncidentID, SupplierID)
and IncidentsReportedByUser (IncidentID, UserID)
- which would remove the empty columns.
But this has disadvantages too. You can then potentially have incidents which aren't reported by anybody.
回答2:
I don't know why your option "seems wrong". I generally prefer your approach over having a column with FK to multiple tables. Your approach, while it may require nulls in the other column, is much more straightforward and obvious. NO DOCUMENTATION REQUIRED. No unnecessary and redundant tables... just an extra column.
General rule of thumb, less tables = less relationships you have to worry about = less headache.
回答3:
If both supplier and user tables have mutually exclusive unique ID numbers, you can have one column that uses that ID as the "reporter."
If they both have unique ID numbers that may have overlapping values, you can use two columns for the ID like:
`reporter_id`,
`reporter_type`
where the type could be an value like s
or u
to reflect the table names. This would also eliminate all those null values created by the method your proposed.
Finally, if they both tables do not have unique ID numbers, give them one! Tables of data about people work much better with primary keys!
来源:https://stackoverflow.com/questions/12035131/creating-database-table