Skip to content
Advertisement

Postgresql tuple constraints about NOT NULL

I am building a database in POSTGRESQL, and I would like to create NOT NULL constraints for my columns, where one and only one column would be NOT NULL.

I have two columns in my table, site_id and buffer_result_id. Only one of these columns will have values.

The above code is just some pseudo-code to show my idea. How can I achieve this function?

Advertisement

Answer

You could use XOR expressed as:

db<>fiddle demo

More info: Exclusive OR – Equivalences


Demo:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement