3 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊
從基類繼承的子類可以看作是數(shù)據(jù)庫(kù)中基類定義的弱實(shí)體,這意味著它們依賴于它們的基類,沒有它就不可能存在。我見過(guò)很多次,為每個(gè)子表存儲(chǔ)唯一的ID,同時(shí)也將FK保存在父表中。一個(gè)FK就足夠了,它更好的是為子表和基表之間的FK關(guān)系啟用ON-DELETE級(jí)聯(lián)。 在TPT中,只看到基表記錄,就無(wú)法找到記錄所代表的子類。這有時(shí)是必要的,當(dāng)您想要加載所有記錄的列表時(shí)(不需要這樣做)。 select
在每個(gè)孩子的桌子上)。處理這一問(wèn)題的一種方法是有一列表示子類的類型(類似于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ù)庫(kù)設(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)注
- 254 瀏覽
添加回答
舉報(bào)