gpt4 book ai didi

mysql - 添加多个外键报错

转载 作者:行者123 更新时间:2023-11-29 04:17:46 24 4
gpt4 key购买 nike

我试图在表 'routes1' 上添加多个外键,但它仍然给我一个错误 Cannot add foreign key constraint,我是否遗漏了一些代码?提前致谢。这是代码片段

CREATE TABLE `routes1` (
`Airline` varchar(3) default NULL,
`Airline_ID` int(11) default NULL,
`Source_Airport` varchar(4) default NULL,
`Source_Airport_ID` varchar(5) default NULL,
`Destination_Airport` varchar(4) default NULL,
`Destination_Airport_ID` varchar(4) default NULL,
`CodeShare` char(1),
`Stops` int(1),
`Equipment` text,
FOREIGN KEY(Airline_ID) REFERENCES airlines(Airline_ID)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(Source_Airport_ID) REFERENCES airports(Airport_ID)
ON DELETE CASCADE ON UPDATE CASCADE
);

这是“airport”引用表的创建表脚本

CREATE TABLE `airports` (
`Airport_ID` int(5),
`Name` text,
`City` text,
`Country` text,
`IATA/FAA`text default NULL,
`ICAO` text,
`Latitude` double NOT NULL,
`Longitude` double NOT NULL,
`Altitude` double NOT NULL,
`Timezone` double not null,
`dst` char(1) default NULL,
`Tz_Database_time_zone` text,
PRIMARY KEY (`Airport_ID`)
);

最佳答案

请查看Airline_ID 和Source_Airport_ID 数据类型的变化:数据类型应与外键的源表相同。

CREATE TABLE `routes1` (
`Airline` varchar(3) default NULL,
`Airline_ID` int(5) default NULL,
`Source_Airport` varchar(4) default NULL,
`Source_Airport_ID` int(5) default NULL,
`Destination_Airport` varchar(4) default NULL,
`Destination_Airport_ID` varchar(4) default NULL,
`CodeShare` char(1),
`Stops` int(1),
`Equipment` text,
FOREIGN KEY(Airline_ID) REFERENCES airlines(Airline_ID)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY(Source_Airport_ID) REFERENCES airports(Airport_ID)
ON DELETE CASCADE ON UPDATE CASCADE
);

如果答案正确请标记为答案。谢谢。

有关更多信息,您可以查看以下链接:

Supports transactions, row-level locking, and foreign keys

关于mysql - 添加多个外键报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36444406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com