Link to home
Start Free TrialLog in
Avatar of DrDamnit
DrDamnitFlag for United States of America

asked on

Explain foreign key constraints in MySQL

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!
Avatar of DrDamnit
DrDamnit
Flag of United States of America image

ASKER

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?
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...
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?
ASKER CERTIFIED SOLUTION
Avatar of gt2847c
gt2847c
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
@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!