假设您设置了一个数据库来存储各种车辆的碰撞测试数据。你想存储快艇,汽车和卡丁车碰撞测试的数据。数据库设计中的继承问题
您可以创建三个独立的表格:SpeedboatTests,CarTests和GokartTests。但是很多列在每个表中都是相同的(例如,执行测试的人的员工ID,碰撞方向(前,侧,后)等)。然而,大量的列会有所不同,所以你不想只把所有的测试数据放在一张表中,因为你会有很多列对于快艇总是空的,但是有很多列会一直存在对于汽车来说是空的,并且对于卡丁车来说,其中的很少一些将是空的。
假设您还想存储一些与测试没有直接关系的信息(例如正在测试的东西的设计者的员工ID)。这些专栏似乎根本不适合放入“测试”表格,特别是因为它们会在同一辆车上的所有测试中重复使用。
让我来举例说明表格的一种可能的排列方式,以便您可以看到所涉及的问题。
Speedboats id | col_about_speedboats_but_not_tests1 | col_about_speedboats_but_not_tests2 Cars id | col_about_cars_but_not_tests1 | col_about_cars_but_not_tests2 Gokarts id | col_about_gokarts_but_not_tests1 | col_about_gokarts_but_not_tests2 Tests id | type | id_in_type | col_about_all_tests1 | col_about_all_tests2 (id_in_type will refer to the id column of one of the next three tables, depending on the value of type) SpeedboatTests id | speedboat_id | col_about_speedboat_tests1 | col_about_speedboat_tests2 CarTests id | car_id | col_about_car_tests1 | col_about_car_tests2 GokartTests id | gokart_id | col_about_gokart_tests1 | col_about_gokart_tests2
什么是这种结构的好/坏,以及实施类似这样的东西的首选方式是什么?
如果还有一些信息适用于您希望在车辆表中使用的所有车辆,该怎么办?那么CarTests表会看起来像这样...
id | vehicle_id | ... With a Vehicles table like this: id | type | id_in_type (with id_in_type pointing to the id of either a speedboat, car, or go-kart)
这似乎是一个皇家混乱似乎。应该如何设置这样的东西?
可能的重复[如何有效地建模数据库中的继承?](http://stackoverflow.com/questions/190296/how-do-you-effectively-model-inheritance-in-a-database) – Musa 2014-08-25 13:45:23