我试图将我的数据插入到我创建的表中,我得到以下两个错误。我不太明白这些错误试图告诉我修复或改变我的数据。有人能指出我在正确的方向,哪些数据会导致这些错误出现。我将附上我的代码下面的错误。谢谢大家。
错误码:1452。不能添加或更新子行:外键约束失败(
meraki
。truck
, CONSTRAINTtruck_ibfk_1
FOREIGN KEY (TruckMakeID
,TruckModelID
) REFERENCEStruckmodel
(TruckMakeID
,TruckModelID
))
错误码:1452。不能添加或更新子行:外键约束失败(
meraki
。allocation
,约束allocation_ibfk_1
外键(TruckVINNum
)引用truck
(TruckVINNum
)
代码如下
CREATE DATABASE meraki;
- List item
USE meraki;
create table TruckMake(
TruckMakeID char(5),
TruckMakeName varchar(20),
primary key(TruckMakeID));
-- create Truckmodel
create table TruckModel(
TruckMakeID char(5),
TruckModelID char(4),
TruckModelName char(4),
primary key(TruckMakeID, TruckModelID),
foreign key(TruckMakeID) references TruckMake(TruckMakeID));
-- create Truck
create table Truck(
TruckVINNum varchar(4),
TruckMakeID char(3),
TruckModelID char(3),
TruckColour varchar(25),
TruckPurchaseDate varchar(40),
TruckCost varchar(25),
primary key (TruckVINNum),
foreign key (TruckMakeID,TruckModelID) references TruckModel(TruckMakeID,TruckModelID));
-- create Service
create table Service(
TransportID char(2),
TransportName varchar(20),
TransportCost varchar(20),
TransportMaxDist varchar(15),
primary key (TransportID));
-- create allocation
create table Allocation(
TruckVINNum varchar(4),
TransportID char(3),
FromDate varchar(25),
ToDate varchar(25),
primary key(TruckVINNum,TransportID),
foreign key(TruckVINNum) references Truck(TruckVINNum),
foreign key(TransportID) references Service(TransportID));
-- insert Truck Makes
Insert into TruckMake values ('TMI1','Mercedes');
Insert into TruckMake values ('TMI2','Volvo');
Insert into TruckMake values ('TMI3','Toyota');
Insert into TruckMake values ('TMI4','Subaru');
Insert into TruckMake values ('TMI5','Ford');
Insert into TruckMake values ('TMI6','Ferrari');
Insert into TruckMake values ('TMI7','Bugatti');
Insert into TruckMake values ('TMI8','Pagani');
Insert into TruckMake values ('TMI9','Volvo');
-- insert Truck Models
Insert into TruckModel values ('TMI1','MO1','MA12');
Insert into TruckModel values ('TMI2','MO2','LA17');
Insert into TruckModel values ('TMI3','MO3','LH21');
Insert into TruckModel values ('TMI4','MO4','MJ21');
Insert into TruckModel values ('TMI5','MO5','OY21');
Insert into TruckModel values ('TMI6','MO6','UI12');
Insert into TruckModel values ('TMI7','MO7','LH18');
Insert into TruckModel values ('TMI8','MO8','MH21');
Insert into TruckModel values ('TMI9','MO9','OH12');
-- insert Trucks
Insert into Truck values ('V023','TM1','MO1','Red','3rd of September, 2021','$350000');
Insert into Truck values ('JK01','TM2','MO2','Green','16th of June, 2020','$343582.56');
Insert into Truck values ('LY29','TM3','MO3','Green','12th of July, 2021','$342499.63');
Insert into Truck values ('AB43','TM4','MO4','White','24th of January,
2020','$360000.51');
Insert into Truck values ('LP21','TM5','MO5','White','12th of Feburary,
2021','$354000.25');
Insert into Truck values ('V022','TM6','MO6','Red','3rd of October, 2021','$345000');
Insert into Truck values ('JK04','TM7','MO7','Red','15th of December, 2020','$247522.36');
Insert into Truck values ('LY25','TM8','MO8','Black','20th of January,
2016','$348499.13');
Insert into Truck values ('AB33','TM9','MO9','Black','30th of January,
2019','$220410.31');
-- inset Service
Insert into Service values ('T1','RemovalServices','$7589','500');
Insert into Service values ('T2','Extractor','$6578','1100');
Insert into Service values ('T3','Movalist','$2200','700');
Insert into Service values ('T4','MovementServices','$6859','1220');
Insert into Service values ('T5','Transportalist','$4530','1150');
Insert into Service values ('T6','RemovalServices','$1597','500');
Insert into Service values ('T7','Movalist','$3000','1100');
Insert into Service values ('T8','Extractor','$2200','700');
Insert into Service values ('T9','Transportalist','$6859','1220');
-- insert Allocation
Insert into Allocation values ('V023','T1','1st of October,2020','3rd of October,2020');
Insert into Allocation values ('JK01','T2','2nd of January,2021','5th of January,2021');
Insert into Allocation values ('LY29','T3', '3rd of July,2020','6th of July,2020');
Insert into Allocation values ('AB43','T4','5th of June,2021','8th of June,2021');
Insert into Allocation values ('LP21','T5','7th of Janurary,2020','11th of
January,2020');
Insert into Allocation values ('V022','T6','10th of February,2021','14th of
February,2021');
Insert into Allocation values ('JK04','T7','15th of September,2020','18th of
September,2020');
Insert into Allocation values ('LY25','T8','17th of October,2021','21st of
October,2021');
Insert into Allocation values ('AB33','T9','18th of March,2020','20th of March,2020');
当您尝试向定义了外键的表中插入值时,您需要确保为外键列输入的特定值在父表中可用。
在您的示例中,当向TruckMake
和TruckModel
表中插入值时,对于TruckMakeID
列,您使用的是带有前缀TMI
的值。但是,当向Truck
表中插入值时,对于列TruckMakeID
,您使用的是前缀为TM
的值(缺少I
)。这就是错误的原因,因为前缀TM
后跟数字的值在TruckModel
表中不可用。
由于数据没有添加到Truck表中,因此在尝试向Allocation
表插入值时出现错误,因为TruckVINNum
表定义了一个引用Truck
表的外键。