How to store tickboxes/dropdown in the database?
See the UI design below:
An Item can have more than 1 group of tickboxes and/or dropdowns, how do I store these data in the database?
I have come up with this database design:
items
table:
+----+--------------+
| id | name |
+----+--------------+
| 1 | Plain Burger |
+----+--------------+
extra_group
table:
+----+---------+--------+--------------------+--------------+
| id | item_id | name | description | control_type |
+----+---------+--------+--------------------+--------------+
| 1 | 1 | Sauces | | checkboxes |
| 2 | 1 | extras | Choose your extras | dropdown |
+----+---------+--------+--------------------+--------------+
and finally extras
table:
+----+----------------+------------------+------+
| id | extra_group_id | value | cost |
+----+----------------+-------------开发者_高级运维-----+------+
| 1 | 1 | with Kitchup | 0.00 |
| 2 | 1 | with Mayo | 0.00 |
| 3 | 2 | with Boiled Rice | 0.00 |
| 4 | 2 | with Chips | 0.00 |
+----+----------------+------------------+------+
Is this the correct way of database design or what can be improved?
Thanks
Looks good, I would be inclined to add a table between item and extra_group (and remove the item_id from extra_group)
item_extra_group table:
+---------+----------------+
| item_id | extra_group_id |
+---------+----------------+
| 1 | 1 |
| 1 | 2 |
+---------+----------------+
This will enable you to model a many-to many relationship, as I would guess that you will have lots of repeating extra_groups e.g (cheese burger, bacon burger) would probably all have the same extras?
That design is just fine. You used the third normal form, which is the accepted way of doing this sort of things.
Here is what you should do to tweak your design, which is pretty good now, but could be improved slightly:
items
table:
+----+--------------+
| id | name |
+----+--------------+
| 1 | Plain Burger |
+----+--------------+
extra_group
table:
+----+---------+--------+--------------------+--------------+
| id | item_id | name | description | control_type |
+----+---------+--------+--------------------+--------------+
| 1 | 1 | Sauces | | checkboxes |
| 2 | 1 | extras | Choose your extras | dropdown |
+----+---------+--------+--------------------+--------------+
A new table options
like so:
+-----------+------------------+------+
| option_id | description | cost |
+-----------+------------------+------+
| 10 | with Kitchup | 0.00 |
| 11 | with Mayo | 0.00 |
| 12 | with Boiled Rice | 0.00 |
| 13 | with Chips | 0.00 |
+----+------+------------------+------+
A modified extras
table:
+----+----------------+-----------+
| id | extra_group_id | option_id |
+----+----------------+-----------+
| 1 | 1 | 10 |
| 2 | 1 | 11 |
| 3 | 2 | 12 |
| 4 | 2 | 13 |
+----+----------------+-----------+
This design is in 3NF and will make your data maintenance much simpler and will avoid problems of inconsistent pricing of extra items.
精彩评论