0

我正在尝试使用 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

大圆函数:

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, CONSTRAINT phpvms_schedules_ibfk_1FOREIGN KEY ( code) REFERENCES phpvms_airlines( code) ON UPDATE CASCADE)

phpvms_airports:

在此处输入图像描述

phpvms_schedules:

在此处输入图像描述

4

1 回答 1

0

您正在尝试将icao值插入到phpvms_schedules. 您需要确保 FK CONSTRAINTphpvms_schedules_ibfk_1指向表icao上的列phpvms_airports而不是id列(我怀疑它当前指向的位置)。

或者

您需要在phpvms_airports.idFK 返回时使用这些值phpvms_airports

于 2013-08-08T16:40:34.460 回答