I'm designing my table, but I have doubts: I will have two columns A
and B
that cannot have repetitions and, in the same row, can't both be null
.
I don't know if the best thing to do is to use a primary key with a combination of the values of A
and B
or a completely different column (for example an autoincremented column).
Best Answer
A
PRIMARY KEY
does not allowNULLs
. But aUNIQUE KEY
does. I suggest you try a simple table that hasA
andB
, each nullable, thenINSERT
suitable combinations to see if they work.UNIQUE(A,B)
says that the pair is unique, while allowing either column to be repeated. Your Question is vague--is that what you wanted?This will not check for "can't both be null"; that would have to be tested for at the application level.
Based on your Comment, you seem to be asking about the
WHERE
clause of aSELECT
, not the table definition. And it seems like neither column should allowNULLs
:(or swap the two keys)
Then the application should decide which of these to use:
depending on what is given in the
<form>
.