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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:27:11+00:00 2026-05-12T21:27:11+00:00

What is the best way to emulate Tagged union in databases? I’m talking about

  • 0

What is the best way to emulate Tagged union in databases?
I’m talking about something like this:

create table t1 {
  vehicle_id INTEGER NOT NULL REFERENCES car(id) OR motor(id) -- not valid
  ...
}

where vehicle_id would be id in car table OR motor table, and it would know which.

(assume that motor and car tables have nothing in common0

  • 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-12T21:27:11+00:00Added an answer on May 12, 2026 at 9:27 pm

    Some people use a design called Polymorphic Associations to do this, allowing vehicle_id to contain a value that exists either in car or motor tables. Then add a vehicle_type that names the table which the given row in t1 references.

    The trouble is that you can’t declare a real SQL foreign key constraint if you do this. There’s no support in SQL for a foreign key that has multiple reference targets. There are other problems, too, but the lack of referential integrity is already a deal-breaker.

    A better design is to borrow a concept from OO design of a common supertype of both car and motor:

    CREATE TABLE Identifiable (
     id SERIAL PRIMARY KEY
    );
    

    Then make t1 reference this super-type table:

    CREATE TABLE t1 (
      vehicle_id INTEGER NOT NULL,
      FOREIGN KEY (vehicle_id) REFERENCES identifiable(id)
      ...
    );
    

    And also make the sub-types reference their parent supertype. Note that the primary key of the sub-types is not auto-incrementing. The parent supertype takes care of allocating a new id value, and the children only reference that value.

    CREATE TABLE car (
      id INTEGER NOT NULL,
      FOREIGN KEY (id) REFERENCES identifiable(id)
      ...
    );
    
    CREATE TABLE motor (
      id INTEGER NOT NULL,
      FOREIGN KEY (id) REFERENCES identifiable(id)
      ...
    );
    

    Now you can have true referential integrity, but also support multiple subtype tables with their own attributes.


    The answer by @Quassnoi also shows a method to enforce disjoint subtypes. That is, you want to prevent both car and motor from referencing the same row in their parent supertype table. When I do this, I use a single-column primary key for Identifiable.id but also declare a UNIQUE key over Identifiable.(id, type). The foreign keys in car and motor can reference the two-column unique key instead of the primary key.

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

Sidebar

Related Questions

The best way of describing this is I have a table of people with
The best way I think to explain this is to tell you what my
the best way to explain is with example so: this is the model public
The best way I can think of to ask this is by example... In
The best way to explain this is with code: byte roominspiration = 0; query
What is the best way to resize photos and create square, same-sized thumbnail pictures
Best way to allow the User to define a Table’s Order? We are using
Best way to describe this is explain the situation. Imagine I have a factory
Best way to manage user requests for reading too many rows from a table
What's the best way to emulate single-precision floating point in python? (Or other floating

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.