Login
You're viewing the code4lib.social public feed.
  • Nov 10, 2026, 10:24 AM

    Most people would consider the following two values to be equal:
    `(1, 2, 3)`
    `(1, 2, 3)`

    This might make you believe that the following values are equal too?
    `(1, NULL, 3)`
    `(1, NULL, 3)`

    FAIL! MySQL doesn't think so!

    > A UNIQUE index permits multiple NULL values for columns that can contain NULL.

    ... so I now have rows everyone else considers duplicate.

    #MySQL

    💬 2🔄 0⭐ 0

Replies

  • 💬 0🔄 0⭐ 1
  • Nov 10, 2026, 1:02 PM

    @svelmoe MySQL seem to have more footguns than other databases. Automatic conversions has bitten me more than once.

    💬 0🔄 0⭐ 0
  • Nov 10, 2026, 4:17 PM

    @simendsjo one way around it on mysql 8.0.13+: a functional key part, so NULL gets a stand-in value inside the index only:

    create unique index uq on t (a, (ifnull(b, 0)), c);

    the column keeps its NULLs, and the index treats them as equal. pick a stand-in that real data never uses. postgres 15 added NULLS NOT DISTINCT for exactly this case.

    💬 0🔄 0⭐ 0