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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:33:00+00:00 2026-05-16T01:33:00+00:00

I am planning to create a web application using spring/hibernate. How should I design

  • 0

I am planning to create a web application using spring/hibernate.

How should I design my postgresql database in order to support inheritance?
Assume that I have the following tables: super_table, sub_table_1, sub_table_2.
Then sub_table_1 and subtable_2 would inherit super_table.

Should I add a type column (like a char or int) so that I would know
if the row in super_table is sub_table_1 or sub_table_2?

Please advise. Thanks.

  • 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-16T01:33:01+00:00Added an answer on May 16, 2026 at 1:33 am

    Sure you can do this for example to enforce that only one sub table can reference a given row:

    CREATE TABLE Super_Table (
      super_id SERIAL PRIMARY KEY,
      sub_type CHAR(1) NOT NULL,
      UNIQUE KEY (super_id, sub_type) 
    );
    
    CREATE TABLE Sub_Table_A (
      super_id PRIMARY KEY,
      sub_type CHAR(1) NOT NULL,
      CHECK (sub_type = 'A'),
      FOREIGN KEY (super_id, sub_type) REFERENCES Super_Table (super_id, sub_type)
    );
    
    CREATE TABLE Sub_Table_B (
      super_id PRIMARY KEY,
      sub_type CHAR(1) NOT NULL,
      CHECK (sub_type = 'B'),
      FOREIGN KEY (super_id, sub_type) REFERENCES Super_Table (super_id, sub_type)
    );
    

    Now there is no way a row in Super_Table can be referenced by a row in both sub tables. The row in Super_Table must have either ‘A’ or ‘B’ and so only one of the sub tables can satisfy the foreign key reference.


    Re your comment:

    My understanding is that the current PostgreSQL implementation of INHERITS allows a number of anomalies related to indexes, unique constaints, and foreign key constraints. Using this feature is tricky and error-prone.

    Basically, because indexes exist only over a single table, if you have a unique constraint on your parent table then how can it enforce that uniqueness over the parent and all of its children? The children could insert values into their tables that already exist in the parent, or the parent could insert a value that already exists in one of the children.

    Likewise foreign keys cannot reference the parent table and/or its children because it’s ambiguous which row is referenced if multiple rows can exist in parent/children with the same primary key or unique value.

    These are known and unresolved limitations of INHERITS in PostgreSQL. The design I showed above resolves the problem for primary key, but not for secondary unique keys.

    http://archives.postgresql.org/pgsql-hackers/2010-05/msg00285.php

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

Sidebar

Related Questions

I was thinking about the architecture of a web application that I am planning
I'm planning to create a blog using php/mysql on apache (probably on a linux
Apologies for the newbie web service question - I am trying to create a
I'm currently in the planning/early building phase of a desktop flex application that will
I am in the planning phases of building a new ASP.NET website. The website
I have a java application that will take the image as an input and
I'm planning to develop a small business app that I'd like to be deployable
I'm working on a small project, it is a web harvester created in python
First of all, I hope I'd get some advice about my practice because based
I've been stuck for ages now trying to implement threading or background image loading

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.