Standard use of 'Z' instead of NULL to represent missing data?

前端 未结 8 565
借酒劲吻你
借酒劲吻你 2020-12-12 20:08

Outside of the argument of whether or not NULLs should ever be used: I am responsible for an existing database that uses NULL to mean "missing or never entered" da

8条回答
  •  伪装坚强ぢ
    2020-12-12 20:59

    This is easily one of the weirdest opinions I've ever heard. Using a magic value to represent "no data" rather than NULL means that every piece of code that you have will have to post-process the results to account/discard the "no-data"/"Z" values.

    NULL is special because of the way that the database handles it in queries. For instance, take these two simple queries:

    select * from mytable where name = 'bob';
    select * from mytable where name != 'bob';
    

    If name is ever NULL, it obviously won't show up in the first query's results. More importantly, neither will it show up in the second queries results. NULL doesn't match anything other than an explicit search for NULL, as in:

    select * from mytable where name is NULL;
    

    And what happens when the data could have Z as a valid value? Let's say you're storing someone's middle initial? Would Zachary Z Zonkas be lumped in with those people with no middle initial? Or would your contractor come up with yet another magic value to handle this?

    Avoid magic values that require you to implement database features in code that the database is already fully capable of handling. This is a solved and well understood problem, and it may just be that your contractor never really grokked the notion of NULL and therefore avoids using it.

提交回复
热议问题