Jump to content

Nested UK's


SaranacLake

Recommended Posts

I have a junction table which has a column that needs to be unique. 

However, that field, along with two other fields - each which is the foreign key to another table - when combined together form yet another unique value.

So in MySQL, can I put a UK on the nested field, and then combine that with the other two fields and create an outer UK as well?

Thanks.

Link to comment
Share on other sites

8 hours ago, kicken said:

If that column by itself is unique, then any set of columns that includes it will automatically be unique, there's no real need for an extra key.

Good point!

But from a database performance standpoint, wouldn't there be a benefit of having the outer UK?

For example, if I use those 3 columns as a quasi-PK to join to another table, wouldn't MySQL be able to find things quicker if I had a second UK on those 3 columns?

 

Link to comment
Share on other sites

1 hour ago, SaranacLake said:

But from a database performance standpoint, wouldn't there be a benefit of having the outer UK?

Keep thinking. If the one column is unique then any search including that column will be able to find the corresponding row...

Link to comment
Share on other sites

1 minute ago, requinix said:

Keep thinking. If the one column is unique then any search including that column will be able to find the corresponding row...

This topic - as discussed in an earlier thread - is confusing from a language standpoint because a "UK" in MySQL is both a "constraint' and an "index".

From a constraint standpoint, if one column is unique, then combing it with two other columns would still guarantee unique values.

But my followup question as asking about performance, as in "indexes".

If you have three columns, column 1, column 2, and column 3.  And column 3 has a UK constraint/index on it, if you then used those three columns as a key to a child table, it is NOT obvious to me that MySQL would perform as well having just a constraint/index on column 3 versus having another constraint/index on column 1 + column 2 + column 3...

 

 

Link to comment
Share on other sites

2 minutes ago, SaranacLake said:

it is NOT obvious to me that MySQL would perform as well having just a constraint/index on column 3 versus having another constraint/index on column 1 + column 2 + column 3

If column 3 is unique across the entire table, then MySQL only has to search column 3 using it's unique index to find the one matching row on your join.  Column 1 and column 2 become essentially redundant and you could just exclude them entirely.

 

Link to comment
Share on other sites

18 minutes ago, kicken said:

If column 3 is unique across the entire table, then MySQL only has to search column 3 using it's unique index to find the one matching row on your join.  Column 1 and column 2 become essentially redundant and you could just exclude them entirely.

 

Okay.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.