We help IT Professionals succeed at work.

Storing common value for various id pairs in mysql

Rohit Bajaj
Rohit Bajaj asked
Last Modified: 2017-05-12
This is a general question although i need to do it in Mysql but can be considered for any way of storage..
Say i have multiple ids  id1, id2, id3,id4...
And for each pair there is a corresponding value or some secret value which needs to be stored in a table...
The values are symmetric ie. the values for (id1, id2) is same as (id2, id1).

Please suggest the table schema or any way to store it...One way obviously is that i have a table with three columns...
id, id, value
and store things like :
id1, id2, val1
id2, id1, val1
id1, id3, val3
id3, id1, val3

The issue i see with this is that duplicate info is being stored in the table...

Also the ids are strings and vals are also strings..
Other way i can think of is of two columsn
if id1 < id2 then i store
id1 + " " + id2  val1

ie. i concatenate them and keep the first one which is smallest ..

Please suggest alternate approaches and pros and cons of above...
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)


yes that seems a good approach...
And what about when i get new value for some id pair..
But now wether it will be an update or an insert depends on whether there is a row with the id pair...otherwise need to insert...

will that cause any issue


will it be like two queries if the id pair are new..
because i will first try an update query and that will return 0 rows and so i will run another insert query ?

is there a way to avoid two queries here ?

MySQL can do that in one step with insert...on duplicate update:

You will want to always insert in a repeatable order.  E.g. Make sure id1 < id2 in all cases.  Otherwise you could insert the same pair twice in two different orders and that would be bad.


Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.