473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Can anyone tell me what i'm doing wrong here?I'm trying to set up a one-to-one relationship between two tables, but it just doesn't work.These are my tables:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE prices
  2. (
  3.     id INT AUTO_INCREMENT NOT NULL PRIMARY KEY ,
  4.     price FLOAT NOT NULL
  5.  
  6. ) ENGINE=INNODB ;
  7. CREATE TABLE films
  8. (
  9.     film_id INT AUTO_INCREMENT NOT NULL PRIMARY KEY ,
  10.     name VARCHAR(30) NOT NULL,
  11.     FOREIGN KEY (film_id) REFERENCES  prices (id)
  12.  
  13.  
  14.     ) ENGINE=INNODB ;
  15.  
And this is the insert statement i'm using to populate them(I'm getting the following error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`video_library`.`films`, CONSTRAINT `films_ibfk_5` FOREIGN KEY (`film_id`) RE
FERENCES `prices` (`id`)))
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO prices (price)
  2. VALUES  (1.99),
  3.         (2.99),
  4.         (3.99),        
  5.         (4.99),
  6.         (5.99),
  7.         (6.99),
  8.         (7.99),
  9.         (8.99),
  10.         (9.99),
  11.         (10.99),
  12.         (11.99),
  13.         (12.99),
  14.         (13.99);
  15.  
  16. INSERT INTO films (name)        
  17. VALUES  ('The Godfather'),
  18.         ('The Godfather II'),
  19.         ('The Shawshank redemption'),
  20.         ('Pulp fiction'),
  21.         ('12 Angry men'),
  22.         ('Schindler\'s List'),
  23.         ('Fight club'),
  24.         ('Inception'),
  25.         ('The green mile'),
  26.         ('Dog Day Afternoon'),
  27.         ('The Dark Knight'),
  28.         ('Saving Private Ryan'),
  29.         ('Se7en');
  30.  
Thanks in advance
Mar 26 '12 #1
2 4095
johny10151981
1,059 1GB
Really? Godfather is cheaper than Godfatehr II?

you need to rethink the price ;)

anyway:

pointer number one this is not a very smart way to design your database. do two things
1. Remove Auto Increment constraints from the second database.
2 and insert all the the data in table two with proper id taken from table on1
Mar 30 '12 #2
Thanks for the help.
Apr 2 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ralph Smith | last post by:
I'm having trouble copying a database to another machine. Here are the two table's in ths database and the sql commands: DROP TABLE IF EXISTS `clients`; CREATE TABLE `clients` ( `client_id`...
1
by: jijo kuruvila | last post by:
actually my code looks like this private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { DataGrid1.Columns.Visible=true;...
4
by: Frank | last post by:
Hello, I have a dataset with 2 tables and a relation (parent - child). I linked that ds to a datagrid. Shows everything fine (very little coding for such functionality!). But.. in the child row I...
3
by: fuimens | last post by:
Hi, With mysql-4.0.20, I have a problem inserting data with foreign key references, MySQL saying ERROR 1216: Cannot add or update a child row: a foreign key constraint fails The message is...
0
by: Zorglub | last post by:
Hi, I have a windos form with two datagrids Master and Detail, I use a dataset with two datatable and a relation between Table1 and Table2. I set the datagrid like : DataViewManager dsView =...
3
by: Ivan | last post by:
Thanks to answer topic previous!!!! I use this script to update one table MERGE INTO EIS.CLI_HISTORY TXN USING EIS.CLI_MAPING CLI ON TXN.CLIENTID = CLI.CLIENTID WHEN MATCHED AND...
4
by: Harshpandya | last post by:
Hi I am new at ASP and MYSQL. I am Using Navicat to access MYSQL database. My Problem is when i create the foreign key in "Navicat" -it gives me this error. 1452-cannot add or update child...
1
by: nitinkhare1 | last post by:
Please help,I'm very frustated. when I ADD the data in kn_history_question table of htbase_tst database....it giving the error "Cannot add or update a child row:a foreign key constraint...
0
muaddubby
by: muaddubby | last post by:
Hi I've got a situation with a dataset that contains two tables with a many-to-one relationship, and am trying to extract the data in xml format. When I execute the GetXml() method, and exception...
6
semanticnotion
by: semanticnotion | last post by:
Hi sir i want to transform the data of one table into another through foreign key but the following error come to my browser Here is my code and data base structure. CREATE TABLE IF NOT...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.