I was looking in this page but also on stackoverflow and google because I though this may be a quite common question but could not find anything related… I may not be using the right terminology here.
The question is quite straight forward.
Is it a good practice having nested unique keys (or mix of primary and unique keys) inside another unique key?
example:
I have a table with columns A, B, C, D, E, F and G
Primary or UNIQUE Key would be (A, B) and UNIQUE KEYS (A, B, C) and (A, B, D).
I would like to know whether or not this is kind of things are a good practice in general but also for mysql.
Thank you very much
Best Answer
PRIMARY KEY
. In MySQL this implies two things: aUNIQUEness
constraint, and an index.UNIQUE
keys in a single table. (Remember:PRIMARY
counts asUNIQUE
.INT
.(a)
, it is unreasonable and unnecessary to have also haveUNIQUE(a,b)
orUNIQUE(b,a)
. On the other hand, it may be useful to have a non-uniqueINDEX(a,b)
and/orINDEX(b,a)
. Think how the uniqueness of(a)
implies the uniqueness of the other two.INDEX(a,b)
, there is virtually no reason to also haveINDEX(a)
(Note: I am not talking aboutUNIQUE
in this bullet item.)If I have not addressed your specific question (perhaps indirectly), please rephrase your question.