我有几个表,假设我有一个汽车表,还有另一个表,其中包含所有类型的可用汽车(只是为了避免汽车表上有多个条目),所以我想对我的汽车表进行约束,该表具有“ TypesOFCar 表中的汽车类型列表/集,此表包含(制造商、型号等),我如何存档此表。
我希望它是模块化的,这样我就可以将另一种汽车添加到 TypeOfCar 表中,然后它就可以在 Cars 表中使用,请提前谢谢。
I have a couple of tables, lets say I have a cars table and I have another table which hold all types of cars available(just to avoid multiple entries on the cars table) so I want a constraint on my cars table that has a "list/set" of types of cars FROM the TypesOFCar Table this table contains (Make, Model, etc..) how can I archive this.
I want it modular so I can just add another kind of car to the TypeOfCar table and it becomes available on the Cars table, thx in advance.
发布评论
评论(2)
实现这一点的最佳方法是通过外键约束。本质上,您派生的表如下所示:
在这些示例表中,我创建了一个外键约束,将
Car
表的CarType_Id
列映射到Id<
CarType
的 /code> 列。此关系强制要求CarType
项必须存在于Car
表中指定的值。The best way to implement this would be through a foreign key constraint. Essentially, you derive your tables such like:
In those example tables, I create a foreign key constraint that maps the
CarType_Id
column of theCar
table to theId
column of theCarType
. This relationship enforces that aCarType
item must exist for the value being specified in theCar
table.您想要将 CarType 列添加到 Cars 表中,并将其设为 外键。
You want to add a CarType column to your Cars table and make it a foreign key to your TypeOfCar table.