Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1039301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:04:58+00:00 2026-05-16T15:04:58+00:00

Been reading the tutorial How to handle a Many-to-Many relationship with PHP and MySQL

  • 0

Been reading the tutorial How to handle a Many-to-Many relationship with PHP and MySQL .

In this question I refer to the “Database schema” section which states the following rules:

This new table must be constructed to
allow the following:

* It must have a column which links back to table 'A'.
* It must have a column which links back to table 'B'.
* It must allow no more than one row to exist for any combination of rows from table 'A' and table 'B'.
* It must have a primary key.

Now it’s crystal clear so far.

The only problem I’m having is with the 3rd rule (“It must allow no more than one row to exist for any combination“).
I want this to be applied as well, but it doesn’t seem to work this way.

On my test instance of mysql (5.XX) I’m able to add two rows which reflect the same relationship!

For example, if I make this relation (by adding a row):
A to B
It also allows me to make this relation as well:
B to A

So the question is two questions actually:

1) How do I enfore the 3rd rule which will not allow to do the above? Have only one unique relation regardless of the combination.

2) When I’ll want to search for all the relations of ‘A’, how would the SQL query look like?

Note #1: Basically my final goal is to create a “friendship” system, and as far as I understand the solution is a many-to-many table. Suggest otherwise if possible.

Note #2: The users table is on a different database from the relations (call it friendships) table. Therefore I cannot use foreign keys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:04:58+00:00Added an answer on May 16, 2026 at 3:04 pm

    For the first question:

    1. Create a unique constraint on both
      columns
    2. Make sure you always sort the columns. So if your table has the
      colummns a and b than make sure
      that a is less than or equal to
      b

    For the second question:

    SELECT
      *
    FROM
      many_to_many_table
    WHERE
      a = A or b = A
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been reading many a tutorial/article on unmanaged DLLs in C++. For the life
I've been reading a little about temporary tables in MySQL but I'm an admitted
I have been reading an interesting statement in http://download.oracle.com/javase/tutorial/jdbc/basics/transactions.html The interesting part is: Catching
I've been reading some tutorials on how to get started using Rails 2.0. (Time
I've been reading through a few tutorials about css, and I saw two different
I have been reading through the C++ FAQ and was curious about the friend
I've been reading through a lot of the rookie Java questions on finalize() and
I've been reading about the new developer-only RC0 for Silverlight, and the fact that
I've been reading a lot about closures and I think I understand them, but
I've been reading that Adobe has made crossdomain.xml stricter in flash 9-10 and I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.