Question

I have a table like this:

something
a [INT]
b [INT]
c [INT]

...where a, b and c are separate Foreign Keys pointing to three different table.id. Since I want to make all regs be unique, and after having read this great answer, I think I should create a new Index this way: UNIQUE INDEX(a, b, c) and (in my case) do IGNORE INSERTS.

But as you can see, I would have one KEY for each column and then another extra UNIQUE INDEX containing all three. Is this a normal thing? It seems strange to me, and I have never seen it.

Was it helpful?

Solution

It is perfectly normal and reasonable to include a column in more than one index. However, if the combination of (a, b, c) is enough to uniquely identify a row it seems that you want a PRIMARY index instead of a UNIQUE one here (technically there is very little difference, but semantically it might be the better choice).

OTHER TIPS

Creating a Primary Key if Something (a, b, c) will invalidate the need for a unique index. An additional Unique index would make sense if your primary key was Something(a, b) and you wanted a Unique Index (a, b, c). But since all three columns are Foreignkey then a Primary key index is what you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top