Creating database table

て烟熏妆下的殇ゞ 提交于 2019-12-23 07:46:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!