MySQL ON DUPLICATE KEY UPDATE with nullable column in unique key
I think something along the lines of (2) is really the best bet — or, at least, it would be if you were starting from scratch. In SQL, NULL means unknown. If you want some other meaning, you really ought to use a special value for that, and 0 is certainly an OK choice.
You should do this across the entire database, not just this one table. Then you shouldn't wind up with weird special cases. In fact, you should be able to get rid of a lot of your current ones (example: currently, if you want the summary row where there is no filter, you have the special case "filter is null" as opposed to the normal case "filter = ?".)
You should also go ahead and create a "not present" entry in the referred-to table as well, to keep the FK constraint valid (and avoid special cases).
PS: Tables w/o a primary key are not relational tables and should really be avoided.
edit 1
Hmmm, in that case, do you actually need the on duplicate key update? If you're doing a INSERT ... SELECT, then you probably do. But if your app is supplying the data, just do it by hand — do the update (mapping zip = null
to zip is null
), check how many rows were changed (MySQL returns this), if 0 do an insert.
With modern versions of MariaDB (formerly MySQL), upserts can be done simply with insert on duplicate key update statements if you go with surrogate column route #5. Adding MySQL's generated stored columns or MariaDB persistent virtual columns to apply the uniqueness constraint on the nullable fields indirectly keeps nonsense data out of the database in exchange for some bloat.
e.g.
CREATE TABLE IF NOT EXISTS bar ( id INT PRIMARY KEY AUTO_INCREMENT, datebin DATE NOT NULL, baz1_id INT DEFAULT NULL, vbaz1_id INT AS (COALESCE(baz1_id, -1)) STORED, baz2_id INT DEFAULT NULL, vbaz2_id INT AS (COALESCE(baz2_id, -1)) STORED, blam DOUBLE NOT NULL, UNIQUE(datebin, vbaz1_id, vbaz2_id) ); INSERT INTO bar (datebin, baz1_id, baz2_id, blam) VALUES ('2016-06-01', null, null, 777) ON DUPLICATE KEY UPDATE blam = VALUES(blam);
For MariaDB replace STORED with PERSISTENT, indexes require persistence.
MySQL Generated Columns MariaDB Virtual Columns