Have an interesting situation: I have a foreign key from a.name (child table) to b.name (parent table). b.name does not have a unique constraint on it, so it may contain duplicate values.
If I put the name ‘bob’ in b.name twice, and then put it in a.name, I can no longer delete or update either instance of ‘bob’ in table b. In both cases, it complains that by deleting/updating the row in table b, the FKey linking a.name to b.name will fail.
Now this makes some sense, but I want to be able to tell the FKey to only fail if there is no other instance ‘bob’ in table b. So if I have multiple instances of ‘bob’ in b.name, I can change/delete any except for the last one.
[Note that doing the obvious and adding a unique constraint on b.name won’t work because it will cause ‘undefined behavior’ in an application I didn’t write]
Any ideas?
Straight from http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
...Additionally, MySQL and InnoDB require that the referenced columns be indexed for performance. However, the system does not enforce a requirement that the referenced columns be UNIQUE or be declared NOT NULL.The handling of foreign key references to nonunique keys or keys that contain NULL values is not well defined for operations such as UPDATE or DELETE CASCADE. You are advised to use foreign keys that reference only UNIQUE and NOT NULL keys....So, keep your FKs tied to unique values or who’s to say what happens?