LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2015, 02:03 PM   #1
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Rep: Reputation: 31
How to restrict many to many table to one instance of a record in MYSQL


Hello all,

I have an app that I am building where I am creating a many to many table used to associate roles to a user.

user_id INT
role_id INT

Using MYSQL how would I go about making it so I could ensure that I have 1 instance of a record and not have duplicates.

user_id | role_id
1234 | 5678
1234 | 5678 # produces an error and doesn't get created

I am told that a possible solution is to set both columns to primary keys but I want to know if there is another way to do this that is better?

I am open to any suggestions.

Thanks,
 
Old 01-23-2015, 05:15 PM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
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.
 
Old 01-23-2015, 05:20 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by SoftSprocket View Post
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.

Last edited by astrogeek; 01-23-2015 at 05:25 PM.
 
Old 01-23-2015, 11:17 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Yes, it is clearly a composite primary key. (Most likely the OP didn't know that such thing is allowed.)
 
Old 01-24-2015, 05:24 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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.

jlinkels
 
Old 01-24-2015, 08:36 AM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
It all depends on what YOU want to do.

Eg:
Quote:
user_id | role_id
1234 | 5678
1234 | 5678 # produces an error and doesn't get created
what about
Quote:
user_id | role_id
1234 | 5678
1234 | 5678 # produces an error and doesn't get created
1234 | 5679 #This is OK or not. ie. 1 user having two roles.
I mean is the table a master record (1 row per user) or a history record

If OK, ie. 1 user having multiple roles, then compound key is OK.
 
Old 01-25-2015, 03:55 PM   #7
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Original Poster
Rep: Reputation: 31
Hi Guys,

Thanks for all the responses.

@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.
 
Old 01-27-2015, 02:58 AM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
http://stackoverflow.com/questions/5...ary-keys-mysql seems to contain what you need.

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.

CREATE TABLE INFO (
t1ID INT,
t2ID INT,
PRIMARY KEY (t1ID, t2ID)
)


OK
 
  


Reply

Tags
mysql 5.6



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the last record on a MySQL table? AQG Linux - Software 5 07-13-2005 12:55 PM
add/delete record in mysql table on linux from a WinXp machine using visual c++ SyncMaster Linux - Enterprise 2 05-30-2005 12:53 AM
Postfix: restrict delivery of mail to users in alias table noeffred Linux - Software 1 02-02-2005 04:05 PM
MySQL non-realtime table-by-table mirroring Passive Linux - Software 1 01-20-2004 12:11 PM
How to import MS ACCESS Table including OLE filed into the MySQL Table ? myunicom Linux - General 1 11-28-2003 11:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration