停止代码和旅程之间存在多对多关系。要消除这种情况,您需要分解关系。
为此,您需要一个中间表,我们称之为 JourneyStopCode,如下所示:
JourneyStopCode:
JourneyStopCodeID (primarykey)
JourneyID
StopCodeID
那么您的 Stop Code 表将没有 JourneyID 字段。
要检索旅程的停止代码,您需要执行以下操作:
SELECT * FROM StopCode
INNER JOIN JourneyStopCode ON StopCode.StopCodeID = JourneyStopCode.StopCodeID
INNER JOIN Journey On Journey.JourneyID = JourneyStopCode.JourneyID
WHERE JourneyID = @yourJourneyID
编辑:可视化:
--------------- --------------------- ----------------
| Journey | | JourneyStopCode | | StopCode |
--------------- --------------------- ----------------
| JourneyID |<--- | JourneyStopCodeID | --->| StopCodeID |
| Description | |----| JourneyID | | | Latitude |
--------------- | StopCodeID |------ | Longitude |
--------------------- ----------------
然后您的数据将如下所示:
旅行
----------------------------------------
| JourneyID | Description |
----------------------------------------
| 34 | Southampton - Portsmouth |
----------------------------------------
停止代码
----------------------------------------
| StopID | Latitude | Longitude |
----------------------------------------
| SSDAFS | 12345 | 67890 |
----------------------------------------
JourneyStopCode
------------------------------------------
| JourneyStopID | JourneyID | StopCodeID |
------------------------------------------
| 1 | 34 | SSDAFS |
------------------------------------------