SaranacLake Posted May 16, 2020 Share Posted May 16, 2020 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. Quote Link to comment Share on other sites More sharing options...
kicken Posted May 16, 2020 Share Posted May 16, 2020 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. Quote Link to comment Share on other sites More sharing options...
SaranacLake Posted May 16, 2020 Author Share Posted May 16, 2020 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted May 16, 2020 Share Posted May 16, 2020 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... Quote Link to comment Share on other sites More sharing options...
SaranacLake Posted May 16, 2020 Author Share Posted May 16, 2020 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... Quote Link to comment Share on other sites More sharing options...
kicken Posted May 16, 2020 Share Posted May 16, 2020 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. Quote Link to comment Share on other sites More sharing options...
SaranacLake Posted May 16, 2020 Author Share Posted May 16, 2020 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.