What is proper way to design common values in relational database?
Example - table car_make
:
id | name |
---|---|
1 | Opel |
2 | BMW |
Table car_model
:
id | name | make_id |
---|---|---|
1 | Astra | 1 |
2 | Vectra | 1 |
3 | I7 | 2 |
4 | X4 | 2 |
5 | Other | ? |
In that example I want every make to have additional "Other" model despite its models.
If we omit the "Other" value, one-many relationship seems to be the most logical because every make has it own models. But if we introduce many-to-many relationship for say thousands of makes just to have the "Other" model for every make, won't it be waste of resources?
Answers
If you want every car make to have an additional "Other" model, it might be more efficient to handle this directly in the car_model
table rather than introducing a many-to-many relationship or duplicating data. Here's a suggestion:
-
Include the "Other" model in the car_model table: You can simply add a single row in the
car_model
table for the "Other" model, and set itsmake_id
to NULL or some special value that represents "Other". This way, each make will have an associated "Other" model without the need for additional relationships or duplicated data.Example:
id name make_id --------------------- 1 Astra 1 2 Vectra 1 3 I7 2 4 X4 2 5 Other NULL or special value
-
Use a flag or special value to identify the "Other" model: Instead of using a separate row for the "Other" model, you can use a flag or special value in the
car_model
table to indicate that a particular model is the "Other" model. This approach requires less storage space and avoids duplication of data.Example:
id name make_id is_other -------------------------------- 1 Astra 1 0 2 Vectra 1 0 3 I7 2 0 4 X4 2 0 5 Other NULL 1
Both of these approaches allow you to represent the "Other" model for every car make without introducing unnecessary complexity or wasting resources. Choose the one that best fits your requirements and design preferences.