PostgreSQL - best way to enforce a multicolumn constraint

Balinas used Ask the Experts™
I am designing a table in PostgreSQL - I have two columns that can not both be null.  Determining which can be null is based on the value of a third column.  Any suggestions?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Database Analyst
You should be able to do that easily on one, two, or more columns in a statement like:

ALTER TABLE customers ADD CONSTRAINT not_null_text CHECK (server_ip <> NULL AND client_ip <> NULL);
--ALTER TABLE customers DROP CONSTRAINT not_null_text
BalinasSr Data Architect


Thanks- That will allow me to make sure that both are not null which ultimately is what I need to constrain in the table, but is there a way to look at a third field and given your example when it is Y allow server_ip to be null but not client_ip and when it is X client_ip can be null but server_id can't be null

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial