How to restrict many to many table to one instance of a record in MYSQL
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Since user_id is the primary key of one table make it a foreign key in the second table with role_id the primary key. You can also make (user_id, role_id) an unique key.
Since user_id is the primary key of one table make it a foreign key in the second table with role_id the primary key. You can also make (user_id, role_id) an unique key.
What he said, the unique key part.
But you can make it a compound primary key (user_id, role_id) too.
You can also use REPLACE INTO instead of INSERT INTO, which enforces the single instance without throwing an error if you try to insert a duplicate.
*** Afterthought...
Any reason you do not want to make them into a compound primary key?
It costs nothing in this case in terms of additional indexing since they are unique by definition.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
You can make it a compound key. Check also UPDATE ... ON DUPLICATE KEY ...
For many-to-many relationships you can also use a so called resolver table. One table with users, one table with roles, and one intermediate table which holds (say) user_ids and role_ids. http://en.wikipedia.org/wiki/Junction_table.
@AnanthaP - yes 1 user would be able to have multiple roles so your description what I am attempting to do is correct.
@jlinkels - I didn't know UPDATE ... ON DUPLICATE KEY ... existed. I certainly will look into that. Also to your post, that is how I have the many to many relationship set up. I have a users table with all the user info and a roles table with all the role info then I have a roles_users table which has the id of both tables to link them together.
@astrogeek - no particular reason not to make this a compound key if that is the best solution. Mainly I am trying to get an idea of what my options are aside from what was suggested to me. From what i am gathering in this thread, using a compound primary key seems the best way to go with this.
I appreciate everyones input on my question. It looks like the way I will be going with this is looking at making a compound primary key and use that. I have never done a compound primary key before so I will be searching the the MYSQL docs to see what they have on the subject.
If you read down the entire discussion, it would seem to clarify all your doubts. The approach suggested in not to actually have a derived field for the compund/composite key but to declare the primary key as per the example below.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.