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 381177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:03:37+00:00 2026-05-12T15:03:37+00:00

Okay i know i can do this in the application layer, which is probably

  • 0

Okay i know i can do this in the application layer, which is probably the easiest thing to do, but just to make sure that no errors water down to the DB, i have a serious question

I have two columns X and Y, each to store two integers (either A or B in any of the columns). Is it possible to have a unique index constraint such that, in no instance should we have

  1. Column X with A and Column Y with B
  2. Column X with B and Column Y with A

I’ll give a scenario

I have two users, userA has id 678498 and userB has id 679879. Both users are about to play a 2 player game which requires that a new record for this session be stored in a table (tbl_chalenger). To do so, i have a table with columns “host” and “challenger”.

I have a unique constrain added to tbl_challenger as

UNIQUE KEY `UNIQUE_PARTICIPANTS` (`host`,`challenger`)

Branding users a host or challenger is basically dependent on who initiated the game. So if userA initiates the game we have an query as follows

INSERT INTO `tbl_challenger` VALUES(678498 , 679879); 

which creates a new record, Sadly though, if at the same time userB attempts to initiate a game with user A, we get

INSERT INTO `tbl_challenger` VALUES(679879, 678498 ); 

Which creates a new unwanted row, of the same participants. This irrespective of the UNIQUE key constraint.

So my question is how to have a constraint that is bidirectional?, such that “host-challenger” as well as “challenger-host” cannot have the same data pair

  • 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-12T15:03:37+00:00Added an answer on May 12, 2026 at 3:03 pm

    In mysql the only way I can think of is to add a couple of utility columns like

    CREATE TABLE tbl_challenger (
      host int,
      challenger int,
      u0 int, u1 int
    );
    

    and add a couple of triggers that set u0 and u1 to the least and greatest of the two:

    CREATE TRIGGER uinsert BEFORE INSERT ON tbl_challenger
     FOR EACH ROW SET NEW.u0 = LEAST(NEW.host,NEW.challenger),
      NEW.u1 = GREATEST(NEW.host,NEW.challenger);
    CREATE TRIGGER uupdate BEFORE UPDATE ON tbl_challenger
     FOR EACH ROW SET NEW.u0 = LEAST(NEW.host,NEW.challenger),
      NEW.u1 = GREATEST(NEW.host,NEW.challenger);
    

    then you add a unique index on (u0,u1)

    CREATE UNIQUE INDEX uniqueness ON tbl_challenger(u0,u1);
    

    And now you will get an error trying to insert duplicate pair regardless of the order.

    On a decent RDBMS like PostgreSQL you would be able to use index on expression:

    CREATE UNIQUE INDEX uniqueness ON tbl_challenger
        ( LEAST(host,challenger), GREATEST( host,challenger) );
    

    So, switch before it’s too late 😉

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

Sidebar

Ask A Question

Stats

  • Questions 203k
  • Answers 203k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use CSV format that is readable for MS… May 12, 2026 at 8:36 pm
  • Editorial Team
    Editorial Team added an answer Update: I've looked back at my notes when I first… May 12, 2026 at 8:36 pm
  • Editorial Team
    Editorial Team added an answer Maybe you need to add the option .matchWholeWord = True… May 12, 2026 at 8:36 pm

Related Questions

I have a very simple problem. I have an application which is written in
Okay, I have this program and I don't want more than one instance of
Okay so i have a semi weridish problem with re.sub. Take the following code:
Okay, I get it. Data in PostgreSQL is case-sensitive. And I know I can
I am currently trying to implement an algorithm to select a unique (16-bit) identifier.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.