Question

I've been wondering this for a while now. When you use MySQL Workbench to create a n:m relation between two tables (let's say A and B) the resulting table (let's say A_B) has three indexes (PRIMARY, one on A's primary key, one on B's primary key).

According to this question PRIMARY KEY is always indexed by itself, so why would there be the need to index each singular field again with a singular index ? Also if this is MySQL's policy, is it different with other DBMSs ?

Was it helpful?

Solution

it's a good rule of thumb in relational databases to have indexes on the columns which are involved in a foreign key relationship as most often your sql queries will join tables based on these columns.

my guess is that workbench follows that rule of thumb but is not smart enough to see that an index already exists in those cases where one of the columns is a Pk.

so it just adds one index per column and does not check if one of the columns is also a primary key. (the pk column does not have to be a part of a foreign key relationship)

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