Question

I'm trying to create relationships in MySQL using forein keys. Everytime I successfully create the forien key, but when I describe the table, the key is considered "MUL". After inserting some records into the parent table, I get null values in the child. I've been researching this for hours and have come up empty handed. I even checked the innodb status and have no foreign key error reports. I'm not entirely sure why I'm getting null values, but I'm assuming its because of the "MUL" key value. Can someone confirm this and try and help me out?

create table employee
( id int
, first varchar(128)
, last varchar(128)
, primary key(id)
) engine=innodb; 

create table borrow
(ref auto_increment
, empID int
, book varchar(128)
, primary key(ref) 
) engine=innodb; 

alter table borrow add constraint fk_borrow
  foreign key (empID) references employee(id);

insert into borrow (empID, book) values (1,'mike');
Was it helpful?

Solution

The 'MUL' just means that it's not a unique index (i.e. duplicate values are allowed for the KEY). (If it were a unique index, the Key column would show 'UNI'). The FOREIGN KEY constraint doesn't have anything to do with the uniqueness of the foreign key column... it normally is non-unique... a parent can usually have zero, one or more children.

The FOREIGN KEY constraint does not disallow NULL values in the child table. Only a NOT NULL constraint (or a trigger) will do that for you. It's perfectly reasonable for a row in a child table to be an orphan, to not be related to a parent.

When you do the INSERT to the child table, you need to provide a non-NULL value for the foreign key column, if you want that row to reference a row in the parent table.


A foreign key can be defined with an ON DELETE SET NULL clause, but that would only be effective if you were to later delete a parent row that had children related to it. In that case, the child rows would have their foreign key column values set to NULL when the parent row is removed.


Mike said: I'm concerned that the child's foreign key is not getting populated by the parent.

The parent is not responsible for populating the foreign key column of the child. There's an option to have the child's foreign key value updated automatically when the parent's id value is changed... preserving the relationship: ON UPDATE CASCADE. But other than the actions performed by an ON UPDATE or ON DELETE clause, the parent has no responsibility of maintaining the values in a child table.

Mike asked: So how would I get the child's column to be populated from the parent?

You wouldn't. You would first locate (or insert) the row to the parent table. You would then preserve the value of the id column (or the values of whatever columns make up the PRIMARY KEY), and then use that same value for the foreign key column on the child row, when you insert a child row(s).

An error is returned when you set a foreign key to an arbitrary value (a value that does not match an existing PRIMARY KEY value in the parent table. That's the expected behavior.

If you are inserting the child row before the parent, you will need to leave the foreign key column as NULL, and then after you know the id value of the parent, you would then update the child row to set the foreign key column.


create table employee
( id int
, first varchar(128)
, last varchar(128)
, primary key(id) ) engine=innodb; 

create table borrow 
(ref auto_increment
, empID int
, book varchar(128)
, primary key(ref) ) engine=innodb;

alter table borrow add constraint fk_borrow
  foreign key (empID) references employee(id);

insert into employee (id, first, last) values (1, 'foo', 'bar');

insert into borrow (empID, book) values (1,'mike');

insert into borrow (empID, book) values (1,'mulligan');

The two rows added to the borrow table are related to the row in employee, by virtue of the value in the foreign key column (empID) being set to a value that matches an id value in the employee table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top