mrc
mrc

Reputation: 3153

How to model an OR condition with two boolean fields in sql?

I am doing a database model where a product can have two different properties but one of these excludes the other one. For example, if property 1 is enabled for the product A, this product can not have property 2 enabled.

How can this condition be achieved? I have thought about using triggers to enable disable the boolean field of the other, but I do not know if there is an option to do this with data modeling.

The rdbms that I am using is postgres sql.

Thank you.

Upvotes: 1

Views: 97

Answers (1)

klin
klin

Reputation: 121594

Use check constraint, e.g.:

create table my_table(
    id integer primary key, 
    property1 boolean, 
    property2 boolean,
    check (not (property1 and property2))
    );

Upvotes: 3

Related Questions