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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:53:04+00:00 2026-05-22T14:53:04+00:00

I’m facing this problem: I have a parent table, and a child table, one

  • 0

I’m facing this problem:

I have a parent table, and a child table, one parent can have multiple children, standard story.

These are the constraints:

  • each parent must have at least one child,
  • each parent must have one favourite child,
  • each parent can have one least-favourite child

How to desing this in SQL?

I’m not sure the standard parent-child tables can be used because of the circular relationship:

Parent table:
parentId
favouriteChildId NOT NULL
leastFavouriteChildId NULL

Child table:
childId
parentId

I was thinking of using a bridge table, but am not sure how to model these constraints.


EDIT: Just to add some clarity, here is part of the problem context:

There is the Price table (child), and PriceGroup table (parent).

PriceGroup has multiple prices, one mandatory mainPrice (favouriteChild) and can have one officialPrice (leastFavouriteChild).

The following is not related to the problem, but sheds some light on the context:
Prices are grouped according to Products they refer to, and one Product can have multiple prices – these are then grouped in price groups, and each group needs reference to a main price, and an official price (if there is one).

  • 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-22T14:53:05+00:00Added an answer on May 22, 2026 at 2:53 pm

    Out of the business rules that you have given

    1. each parent must have at least one child,
    2. each parent must have one favourite child,
    3. each parent can have one least-favourite child

    Your solution of

    Parent table:
    parentId (PK)
    favouriteChildId NOT NULL (FK)
    leastFavouriteChildId NULL (FK)
    
    Child table:
    childId (PK)
    parentId (FK)
    

    satisfies 2 and 3.
    But also it satisfies the 1 (since favouriteChildId NOT NULL will not allow creating parent records with no children).

    Since you already have the above, I will assume that the real question for you is how to make parentId in Child table NOT NULL.

    Normally, there are provision in SQL so that you could do something like

    BEGIN TRANS
    INSERT INTO TABLE1 (FK not checked yet)
    INSERT INTO TABLE2 (FK not checked yet)
    COMMIT (All integrity checked)
    

    in that case ‘circular reference’ would not be a problem (see DEFERRED)

    Mysql does not support it, so you have following options

    Triggers:
    It can be assumed that at the time of inserting parent record a favorite child is already known then you can have a trigger that would run before insert on the parent table and

    • insert the favorite child into child table getting its ID
    • insert the parent record with the child’s ID

    NOTE: Problem is that this way you can formally satisfy the criteria, but to insert the child record first you will have to either use additional columns in parent so that the trigger can know about other fields in child table or insert a blank record (in either case the design is not clean)

    Integrity through security
    The above can be implemented as stored procedure without requiring additional fields on Parent table level. However stored procedures could, normally be bypassed so it does not qualify as real integrity rule.

    There is, a generic way to make something achieved with a stored procedure qualify as integrity rule – and that is to remove write permissions for all regular users (and applications) for these tables and allow the data to be changed only through stored procedure.

    EDIT:
    Regarding triggers there is also a way to implement the rule with triggers, and that is to accept that you will have to insert records separately and that you can must have, at one moment data that breaks your business rules.

    In that case you can have a STATUS atrribute for the parent record (for example: COMPLETE vs INCOMPLETE) and make favouriteChildId a NULLable FK, but when updating the status to COMPLETE you can have trigger check that integrity is respected.

    This requires additional column but can make things quite clean (you can actually create a view on this table that would only expose only records that are COMPLETE, effectively making it look like the table with FK NOT NULL).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.