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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:21:23+00:00 2026-06-09T11:21:23+00:00

I have three tables, table A has a unique primary key that is auto-incremented,

  • 0

I have three tables, table A has a unique primary key that is auto-incremented, and the other two (table B and C) have primary keys that have a foreign key constraint that ties them to the first tables primary key.

I want to make a constraint that maintains that for all rows in the second and third tables they cannot contain any duplicate, and for all records in table A there is a matching record in B or C.

So basically a record of type A can be a type B or C and must be one of B or C.

Is there away to make this constraint without triggers in MySQL? or are triggers necessary?

Thanks for any help.

  • 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-09T11:21:25+00:00Added an answer on June 9, 2026 at 11:21 am

    You can use a “type” table:

    CREATE TABLE Type
      ( type_code CHAR(1) NOT NULL
      , PRIMARY KEY (type_code)
      ) ;
    

    with exactly 2 rows (as many as the different subtype tables you need:

    INSERT INTO Type (type_code)
    VALUES ('B'), ('C') ;
    

    The supertype table (that includes a column that references “Type”):

    CREATE TABLE A
      ( a_id INT NOT NULL AUTO_INCREMENT
      , type_code CHAR(1) NOT NULL
      , PRIMARY KEY (a_id)
      , UNIQUE KEY (type_code, a_id)
      , FOREIGN KEY (type_code)
          REFERENCES Type (type_code)
      ) ;
    

    The subtype tables (that are now referencing the combination of A’s Primary key and type_code:

    CREATE TABLE B
      ( a_id INT NOT NULL
      , type_code CHAR(1) NOT NULL DEFAULT 'B'
      , PRIMARY KEY (type_code, a_id)
      , FOREIGN KEY (type_code, a_id)
          REFERENCES A (type_code, a_id)
      , CHECK (type_code = 'B')
      ) ;
    
    CREATE TABLE C
      ( a_id INT NOT NULL
      , type_code CHAR(1) NOT NULL DEFAULT 'C'
      , PRIMARY KEY (type_code, a_id)
      , FOREIGN KEY (type_code, a_id)
          REFERENCES A (type_code, a_id)
      , CHECK (type_code = 'C')
      ) ;
    

    The above would work fine, if only MySQL had implemeneted CHECK constraints. But it hasn’t. So, to be absolutely sure that all your specifications are enforced, and not 'B' type data is inserted in C table, you’ll have to add 2 more “type” tables (and remove the useless in MySQL CHECK constraints):

    CREATE TABLE TypeB
      ( type_code CHAR(1) NOT NULL
      , PRIMARY KEY (type_code)
      ) ;
    
    CREATE TABLE TypeC
      ( type_code CHAR(1) NOT NULL
      , PRIMARY KEY (type_code)
      ) ;
    

    with exactly 1 rows each:

    INSERT INTO TypeB (type_code)
    VALUES ('B') ;
    
    INSERT INTO TypeC (type_code)
    VALUES ('C') ;
    

    and the additional FKs:

    ALTER TABLE B
      ADD FOREIGN KEY (type_code)
        REFERENCES TypeB (type_code) ;
    
    ALTER TABLE C
      ADD FOREIGN KEY (type_code)
        REFERENCES TypeC (type_code) ;
    

    With these constraints, every row of table A, will be either of type B or C and it will be in the respective table (B or C) and never in both.

    If you also want to ensure that they will be in exactly one table (and never in neither B nor C), that should be taken care when inserting in A (all insertions should be done with a transaction that enforces that requirement).

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

Sidebar

Related Questions

I have a SqlServer 2008 table which has a Primary Key (IsIdentity=Yes) and three
I have a MySQL table with a primary key field that has AUTO_INCREMENT on.
I have a table that has three different date columns, so I set each
I have a table in my data base that has three columns: Screen, Icon,
I have three tables: Registrants Registrant ID (Primary key) Correspondences Correspondence ID Correspondence Time
I have two tables that get joined regularly. Table One is about 1 Million
I have a relational database with about 100 tables. Each table has unique, numerical,
I have a table in HTML which has three columns i.e Error - User
I have a table column where each row has one of three states, and
I have a table PGM_MASTER, which has three columns PGM_ID | int(11) PGM_ENV_ID |

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.