3 回答

TA貢獻1858條經(jīng)驗 獲得超8個贊
從基類繼承的子類可以看作是數(shù)據(jù)庫中基類定義的弱實體,這意味著它們依賴于它們的基類,沒有它就不可能存在。我見過很多次,為每個子表存儲唯一的ID,同時也將FK保存在父表中。一個FK就足夠了,它更好的是為子表和基表之間的FK關(guān)系啟用ON-DELETE級聯(lián)。 在TPT中,只看到基表記錄,就無法找到記錄所代表的子類。這有時是必要的,當您想要加載所有記錄的列表時(不需要這樣做)。 select
在每個孩子的桌子上)。處理這一問題的一種方法是有一列表示子類的類型(類似于TPH中的rowType字段),因此以某種方式混合了TPT和TPH。
public class Shape {
int id;
Color color;
Thickness thickness;
//other fields
}
public class Rectangle : Shape {
Point topLeft;
Point bottomRight;
}
public class Circle : Shape {
Point center;
int radius;
}
以上類的數(shù)據(jù)庫設計可以如下所示:
table Shape
-----------
int id; (PK)
int color;
int thichkness;
int rowType; (0 = Rectangle, 1 = Circle, 2 = ...)
table Rectangle
----------
int ShapeID; (FK on delete cascade)
int topLeftX;
int topLeftY;
int bottomRightX;
int bottomRightY;
table Circle
----------
int ShapeID; (FK on delete cascade)
int centerX;
int center;
int radius;
- 3 回答
- 0 關(guān)注
- 462 瀏覽
添加回答
舉報