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

  • SEARCH
  • Home
  • 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 7495209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:59:57+00:00 2026-05-29T17:59:57+00:00

I want to create a MySQL table to hold relationship data between users. The

  • 0

I want to create a MySQL table to hold relationship data between users. The relationship between user A to B and user B to A can be different.

Example:

  1. Relationship of (from) Bob (to) Alice: 0.9 – Bob loves Alice’s stuff.
  2. Relationship of (from) Alice (to) Bob: 0.5 – Alice finds Bob’s stuff mediocre.

My question:

I have implemented two CONSTRAINTS as UNIQUE KEYs on two FOREIGN KEYS that reference user_ids in a users table. Can I do this? Are they treated as two separate UNIQUE KEYs?

How can I implement a CONSTRAINT that will only allow one occurrence of each (from) UserA (to) UserB relationship and (from) UserB (to) UserA relationship per user_id? Am I going about it the right way?

The SQL:

CREATE TABLE relationships (
  relationship_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
  from_user_id MEDIUMINT UNSIGNED NOT NULL,
  to_user_id MEDIUMINT UNSIGNED NOT NULL,
  relationship_level DECIMAL(1,1) NOT NULL,
  PRIMARY KEY (relationship_id),

  FOREIGN KEY (from_user_id) REFERENCES users (user_id)
    ON DELETE CASCADE ON UPDATE NO ACTION,

  FOREIGN KEY (to_user_id) REFERENCES users (user_id)
    ON DELETE CASCADE ON UPDATE NO ACTION,

  CONSTRAINT from_to_relationship UNIQUE KEY (from_user_id, to_user_id),
  CONSTRAINT to_from_relationship UNIQUE KEY (to_user_id, from_user_id),

  INDEX relationship_from_to (relationship_id, from_user_id, to_user_id, relationship_level),
  INDEX relationship_to_from (relationship_id, to_user_id, from_user_id, relationship_level)

) ENGINE=INNODB;

I hope someone can assist.

  • 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-29T17:59:57+00:00Added an answer on May 29, 2026 at 5:59 pm

    Keep only one of these UNIQUE constraints – there is no need to have both. When a row fails UNIQUE KEY (from_user_id, to_user_id) it also fails UNIQUE KEY (to_user_id, from_user_id) and vice versa, so they are logically equivalent. Even with only one UNIQUE constraint, when trying to represent a relationship between Alice and Bob, you can have at most one {Alice, Bob} row, and at most one {Bob, Alice} row.

    As for performance (i.e. traversing the relationship in both directions), you might want to consider indexing {from_user_id, to_user_id} (for “forward” traversal) and/or {to_user_id, from_user_id} (for “backward” traversal). You might even ditch the surrogate primary key (relationship_id) and go for the natural PK, thus lowering the need for indexing (secondary indexes are expensive for clustered tables, see Understanding InnoDB clustered indexes, section “Disadvantages of clustering”).

    In my opinion, your table should look like this:

    CREATE TABLE relationships (
        from_user_id MEDIUMINT UNSIGNED NOT NULL,
        to_user_id MEDIUMINT UNSIGNED NOT NULL,
        relationship_level DECIMAL(1,1) NOT NULL,
        PRIMARY KEY (from_user_id, to_user_id), -- InnoDB is clustered, so naturally "covers" relationship_level as well.
        FOREIGN KEY (from_user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE NO ACTION,
        FOREIGN KEY (to_user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE NO ACTION,
        INDEX relationship_to_from (to_user_id, from_user_id, relationship_level) -- Including relationship_level may or may not be a good idea.
    ) ENGINE=INNODB;
    

    NOTE: Whether you include relationship_level in INDEX or not depends on whether you want index-only scan in “backward” direction as well. The “forward” direction is naturally covered by the PK (since InnoDB is clustered).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we create a new MySQL database( not a table, I want a new
i want to create tabs according to data from MySQL database using php for
I have a basic users table I want to create in MySQL. I do
I want to create a new column in a MYSQL table that has Fn
I want to implement the following constraints in mysql: create table TypeMapping( ... constraint
I wonder if there is a (native) possibility to create a MySQL table from
QUESTION How can I create a MySQL table so, that a field's default value
I have a MySQL table and I want to pick certain columns to create
I want to create a class which uses PDO to interact with MySQL. Can
My MySQL table contains 2 fields and I want to create a new parameter

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.