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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:43:00+00:00 2026-06-11T01:43:00+00:00

I have a table that looks something like this: CREATE TABLE A ( A_UNIQUE_ID

  • 0

I have a table that looks something like this:

CREATE TABLE A (
  A_UNIQUE_ID int NOT NULL PRIMARY KEY,
  A_OTHER_ID int NOT NULL,
  A_CURRENT_FL bit NOT NULL )

and it has values similar to this

INSERT INTO TABLE A VALUES (1, 1, 0);
INSERT INTO TABLE A VALUES (2, 1, 0);
INSERT INTO TABLE A VALUES (3, 1, 1);

I want to create another table B like this

CREATE TABLE B (
  B_UNIQUE_ID int NOT NULL PRIMARY KEY,
  A_OTHER_ID int NOT NULL)

where B.A_OTHER_ID is constrained to a unique row in A. So B.A_OTHER_ID = A.A_OTHER_ID AND A.CURRENT_FL = 1. Is that possible with some type of check constraint setup? Examples are very much appreciated.

  • 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-11T01:43:02+00:00Added an answer on June 11, 2026 at 1:43 am

    You can do it with some computed columns, but it doesn’t look very pretty:

    CREATE TABLE A (
      A_UNIQUE_ID int NOT NULL PRIMARY KEY,
      A_OTHER_ID int NOT NULL,
      A_CURRENT_FL bit NOT NULL,
      OTHER_XREF as CASE WHEN A_CURRENT_FL = 1 THEN A_OTHER_ID END persisted,
      UNIQ as CASE WHEN A_CURRENT_FL = 0 THEN A_UNIQUE_ID ELSE 0 END persisted 
      constraint UQ_OTHER_XREF UNIQUE (OTHER_XREF,UNIQ)
      )
    
    INSERT INTO A VALUES (1, 1, 0);
    INSERT INTO A VALUES (2, 1, 0);
    INSERT INTO A VALUES (3, 1, 1);
    
    CREATE TABLE B (
      B_UNIQUE_ID int NOT NULL PRIMARY KEY,
      A_OTHER_ID int NOT NULL,
      XREF as 0 persisted,
      constraint FK_B_A FOREIGN KEY (A_OTHER_ID,XREF) references A (OTHER_XREF,UNIQ))
    

    This works:

      insert into B VALUES (2,1)
    

    This breaks the FK constraint:

      insert into B VALUES (3,2)
    

    The two computed columns in table A ensure that any row with A_CURRENT_FL = 1 has that as a distinct value in OTHER_XREF. Other rows will have NULL in that column. However, to apply a UNIQUE constraint (the target for the foreign key), we need something that will be distinctly different for every row with a NULL, whilst being a well-known value for the row with A_CURRENT_FL = 1. In this case, I made the well known value 0.

    Finally, the foreign key constraint needs to match both of these columns, so we add a new computed column in table B with the well known value 0.

    Column names aren’t particularly good, but I’m sure you can come up with some.


    Note, also, that by the above, we’ve also implemented another constraint that you didn’t mention, but that also may be important – there can’t be two rows now in table A that both have A_CURRENT_FL set to 1, for the same A_OTHER_ID value. This also generates an error:

    INSERT INTO A VALUES (4,1,1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks something like this: CREATE TABLE student_results(id integer, name
I have a fairly complex query that looks something like this: create table Items(SomeOtherTableID
I have a table that looks like this CREATE TABLE `purchases` ( `id` INT(10)
I have a table that looks like this CREATE TABLE `time_table` ( `id` INT(10)
I have this table that looks like this CREATE TABLE `purchases` ( `id` INT(10)
I have a table that looks something like this CREATE TABLE MyTable( [RecordID] [bigint]
I have a table that looks something like this: id | firstperson | secondperson
I have a table that looks something like this: Name Year Value A 2000
I have a MySQL table that looks something like this: +-----+------------+ | id |
I currently have a table structure that looks something like this(some details omitted): ColumnName

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.