I'm creating er-diagram for "Airport" database. And I got stick with tables "Flight" and "Airport" . In table "Flight" I have 2 columns: "Departure_place" and "Arrival_ place". Both of it should be connected to values in table "Airport". So, How can I make that in the proper way on er-diagram and on sql?
我正在为“机场”数据库创建er-diagram。我坚持使用桌子“飞行”和“机场”。在表格“飞行”中,我有两列:“Departure_place”和“Arrival_ place”。它们都应该连接到表“Airport”中的值。那么,我怎样才能在er-diagram和sql上以正确的方式实现呢?
1 个解决方案
#1
-1
I think you are looking for foreign keys. Here is a tutorial for that:
我想你正在寻找外键。这是一个教程:
https://www.w3schools.com/sql/sql_foreignkey.asp
https://www.w3schools.com/sql/sql_foreignkey.asp
In your case first create the tables:
在您的情况下,首先创建表:
create table flight(
idFlight int primary key,
idDeparturePlace int,
idArrivalPlace int);
create table airport(
idAirport int primary key);
Now you have to add tow foreign keys. One for idDepaturePlace
and one for idArrivalPlace
.
现在你必须添加两个外键。一个用于idDepaturePlace,另一个用于idArrivalPlace。
Follow this syntax:
请遵循以下语法:
ALTER TABLE <child table> ADD FOREIGN KEY (<col in child table>) REFERENCES <master table>(<col in master table>);
In your case you have to add this both keys to your flight table:
在您的情况下,您必须将这两个键添加到您的航班表:
ALTER TABLE flight ADD FOREIGN KEY (idDepaturePlace) REFERENCES airport(idAirport);
ALTER TABLE flight ADD FOREIGN KEY (idArrivalPlace) REFERENCES airport(idAirport);
#1
-1
I think you are looking for foreign keys. Here is a tutorial for that:
我想你正在寻找外键。这是一个教程:
https://www.w3schools.com/sql/sql_foreignkey.asp
https://www.w3schools.com/sql/sql_foreignkey.asp
In your case first create the tables:
在您的情况下,首先创建表:
create table flight(
idFlight int primary key,
idDeparturePlace int,
idArrivalPlace int);
create table airport(
idAirport int primary key);
Now you have to add tow foreign keys. One for idDepaturePlace
and one for idArrivalPlace
.
现在你必须添加两个外键。一个用于idDepaturePlace,另一个用于idArrivalPlace。
Follow this syntax:
请遵循以下语法:
ALTER TABLE <child table> ADD FOREIGN KEY (<col in child table>) REFERENCES <master table>(<col in master table>);
In your case you have to add this both keys to your flight table:
在您的情况下,您必须将这两个键添加到您的航班表:
ALTER TABLE flight ADD FOREIGN KEY (idDepaturePlace) REFERENCES airport(idAirport);
ALTER TABLE flight ADD FOREIGN KEY (idArrivalPlace) REFERENCES airport(idAirport);