Skip navigation links

MySQL Forums :: InnoDB :: Cannot add or update a child row:a foreign key constraint fails


Advanced Search

Cannot add or update a child row:a foreign key constraint fails
Posted by: Momchil Ivanov ()
Date: June 12, 2010 02:19PM

hello,
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))
engine=InnoDB


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 :-))))

Options: ReplyQuote


Subject Views Written By Posted
Cannot add or update a child row:a foreign key constraint fails 2790 Momchil Ivanov 06/12/2010 02:19PM
Re: Cannot add or update a child row:a foreign key constraint fails 927 Rick James 06/13/2010 10:20PM
Re: Cannot add or update a child row:a foreign key constraint fails 934 Momchil Ivanov 06/16/2010 06:37AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.