您需要区分汽车模型实例(“my car vin xxx”)和汽车模型规范(“2009 Mazda 3”)。一般来说,模型规范有一个型号,一个模型实例有一个序列号(在这种情况下是 VIN)。
您还需要区分模型规范的可用选项和模型实例的实际安装选项。
/* the model specifications: */
vehicle_model
id
model_name
from_year
to_year (nullable)
manufacturer_id
/* engine specifications */
engine
id
name (ex. "Cummings Turbo Diesel")
...
/* available engines for a model. you would use this table to show only the appropriate engines for a given model spec */
vehicle_model_engine
model_id
engine_id
/* vehicle instances: */
vehicle
id
vin
model_id FK vehicle_model
engine_id FK engine
您可以将一个多列外键从 vehicle(model_id, engine_id) 放到 vehicle_model_engine(model_id, engine_id),或者只将一个键放到 engine(id) 并在保存前使用应用程序逻辑进行检查。