2016-05-15 50 views
0

我有這張3表,這是可用旅行,國家和位置。在我的ERD設計中的關係是如何解決這個風扇陷阱在sql設計

位置| M-1 |國家| 1-M |可用旅行

這是我的決議

create table country 
(
countryID int not null IDENTITY(1,1) PRIMARY KEY, 
countryName varchar(50), 
passportRegulation text, 
currency varchar(20), 
) 


create table location 
(
locationID int not null IDENTITY(1,1) PRIMARY KEY, 
locationName varchar(100), 
countryID int references country(countryID)on delete cascade 
) 

create table availableTrip 
(
availableTripID int not null IDENTITY(1,1) PRIMARY KEY, 
countryID int references country(countryID), 
locationID int references location(locationID) 
) 

嘗試是否有可能,如果有兩個外鍵添加到現有行我是正確的,但是我覺得這是多餘的,因爲基於位置我可以知道這個國家。我有點失去了方向這樣的設計

+0

是LOCATON ref足夠了。 –

回答

1

假設countryId需要在locationId匹配的國家,那麼你只需要在availableTriplocationId

你會使用查找相應的國家加入:

select avt.*, l.countryid 
from availabletrip avt join 
    location l 
    on avt.locationid = l.locationid; 

如果countryId能夠從locationId是不同的,那麼你會希望兩列。