假設您設置了一個數據庫來存儲各種車輛的碰撞測試數據。你想存儲快艇,汽車和卡丁車碰撞測試的數據。數據庫設計中的繼承問題
您可以創建三個獨立的表格: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