Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Explain foreign key constraints in MySQL

Posted on 2013-12-23
6
Medium Priority
?
804 Views
Last Modified: 2013-12-24
I have previously always built databases by hand, but recently discovered the MySQL Workbench, which is a handy little tool.

Unfortunately, it is adding foreign key constraints to my database relationships, which is something I have never used before.

I am getting this error on an INSERT command:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`bugreport`.`projects_has_workers`, CONSTRAINT `fk_projects_has_workers_workers1` FOREIGN KEY (`workers_workers_id`) REFERENCES `workers` (`workers_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

Can someone explain to me what this means? I understand that the database is upset that a foreign key is reference, and it probably thinks that something doesn't exist where it should, but I just don't quite understand it.

Thanks!
0
Comment
Question by:DrDamnit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 32

Author Comment

by:DrDamnit
ID: 39737254
I think I figured it out. It's mad because there is nothing in the workers table, and a record in the workers table must exists prior to my being able to build a relationship between the project table and the workers table. Right?
0
 
LVL 9

Expert Comment

by:gt2847c
ID: 39737277
The foreign key constraint requires the existing of a matching value in the referenced table before it will allow a row to be inserted.  Just guessing based on how the error reads, it may be that the constraint is not doing what you meant it to do.  The constraint is complaining that there is no worker_id in the workers table matching the row you are attempting to insert.  It looks like you're attempting to make sure that a project has workers, but that's not quite what the constraint is attempting to enforce.

I could be reading a whole lot more into that error message than what you meant, so I may have misinterpreted it...
0
 
LVL 32

Author Comment

by:DrDamnit
ID: 39737370
I am pretty sure that it was complaining because the workers table was completely empty, and the ID I was attempting to pass was not a workers.workers_id value, but (in fact) was a users.users_id value.

Would that throw the error?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Accepted Solution

by:
gt2847c earned 2000 total points
ID: 39737427
The constraint mentioned in the error you posted lists the worker_id from the workers table.  An empty workers table would prevent you from inserting any records in the table where your constraint was applied.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39737434
Please, how have you created the table? I'm use MySqlWorkbench somtimes but it never created nothing if I didn't tell it to do :).

When you create a new table you have to specify columns names and properties. Below the blank background screen where you set columns properties there are 6 tabs:

1. Columns
2. Indexes
3. Foreign Keys
4. Triggers
5. Partitioning
6. Options

If you press F1 where one of these tabs is selected (or when a control within each tab is selected) yopu'll get the built-in help which will explain all you need to know. But the imnportant thing is that if you don't set manually a foreign key, the program doesn't create one for you, so you have to think about how you have created your tables because you can simply avoid the foreign key if you don't need it.

Best
0
 
LVL 32

Author Comment

by:DrDamnit
ID: 39737482
@marqusG:

I created the table using the database diagram editor. To create a one-to-one relationship, I press '1' on the keyboard and connect the tables. To create a projects_has_workers table, I used the 5 key. It is creating the constraints automatically.

Here is the table definition as created by workbench:
CREATE TABLE `workers_has_skilltags` (
  `workers_workers_id` int(11) NOT NULL,
  `skilltags_skilltags_id` int(11) NOT NULL,
  PRIMARY KEY (`workers_workers_id`,`skilltags_skilltags_id`),
  KEY `fk_workers_has_skilltags_skilltags1_idx` (`skilltags_skilltags_id`),
  KEY `fk_workers_has_skilltags_workers1_idx` (`workers_workers_id`),
  CONSTRAINT `fk_workers_has_skilltags_skilltags1` FOREIGN KEY (`skilltags_skilltags_id`) REFERENCES `skilltags` (`skilltags_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_workers_has_skilltags_workers1` FOREIGN KEY (`workers_workers_id`) REFERENCES `workers` (`workers_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Open in new window


I have just confirmed that the problem was user error on my part - once I started adding values to the tables, the errors go away!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In this article, we’ll look at how to deploy ProxySQL.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question