I have a table like the following:
create table my_table (
id int8 not null,
id_A int8 not null,
id_B int8 not null,
id_C int8 null,
constraint pk_my_table primary key (id),
constraint u_constrainte unique (id_A, id_B, id_C)
);
And I want (id_A, id_B, id_C) to be distinct in any situation. So the following two inserts must result in an error:
INSERT INTO my_table VALUES (1, 1, 2, NULL);
INSERT INTO my_table VALUES (2, 1, 2, NULL);
But it doesn't behave as expected because according to the documentation, two NULL values are not compared to each other, so both inserts pass without error.
How can I guarantee my unique constraint even if id_C can be NULL in this case?
Actually, the real question is: can I guarantee this kind of uniqueness in "pure sql" or do I have to implement it on a higher level (java in my case)?

INSERT INTO my_table VALUES (NULL, NULL, 1, 2)will return Error becauseid_Acannot be Null. If you meanINSERT INTO my_table VALUES (NULL, 1, 2, NULL), yes, you are right, both Inserts will pass fine. – ypercube Dec 27 '11 at 9:14NULLstand for in columnid_C? Will you later be changing theseNULLs into actual values? – ypercube Dec 27 '11 at 9:17(1,2,1)and(1,2,2)in the(A,B,C)columns. Should a(1,2,NULL)be allowed to be added or not? – ypercube Dec 27 '11 at 9:27