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

  • Home
  • SEARCH
  • 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 884433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:45:00+00:00 2026-05-15T12:45:00+00:00

Sorry for the title, it’s difficult to explain. I need a data model similar

  • 0

Sorry for the title, it’s difficult to explain.

I need a data model similar to this:
alt text

As you can see, a set can belong to both a user or a school. My problem: It should only be allowed to belong either to a user OR a school. But never both at the same time.

How can I solve this problem?

  • 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-15T12:45:00+00:00Added an answer on May 15, 2026 at 12:45 pm

    Your current design is called exclusive arcs where the sets table has two foreign keys, and needs exactly one of them to be non-null. This is one way to implement polymorphic associations, since a given foreign key can reference only one target table.

    Another solution is to make a common “supertable” that both users and schools references, and then use that as the parent of sets.

    create table set_owner
    
    create table users 
      PK is also FK --> set_owner
    
    create table schools
      PK is also FK --> set_owner
    
    create table sets 
      FK --> set_owner
    

    You can think of this as analogous to an interface in OO modeling:

    interface SetOwner { ... }
    
    class User implements SetOwner { ... }
    
    class School implements SetOwner { ... }
    
    class Set {
      SetOwner owner;
    }
    

    Re your comments:

    So the SetOwner table contains both UserIDs and SchoolIDs, correct? That would mean that I wouldn’t be allowed to have the same ID for an user and a school. How can I enforce this?

    Let the SetOwners table generate id values. You have to insert into SetOwners before you can insert into either Users or Schools. So make the id’s in Users and Schools not auto-incrementing; just use the value that was generated by SetOwners:

    INSERT INTO SetOwners DEFAULT VALUES; -- generates an id
    INSERT INTO Schools (id, name, location) VALUES (LAST_INSERT_ID(), 'name', 'location');
    

    This way no given id value will be used for both a school and a user.

    If I’d like to get the owner type for a set, do I need an ownerType attribute in my SetOwner table?

    You can certainly do this. In fact, there may be other columns that are common to both Users and Schools, and you could put these columns in the supertable SetOwners. This gets into Martin Fowler’s Class Table Inheritance pattern.

    And if I want to get the name of the school or the user (whichever type it is), can I do this with a single query, or do I need two queries (first to get the type and second to get the name)?

    You need to do a join. If you’re querying from a given Set and you know it belongs to a user (not a school) you can skip joining to SetOwners and join directly to Users. Joins don’t necessarily have to go by foreign keys.

    SELECT u.name FROM Sets s JOIN Users u ON s.SetOwner_id = u.id WHERE ...
    

    If you don’t know whether a given set belongs to a User or a School, you’d have to do an outer join to both:

    SELECT COALESCE(u.name, sc.name) AS name 
    FROM Sets s 
    LEFT OUTER JOIN Users u ON s.SetOwner_id = u.id
    LEFT OUTER JOIN Schools sc ON s.SetOwner_id = sc.id 
    WHERE ...
    

    You know that the SetOwner_id must match one or the other table, Users or Schools, but not both.

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

Sidebar

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.