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

The Archive Base Latest Questions

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

How can I efficiently create a unique index on two fields in a table

  • 0

How can I efficiently create a unique index on two fields in a table like this: create table t (a integer, b integer);

where any unique combination of two different numbers cannot appear more than once on the same row in the table.

In order words if a row exists such that a=1 and b=2, another row cannot exist where a=2 and b=1 or a=1 and b=2. In other words two numbers cannot appear together more than once in any order.

I have no idea what such a constraint is called, hence the ‘two-sided unique index’ name in the title.

Update: If I have a composite key on columns (a,b), and a row (1,2) exists in the database, it is possible to insert another row (2,1) without an error. What I’m looking for is a way to prevent the same pair of numbers from being used more than once in any order…

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

    How about controlling what goes into the table so that you always store the smallest number into the first column and the largest one in the second? As long as it ‘means’ the same thing of course. It’s probably less expensive to do it before it even gets to the database.

    If this is impossible, you could save the fields as is but have them duplicated in numerical order into two OTHER fields, on which you would create the primary key (pseudo code-ish) :

    COLUMN A : 2 COLUMN B : 1  COLUMN A_PK : 1  ( if new.a < new.b then new.a else new.b ) COLUMN B_PK : 2  ( if new.b > new.a then new.b else new.a ) 

    This could easily be done with a trigger (as in Ronald’s response) or handled higher up, in the application.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Compile the function with edebug (C-u C-M-x instead of C-M-x),… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer Here you go buddy! SET @counter = 0; SET @category… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer Everything you need to try this out: public partial class… May 11, 2026 at 11:44 pm

Related Questions

What is the best way to deal with storing and indexing URL's in SQL
Possible Duplicate: How do I generate a hashcode from a byte array in c#
I'm writing an algorithm in PHP to solve a given Sudoku puzzle. I've set
I'm trying to get my head round this mind boggling stuff they call Database

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.