2个实体之间的不同关系

Different relationship between 2 entities

在我的项目中,我有一部分说 -

An airline has many flights and each flight has one source (airport) and one or more destinations (airports). For each source destination pairs, you have to record the distance.

根据这个说法我做了这样一个erd -

                   _________
                  | Airline |
                   ---------
                       |
                      has
                   ____|____ 
                  | Flights |
                   ---------
               /               \
            s.has             d.has
      _______/______       ______\_______
     |source_airport|     | dest_airport |
      --------------       --------------

评论太长了。您可以使用两个 table 来表示给定的航班,一个用于位置,另一个用于将一个位置与另一个位置相关联 table。

CREATE TABLE locations (
    id INT NOT NULL PRIMARY KEY,
    name VARCHAR NOT NULL,
    latitude DOUBLE NOT NULL,
    longitude DOUBLE NOT NULL
);

CREATE TABLE flights (
    source_id INT NOT NULL,
    dest_id INT NOT NULL,
    PRIMARY KEY (source_id, dest_id),
    FOREIGN KEY (source_id) REFERENCES locations(id),
    FOREIGN KEY (dest_id) REFERENCES locations(id)
);

所以,现在航班只是 flights 交叉点 table 的一个入口。每个机场的元数据位于 locations table.