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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:15:40+00:00 2026-06-15T19:15:40+00:00

I’m currently developping a Website based on MySQL, but switching to another database is

  • 0

I’m currently developping a Website based on MySQL, but switching to another database is not a problem if necessary (like for CTE or so).

I’m looking for the best database data structure (and if possible some SQL snippets) to handle my current project, which is as follow:

  • A product can be made of other products (recursively), so products have to be designed in a tree-oriented architecture.
  • A product may be used for the fabrication of many products. That is where something like the Nested Set architecture won’t work, because a node can be a children and a parent of many nodes, which does not seem possible using NestedSet.
  • A product has a quantity available for each day, but only if it is a “leaf” (it is not made from another product). Otherwise its quantity will depend on the quantity of its direct children (and so on, until the recursion reaches the leaves). So products may be linked to a table “availability”, which contains “product_id”, “date” and “quantity”.

The “tree” structure of the products is quite stable, so the speed of Update/Insert/Delete queries does not really matters.

The final goal is to be able to retrieve in a SELECT query as fast as possible, the list of all the products that are available between two dates specified.

This means that:

  • 1) If the product is not made of another one, it is available if quantity > 0 for each day between the two dates.
  • 2) If the product is made of other ones, it is avalailable only if (1) is true for all its children. If the direct children are not “leaf”, they will not have quantity, so a recursion for (2) will happened until it reaches the final leaves.

By the way, in a real case scenario, my products will never have more than 5 level of depth. Maybe it would be a good (but very ugly) idea to store all the parent ID’s in separate columns.

  • 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-06-15T19:15:41+00:00Added an answer on June 15, 2026 at 7:15 pm

    The data structure you describe is not a tree (which requires that every node has exactly one parent, except the root which has no parent): rather, it is a more general DAG:

    Directed Acyclic Graph

    Other RDBMS have native support for hierarchical data and recursive queries, whereas MySQL does not; instead a good general relational model for storing such data in MySQL is to create a table of the graph’s transitive closure. Using // as the statement delimiter:

    CREATE TABLE Products (
      SKU         SERIAL                COMMENT 'Stock-Keeping Unit code',
      Name        VARCHAR(15)           COMMENT 'Product name',
      Description VARCHAR(255)          COMMENT 'Descriptive text',
      Price       DECIMAL(6,2)          COMMENT 'Selling price',
      isAtomic    BOOLEAN DEFAULT FALSE COMMENT 'Flag indicating atomicity'
    )
      ENGINE  = InnoDB,
      COMMENT = 'Properties relating to each product'
    //
    
    CREATE TABLE ProductComponents (
      SKU          BIGINT UNSIGNED NOT NULL COMMENT 'Stock-Keeping Unit Code',
      ComponentSKU BIGINT UNSIGNED NOT NULL COMMENT 'SKU of comprised component',
      PRIMARY KEY (SKU, ComponentSKU),
      INDEX       (ComponentSKU, SKU),
      FOREIGN KEY (         SKU) REFERENCES Products (SKU),
      FOREIGN KEY (ComponentSKU) REFERENCES Products (SKU)
    )
      ENGINE  = InnoDB,
      COMMENT = 'Transitive closure of the product DAG'
    //
    

    One can use triggers to enforce atomicity in the latter table:

    CREATE TRIGGER ins_atomic BEFORE INSERT ON ProductComponents
    FOR EACH ROW IF
      NEW.SKU <> NEW.ComponentSKU
      AND (SELECT isAtomic FROM Products WHERE SKU = NEW.SKU)
    THEN
      SIGNAL SQLSTATE '45000' SET
        MESSAGE_TEXT = 'Atomic product cannot have a component'
      ;
    END IF//
    
    CREATE TRIGGER upd_atomic BEFORE UPDATE ON ProductComponents
    FOR EACH ROW IF
      NEW.SKU <> NEW.ComponentSKU
      AND (SELECT isAtomic FROM Products WHERE SKU = NEW.SKU)
    THEN
      SIGNAL SQLSTATE '45000' SET
        MESSAGE_TEXT = 'Atomic product cannot have a component'
      ;
    END IF//
    

    You may also want a similar trigger that prevents incorrect updating of Products.isAtomic:

    CREATE TRIGGER upd_prod BEFORE UPDATE ON Products
    FOR EACH ROW IF NEW.isAtomic AND EXISTS (
      SELECT * FROM ProductComponents WHERE SKU <> ComponentSKU AND SKU = NEW.SKU
    ) THEN
      SIGNAL SQLSTATE '45000' SET
        MESSAGE_TEXT = 'Atomic product cannot have a component'
      ;
    END IF//
    

    For the graph depicted above, your data would be as follows:

    INSERT INTO Products
      (SKU, isAtomic)
    VALUES
      (  2, TRUE    ),
      (  3, FALSE   ),
      (  5, FALSE   ),
      (  7, FALSE   ),
      (  8, FALSE   ),
      (  9, TRUE    ),
      ( 10, TRUE    ),
      ( 11, FALSE   )
    //
    
    INSERT INTO ProductComponents
      (SKU, ComponentSKU)
    VALUES
      (2,2),
      (3,3),   (3,8),   (3,9),   (3,10),
      (5,5),   (5,11),  (5,2),   (5,9),   (5,10),
      (7,7),   (7,8),   (7,9),   (7,11),  (7,2),   (7,10),
      (8,8),   (8,9),
      (9,9),
      (10,10),
      (11,11), (11,2),  (11,9),  (11,10)
    //
    

    Then you can store availability as follows:

    CREATE TABLE ProductAvailability (
      SKU      BIGINT UNSIGNED NOT NULL COMMENT 'Stock-Keeping Unit Code',
      Date     DATE                     COMMENT 'Availability date',
      Quantity INT                      COMMENT 'Available quantity',
      PRIMARY KEY (SKU, Date),
      FOREIGN KEY (SKU) REFERENCES Products (SKU)
    )
      ENGINE  = InnoDB,
      COMMENT = 'Available quantities'
    //
    

    Some test data for the above might be:

    INSERT INTO ProductAvailability
      (SKU, Date        , Quantity)
    VALUES
      (  2, '2012-12-13',     NULL),
      (  2, '2012-12-15',       15),
      (  9, '2012-12-13',      234),
      (  9, '2012-12-14',       46),
      (  9, '2012-12-15',        0),
      ( 10, '2012-12-13',        4),
      ( 10, '2012-12-14',        7),
      ( 10, '2012-12-15',        5)
    //
    

    And your query would be:

    SELECT   p.*
    FROM     Products p
        JOIN ProductComponents c USING (SKU)
        JOIN (
               SELECT   p.SKU AS ComponentSKU,
                        COUNT(*) = DATEDIFF(@end_date, @start_date) + 1 AS available
               FROM     Products p LEFT JOIN ProductAvailability a
                     ON a.SKU = p.SKU
                    AND a.Quantity > 0
                    AND a.Date BETWEEN @start_date AND @end_date
               WHERE    p.isAtomic
               GROUP BY p.SKU
             ) q USING (ComponentSKU)
    GROUP BY p.SKU
    HAVING   NOT SUM(q.available = 0)
    

    See it on sqlfiddle.

    • 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&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I've tracked down a weird MySQL problem to the two different ways I was
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is 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.