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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:19:24+00:00 2026-06-07T09:19:24+00:00

I have the following table definition: CREATE TABLE [Car] ( CarID int NOT NULL

  • 0

I have the following table definition:

CREATE TABLE [Car] 
(
   CarID int NOT NULL PRIMARY KEY IDENTITY(1,1),
   FirstColorID int FOREIGN KEY REFERENCES Colors(ColorID),
   SecondColorID int FOREIGN KEY REFERENCES Colors(ColorID),

   UNIQUE(FirstColorID, SecondColorID)
)

I want the two Color columns to be unique, regardless of the combination they appear in.

E.g. attemping:

INSERT INTO Car (FirstColorID, SecondColorID) VALUES (1, 2); --should succeed

but then trying the same after that first record exists with the colors reversed should fail:

INSERT INTO Car (FirstColorID, SecondColorID) VALUES (2, 1); --should violate constraint/check

I have this problem solved using high-level code, but I’d rather the constraint be enforced directly in the database, preferably without involving things like triggers.

Thanks.

  • 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-06-07T09:19:26+00:00Added an answer on June 7, 2026 at 9:19 am

    You could maybe create computed column, something like:

    CREATE TABLE [Car] 
    (
       CarID int NOT NULL PRIMARY KEY IDENTITY(1,1),
       FirstColorID int, --FOREIGN KEY REFERENCES Colors(ColorID),
       SecondColorID int, --FOREIGN KEY REFERENCES Colors(ColorID),
       xColor As Cast(Case When FirstColorID > SecondColorID Then FirstColorID Else SecondColorID End as varChar) + ',' + 
            Cast(Case When FirstColorID <= SecondColorID Then SecondColorID Else FirstColorID End as varChar),
       UNIQUE(xColor)
    )
    

    UPDATE (You should test it before, i did just very quick testing)

    Idea:

    Integer is 4 byts. If i put 2 integers together- i get 8 bytes. If i order them- i get unique BigInt value (8 bytes).

    So what i do:

    1. I make sure they are in correct order
    2. I shift bytes of 32 bits to left (so just by multiplying 4294967296 i get what i want)
    3. I make logical OR- so i get 8 byte BigInt value, that should always be unique!

    So:

    CREATE TABLE [Car] 
    (
       CarID int NOT NULL PRIMARY KEY IDENTITY(1,1),
       FirstColorID int, --FOREIGN KEY REFERENCES Colors(ColorID),
       SecondColorID int, --FOREIGN KEY REFERENCES Colors(ColorID),
       xColor As 
           Case When FirstColorID > SecondColorID Then 
                Cast(FirstColorID as BigInt) * Cast(4294967296 as BigInt) | Cast(SecondColorID as BigInt)
            Else 
                Cast(SecondColorID as BigInt) * Cast(4294967296 as BigInt) |  Cast(FirstColorID as BigInt)
            End
      UNIQUE(xColor)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table definition: CREATE TABLE X ( A SOMETYPE NOT NULL,
Suppose I have the following table definition: CREATE TABLE x (i serial primary key,
I have the following table EVENT_LOG : EVENT_ID: pk, int, not null TYPEID: fk,
Assuming you have the following database table: create table Names ( Id INT IDENTITY
This is my table definition: CREATE TABLE orders_total ( orders_total_id int unsigned NOT NULL
I have a table with the following definition: CREATE TABLE url_tracker ( id int
I have the following table declared in MySQL: CREATE TABLE IF NOT EXISTS `ci_sessions`
I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT
I have the following table definition for journal CREATE TABLE public.journal ( id public.inc_journal
Lets say i have a table with the following definition create table dummy (col1

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.