尝试运行以下查询时:
UPDATE Flight
SET FLNO = '1001'
WHERE FLNO = '1000';
我收到以下错误:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`user/FlightLegInstance`, CONSTRAINT `FlightLegInstance_ibfk_1` FOREIGN KEY (`FLNO`) REFERENCES `FlightLeg` (`FLNO`) ON UPDATE CASCADE)
以下是我的创建查询:
飞行:
CREATE TABLE Flight (
FLNO INTEGER NOT NULL,
Meal varchar(50) NOT NULL,
Smoking char(1) NOT NULL,
PRIMARY KEY (FLNO)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
FlightLeg:
CREATE TABLE FlightLeg (
FLNO INTEGER NOT NULL,
Seq char(25) NOT NULL,
FromA char(3) NOT NULL,
ToA char(3) NOT NULL,
DeptTime DATETIME NOT NULL,
ArrTime DATETIME NOT NULL,
Plane INTEGER NOT NULL,
PRIMARY KEY (FLNO, Seq),
FOREIGN KEY (FLNO) REFERENCES Flight(FLNO) ON UPDATE CASCADE,
FOREIGN KEY (FromA) REFERENCES Airport(Code),
FOREIGN KEY (ToA) REFERENCES Airport(Code)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
FlightLegInstance:
CREATE TABLE FlightLegInstance (
Seq char(25) NOT NULL,
FLNO INTEGER NOT NULL,
FDate DATE NOT NULL,
ActDept DATETIME NOT NULL,
ActArr DATETIME NOT NULL,
Pilot INTEGER NOT NULL,
PRIMARY KEY (Seq, FLNO, FDate),
FOREIGN KEY (FLNO) REFERENCES FlightLeg(FLNO) ON UPDATE CASCADE,
FOREIGN KEY (FLNO, FDate) REFERENCES FlightInstance(FLNO, FDate) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我认为错误是在FlightLegInstance的两个FK定义之一,但我不确定。任何人都可以帮我这个吗?
感谢。
答案 0 :(得分:0)
这不起作用,因为您已将第二个表中的[flno]列定义为foriegn键 解决这个问题的方法是你必须在两个地方更新它 感谢