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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:53:46+00:00 2026-05-25T13:53:46+00:00

I have a composite primary key table (Maintenance Items). I would like to create

  • 0

I have a composite primary key table (Maintenance Items). I would like to create another table which maps a parent child relationship between items within this table.

I am not sure how to create the Parent Child Relationship table see below either option 1 or option 2.

NOTE: Our business rules stipulate that a parent child relationship can only exits between maintenance items which share the same maintenance program. This will never change!

Table Maintenance Programs
ProgramCode (Primary Key)
ProgramDescription
Other Columns …

Table Maintenance Items (Composite Primary key Table)
ProgramCode (Composite Primary Key)
MaintenanceCode (Composite Primary Key)
MaintenanceDescription
Other Columns …

Table Parent/Child Maintenance Items (Option 1)
ProgramCode
ParentMaintenanceCode
ChildMaintenanceCode

Table Parent/Child Maintenance Items (Option 2)
ParentProgramCode (Same value as ChildProgramCode)
ParentMaintenanceCode
ChildProgramCode (Same value as ParentProgramCode)
ChildMaintenanceCode

There will be no other columns in the Parent/Child tables it is a relationship mapping table only.

Which is the better option? Option 2 seems to suit best practises but given our buisness rule means we essentially have two columns with exactly the same data (ProgramCode).

  • 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-25T13:53:46+00:00Added an answer on May 25, 2026 at 1:53 pm

    The only reason for using an associative entity like you’re suggesting is if you want a many-to-many relationship between maintenance items. If that is what you’re after, then option 1 is what you do. The schema would look like something like

    create table MaintenanceProgram
    (
      ProgramCode int not null ,
    
      ... -- other non-key attributes here
    
      primary key ( ProgramCode ) ,
    
    )
    create table MaintenanceItem
    (
      ProgramCode     int not null ,
      MaintenanceCode int not null ,
    
      ... -- other non-key attributes here
    
      primary key ( ProgramCode , MaintenanceCode ) ,
    
      foreign key                     ( ProgramCode )
        references MaintenanceProgram ( ProgramCode ) ,
    
    )
    create table MaintenanceItemMap
    (
      ProgramCode           int not null , 
      ParentMaintenanceCode int not null ,
      ChildMaintenanceCode  int not null ,
    
      primary key ( ProgramCode , ParentMaintenanceCode , ChildMaintenanceCode ) ,
    
      foreign key                  ( ProgramCode , ParentMaintenanceCode )
        references MaintenanceItem ( ProgramCode , MaintenanceCode       ) ,
      foreign key                  ( ProgramCode , ChildMaintenanceCode  )
        references MaintenanceItem ( ProgramCode , MaintenanceCode       ) ,
    
      check ( ParentMaintenanceCode != ChildMaintenanceCode ) ,
    
    )
    

    This ensure that all related maintenance items share the same ProgramCode and that a maintenance item cannot map to itself (the check constraint).

    However, your problem statement refers to a parent/child relationship, which sounds more like a hierarchy/tree. In this case, the schema you would want would look something like this:

    create table MaintenanceProgram
    (
      ProgramCode int not null ,
    
      ... -- other non-key attributes here
    
      primary key ( ProgramCode ) ,
    
    )
    create table MaintenanceItem
    (
      ProgramCode           int not null ,
      MaintenanceCode       int not null ,
      ParentMaintenanceCode int     null ,
    
      ... -- other non-key attributes here
    
      primary key ( ProgramCode , MaintenanceCode ) ,
    
      foreign key                     ( ProgramCode  )
        references MaintenanceProgram (  ProgramCode ) ,
      foreign key                     ( ProgramCode , ParentMaintenanceCode )
        references MaintenanceItem    ( ProgramCode , MaintenanceCode       ) ,
    
      check ( MaintenanceCode != ParentMaintenanceCode or ParentMaintenanceCode is null ) ,
    
    )
    

    The above says that each maintenance item is related to a single maintenance program; conversely, that each maintenance program has zero or more maintenance items.

    Further, that each maintenance item has to zero or 1 parent maintenance items, which must be related to the same maintenance program.

    The check constraint says that a given maintenance item may not be its own parent.

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

Sidebar

Related Questions

I have a table with a composite Primary Key, arranged something like this: CREATE
I have a table (Table1) which has a composite primary key(Column1 + Column2). I
We have a composite primary key for the site table defined below. Functionally, this
I have a table A with a composite primary key and I have table
How to identify composite primary key in any Mysql Database table? or EDIT 2
I have encountered a problem in that I already have a composite primary key
Is it better to have a single primary key, or use composite primary keys
I have two tables: <class name=Content table=language_content lazy=false> <composite-id> <key-property name=contentID column=contentID/> <key-property name=languageID
We have a fairly big DB (~200 tables) which almost entirely uses composite primary
I have a composite control that contains a ListBox child control. On postback, the

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.