Cannot add or update a child row:a foreign key constraint fails
Posted by: Momchil Ivanov
Date: June 12, 2010 02:19PM
I am a beginner in MySQL just started studding it 3 weeks ago and I am trying to insert data from ".csv" file and when I run the command is giving me error message. I thing I have a problem with creating the table.
That is the script for my table creation:
create table Bookings (
booking_id integer not null auto_increment,
customer_id integer not null,
car_id integer not null,
start_date date not null,
end_date date not null,
constraint primary key (booking_id),
constraint foreign key (customer_id) references Customers(customer_id),
constraint foreign key (car_id) references Cars(car_id))
I am not sure but from that i have been reading and learning in my class I decide that
"(customer_id) references Customers(customer_id)," have to be foreign key because I have it like PRIMARY KEY in CUSTOMER table and " constraint foreign key (car_id) references Cars(car_id)" have to be foreign key because I have it like PRIMARY KEYY in CARS.
When I run the command in MySQL is creating the table without showing any error and after that I am trying to run the
LOAD DATA LOCAL INFILE 'C:/Documents and Settings/Administrator/Desktop/CarHireData/carhire_bookings.csv'
INTO TABLE Bookings
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(booking_id, customer_id, car_id, start_date, end_date);
and here is giving me error message :
Script line: 1 Cannot add or update a child row: a foreign key constraint fails (`u5501941fma`.`Bookings`, CONSTRAINT `Bookings_ibfk_2` FOREIGN KEY (`car_id`) REFERENCES `Cars` (`car_id`))
If you can help it is going to be great
THANK YOU in advance :-))))