我正在尝试使用SQL脚本自动为每个机场添加时间表。这是我到目前为止的代码:
INSERT INTO phpvms_schedules (depicao, arricao, distance)
SELECT
depart.icao,
arrive.icao,
ROUND(GreaterCircleNm(depart.lat,depart.lng,arrive.lat,arrive.lng),2) as Nm
FROM phpvms_airports AS depart
INNER JOIN phpvms_airports AS arrive ON depart.icao <> arrive.icao
GreaterCircleNm功能:
DROP FUNCTION IF EXISTS GreaterCircleNm;
DELIMITER go
CREATE FUNCTION GreaterCircleNm( lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT ) RETURNS float
BEGIN
DECLARE pi, q1, dist FLOAT;
SET pi = PI();
SET lat1 = lat1 * pi / 180;
SET lon1 = lon1 * pi / 180;
SET lat2 = lat2 * pi / 180;
SET lon2 = lon2 * pi / 180;
SET q1 = ACOS(sin(lat1)*sin(lat2)+COS(lat1)*COS(lat2)*COS(lon1-lon2));
SET dist = q1*180*60/pi;
RETURN dist;
END;
go
DELIMITER ;
总而言之,我真的需要帮助修复此错误:
1452 - 无法添加或更新子行:外键约束失败(
virtu259_phpvms
。phpvms_schedules
,CONSTRAINTphpvms_schedules_ibfk_1
FOREIGN KEY(code
)参考phpvms_airlines
(code
)ON UPDATE CASCADE)
phpvms_airports:
phpvms_schedules:
答案 0 :(得分:0)
您正尝试将icao
值插入phpvms_schedules
。您需要确保FK CONSTRAINT phpvms_schedules_ibfk_1
指向icao
表上的phpvms_airports
列,而不是id
列(我目前指向它的位置) )。
OR
您需要在FK返回phpvms_airports.id
时使用phpvms_airports
值。