I'm developing a desktop application to help the technician to easily provide updates to an embedded system.
The background is very simple the database (SQLite
) have several tables, not identical but quite similar, where each row represent an item. For this question we can say every table has this structure: id
, value
.
My problem is to design the table packages
.
A "package" may contain 0, 1 or more items from 0, 1 or more tables.
Let's see a basic example:
Table Foo
id | value |
---|---|
1 | abc |
2 | def |
Table Bar
id | value |
---|---|
1 | ghi |
2 | jkl |
Here some examples of packages:
pkg1: from Foo (1)
pkg2: from Bar (1, 2)
pkg3: from Foo (1, 2) and from Bar (2)
in the real case the item's tables are 10. My first attempt was pretty naive:
Table packages, #1
id | name | idFoo | idBar |
---|---|---|---|
1 | pkg1 | 1 | NULL |
2 | pkg2 | NULL | 1 |
2 | pkg2 | NULL | 2 |
3 | pkg3 | 1 | NULL |
3 | pkg3 | 2 | NULL |
3 | pkg3 | NULL | 2 |
The id*
fields are foreign keys to the related tables.
I don't like it since it requires as many columns as there are tables, and as many rows as there are items to be inserted. Another downside is the primary key would be composed of all the fields to ensure there are no dupes.
A second attempt was to put all the id
s inside the same field:
Table packages, #2
id | name | idsFoo | idsBar |
---|---|---|---|
1 | pkg1 | 1 | NULL |
2 | pkg2 | NULL | 1,2 |
3 | pkg3 | 1,2 | 2 |
This is a bit better since I have one row per package and the primary key can be the usual id
. But the ids*
columns are not foreign keys anymore, so I cannot guarantee the integrity of the database.
What are the downsides of the last approach?
id
,value
." - this appears to be the root of your problem. What exactly do these tables represent, and how are they "similar but not identical"?