D
D
Denis Kuznetsov2019-04-18 16:46:10
PostgreSQL
Denis Kuznetsov, 2019-04-18 16:46:10

How to check fields to ensure that only one of them is not empty?

I have fields in the
PSRN table and a passport number, I need to impose a condition on them so that when filling in the data, one of them is filled in or not, if two or none are filled, the system would give an error, can I do this and if so, how ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2019-04-18
@DennisKingsman

You need a check constraint .
There seems to be no XOR in postgresql, so show on your fingers:

alter table tablename add constraint tablename_p1_xor_p2 check((p1 is null and p2 is not null) or (p2 is null and p1 is not null));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question