Either OR non-null constraints in MySQL

前端 未结 6 1533
忘了有多久
忘了有多久 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:25

    I've done something similar in SQL Server, I'm not sure if it will work directly in MySQL, but:

    ALTER TABLE tableName ADD CONSTRAINT constraintName CHECK ( (fieldA IS NOT NULL) OR (fieldB IS NOT NULL) );
    

    At least I believe that's the syntax.

    However, keep in mind that you cannot create check constraints across tables, you can only check the columns within one table.

提交回复
热议问题