I have an integer column which I would like to add a foreign key constraint to. Only problem is if that column does not have/need a value, by default MySQL puts in a ‘0’ value. This obviously breaks the foreign key constraint as there is no record in the primary table with a PK of 0.
How can I overcome this problem?
You may want to set your foreign key to accept
NULLvalues, and use aNULLinstead of a0value.Conceptually,
NULLmeans a missing unknown value. If your row does “not have/need the value”, I believe aNULLfits in perfectly.And yes, a
NULLvalue would not break your foreign key constraint.Let’s build a basic example:
Then: