I have a table Item
with many items of many types. Differently typed items are linked to different resources each of whom have a table with primary key.
Currently to achieve this I have two columns in Item
table: Resource type
and Resource ID
.
The question is, what would be propper way to refactor Item
table in a way so I can set up foreign keys to Resource ID
?
Preferably without creating many new tables just for this reason or without adding many new columns to Item
table, where most of them would be empty.
**EDIT**
Additional note: aside from resource id's, all items also have multiple other fields, some of which may be used only by some Resource type
.
As far as I have looked it up, I am seeing 4 options:
- Have a table
Item
with columnsResource type
andResource ID
as well as all additional columns (case explained above).- (+) smaller amount of unused cells
- (-) some unused cells
- (-) no foreign keys on
Resource ID
- Have a table
Item
with additional column for eachResource type
as well as all additional common columns.- (+) availability of foreign keys on
Resource ID
- (-) a lot of unused cells
- (+) availability of foreign keys on
- Have a table
Item
with only common columns (or even with onlyItem ID
) + additional table with columns:ID
,Item ID
,Value type
,Value BIGINT
,Value INT
,Value STRING
. Second table will be used forResource ID
's as well as fields that are unique for one or anotherResource type
.- (+) smaller and constant amount of unused cells
- (+/-) more space per unique field, but overall smaller than in other cases.
- (-) no foreign keys on
Resource ID
- New table for each
Resource type
- (+) availability of foreign keys on
Resource ID
- (+) more control over each
Resource type
- (-) a lot of new tables, number of which will increase as new
Resource type
's will be introduced.
- (+) availability of foreign keys on
I am now evaluating, which of these cases will be more beneficial to my case.
Best Answer
It was decided to go with something between second and third options - adding additional table with columns
Value type
and separate columns for eachResource type
.Resource ID
Resource type
's to eachItem
Resource type
for each fieldItem
, when resource withResource ID
is deleted.