2010-12-02 73 views
0

我有4個表:問題多個引用外鍵

1 - tbl_cars

id (PK) 
car_name 
model 
year 

2 - tbl_carOwner

id (PK) 
carId (FK - reference to tbl_cars) 
ownerId (FK - reference to tbl_ownerPersnoal or tbl_ownerCompany or tbl_ownerGov) 
ownerType (to make a differentiation about owner) 

3 - tbl_ownerPersonal

id (PK) 
name 
ic_no 
address 

4 - tbl_ownerCompany

id (PK) 
registration_no 
business_type 
name 
address 
fax 
mail 

5 - tbl_ownerGov

id (PK) 
agency_name 

正如你所看到的,我需要做,因爲不同領域的所有者之間的區別。

如何將所有表與mysql和nhibernate相關聯?

回答

1

你不能這樣做 - 這違背了關係數據庫設計的基本原則。外鍵永遠只能引用一個且只有一個表 - 你不能有引用三個表的一個外鍵...

你需要做的是在你的tbl_carOwner

ownerIdPersonal (FK to tbl_ownerPersonal) 
ownerIdCompany (FK to tbl_ownerCompany) 
ownerIdGov  (FK to tbl_ownerGov) 
有三個FK關鍵

並且最好確保在任何給定時間只有其中一個鍵如果有效(NOT NULL)。