If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Interchangeable columns in primary key

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-05-11, 16:42
skimbels skimbels is offline
Registered User
 
Join Date: Apr 2011
Posts: 1
Interchangeable columns in primary key

I am designing a mySQL database.

I have the problem however whereby I have a table representing relationships, i.e. person1 and person2 are related. I only want one record representing this relationship however and I'm not sure how to define the primary key so that both records (person1-person2 and person2-person1) are not allowed in a single table, rather only one of the two records, as having both records is redundant.

Regards,

Stephen Adams
Reply With Quote
  #2 (permalink)  
Old 04-05-11, 17:48
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
There is no primary key variation that covers what you are looking to achieve, however, by using a simple technique during the insert by always adding the lower value in the first field and the higher value in the second field then a primary key on person1, person2 will work. The technique can be done programmatically through your programming language or by creating a stored procedure and using this as an API. Any time an insert needs to be performed on this table then use the stored procedure which includes the logic of placing the lower value first and the higher value second.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 04-05-11, 18:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by skimbels View Post
I only want one record representing this relationship however
the drawback to this is that any query looking for friends will require a UNION

if you were to add both rows -- person1,person2 and person2,person1 -- then your query would be a lot simpler and more efficient

it's a tradeoff, which i know i woulda solved one way back in the day when disk space was expensive (hunnerts of bucks per kilobyte), and which i would solve the other way in this day when a terabyte of space is only a couple bucks

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Tags
primary key, redundant records, relationships

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On