Either OR non-null constraints in MySQL

前端 未结 6 1520
忘了有多久
忘了有多久 2021-02-05 23:06

What\'s the best way to create a non-NULL constraint in MySQL such that fieldA and fieldB can\'t both be NULL. I don\'t care if either one is NULL by itself, just as long as the

6条回答
  •  一整个雨季
    2021-02-05 23:15

    This isn't an answer directly to your question, but some additional information.

    When dealing with multiple columns and checking if all are null or one is not null, I typically use COALESCE() - it's brief, readable and easily maintainable if the list grows:

    COALESCE(a, b, c, d) IS NULL -- True if all are NULL
    
    COALESCE(a, b, c, d) IS NOT NULL -- True if any one is not null
    

    This can be used in your trigger.

提交回复
热议问题