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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:30:14+00:00 2026-05-12T11:30:14+00:00

I have a table which needs to link one of three seperate tables, but

  • 0

I have a table which needs to link one of three seperate tables, but it should only link to one of them, e.g.

Main_Table
id UNIQUEIDENTIFIER
t1_id UNIQUEIDENTIFIER
t2_id INT
t3_id INT

T1
id UNIQUEIDENTIFIER
name VARCHAR(255)

T2
id INT
name VARCHAR(255)

T3
id INT
name VARCHAR(255)

Is it possible to have a constraint whereby only one of t1, t2 or t3 is not null at any one time?

Is this just bad design? If so, what suggestions would you make for the design?

EDIT:

I’ve been asked to elaborate the reasons behind this particular design.

Main_Table is attempting to be a payer table, which could reference either an individual user (T1), a group of individual users (T2), or a group of groups (T3).

This is a database design I’ve inherited, and it isn’t really subject to change unfortunately.

My biggest problem is that I need to associate between different types, so a type field won’t work here as the indexes are different.

  • 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-12T11:30:14+00:00Added an answer on May 12, 2026 at 11:30 am

    The design you’re describing is called exclusive arcs. Yes, it’s a pretty fragile design and even fails some rules of normalization.

    Here’s an alternative:

    Main_Table
    id UNIQUEIDENTIFIER
    t_id INT NOT NULL
      FOREIGN KEY (t_id) REFERENCES T0 (id)
    
    T0
    id UNIQUEIDENTIFIER
    type INT NOT NULL CHECK (type IN (1,2,3))
      UNIQUE KEY (id, type)
    
    T1
    id INT 
    type INT NOT NULL CHECK (type = 1) 
    name VARCHAR(255)
      FOREIGN KEY (id, type) REFERENCES T0 (id, type)
    
    T2
    id INT
    type INT NOT NULL CHECK (type = 2)
    name VARCHAR(255)
      FOREIGN KEY (id, type) REFERENCES T0 (id, type)
    
    T3
    id INT
    type INT NOT NULL CHECK (type = 3)
    name VARCHAR(255)
      FOREIGN KEY (id, type) REFERENCES T0 (id, type)
    

    With this design, each row in Main_Table must reference one row in T0.
    Likewise, each row in T0 can be the parent of only one row in T1, T2, or T3.

    This is a way to implement Class Table Inheritance and Polymorphic Associations without breaking referential integrity.


    Main_Table is attempting to be a payer
    table, which could reference either an
    individual user (T1), a group of
    individual users (T2), or a group of
    groups (T3).

    Right, so think of this in terms of object-oriented design. If you had three classes that could function as a recipient of payments, you’d create a interface called Payable or something, so that each you could rely on typing those objects. All Payable objects must have a sendPayment() method for instance. In some OO languages, the interface is a superclass and is called an abstract class or a pure virtual class.

    The T0 table functions as a common type for each of the child tables T1, T2, and T3. When Main_Table has a foreign key to T0, it’s like saying Main_Table must have a reference to some entity that is Payable, but any object descending from that superclass is okay to use.

    The type column is just a trick to make sure that a given T0.id can be referenced only by one subclass table at a time. It’s kind of optional, if you can rely on your application logic to insert a given child row into only one of the subclass tables.


    Also see the section on Polymorphic Associations in my presentation “SQL Antipatterns Strike Back.”

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

Sidebar

Related Questions

I have a table which needs to be scrollable within a single page. I've
I have an entity which needs fields from another table. The second table is
I have one table TABLE_SUBJECT which contain a number of subjects. I need to
I have a MySQL table which has three columns: Authority | MapID | Time
I have three tables, Entries , Tags , Entry_Tag_Link I have my query which
We have a link table that can handle multiple types of object on one
I have a table in which I need both the values to be primary
I have table which contains double values stored in mysql database ...I need to
I have a table called tester which I need to overlay with a temporary
I have a rather large table which I need to query for a reporting

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.