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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:15:06+00:00 2026-05-26T06:15:06+00:00

I need to create MySQL tables that represent a tree structure like this: Root

  • 0

I need to create MySQL tables that represent a tree structure like this:

Root
|- Chapter 1
|     |- Chapter 1.1
|     |    |- Article 1.1.1
|     |    |- Article 1.1.2
|     |- Article 1.2
|     |- Chapter 1.3
|          |- Chapter 1.3.1
|          |      |- Article 1.3.1.1
|          |      |- Article 1.3.1.2
|          |- Article 1.3.2
|          |- Article 1.3.3
|- Chapter 2
      |-Chapter 2.1
      |     |- ...
      |- Chapter 2.2
      |- ...

Simply speaking, there are two types of entity: Chapter and Article. Article is the smallest entity that there is no child under it, while Chapter can contains sub-chapter or articles as children entity. Each entity will have an ID and a Name.

The order of children has no set rule, it can be Chapter, then Article, then Chapter again.

Another challenge is, when a chapter is re-positioned from one chapter to another chapter, all the children should also be re-positioned accordingly. For example, when I move chapter 1.3.1 to under chapter 1.1 (so chapter 1.3.1 becomes chapter 1.1.3), then the article 1.3.1.1 and article 1.3.1.2 should also be moved and become article 1.1.3.1 and article 1.1.3.2. And at the same time Article 1.3.2 and Article 1.3.3 will become Article 1.3.1 and Article 1.3.2 respectively.

So what I’m asking is, how to design the database table so as to present these relationships? And how the SQL will looks like for adding new element / deleting element and re-positioning the elements? (I can use Ajax to handle the re-positioning interaction, and use PHP to generate those hierarchy numbering)

Also, as the tree is generally quite long, I wish to avoid updating of all elements just because only one element is repositioned. (Not sure if this wish is technically possible.)

  • 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-26T06:15:06+00:00Added an answer on May 26, 2026 at 6:15 am

    The best information I have found on representing tree structures in a database is in Joe Celko’s Trees and Hierarchies in SQL for Smarties.

    You can probably find enough information on the web but I’d recommend getting the book, I found it very useful when implementing a nested set hierarchy.

    You can either use an adjacency list or nested sets to model the tree in the database, I’m assuming you are using an adjacency list (where each entry has a parent attribute)

    If you want to be able to move entire sub trees from one parent to anther then it should be as easy as changing the parent_id (or whatever PK you are using) to reference the new parent. A nested set model requires changes to all the nodes when a sub tree is moved.

    However, other operations are easier on a nested set, such as selecting all the child nodes under a specific parent. This can be more difficult with the adjacency list model but it has become easier with the advent of recursive CTE.

    If you are not worried about the order of the content I would avoid storing the chapter numbers with your data. Apply them when you select the data and then you avoid having to update every node when your tree changes.

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

Sidebar

Related Questions

I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
In MySQL 5.1 and PHP 5.4 I need to create temporary tables from the
I'm working in an application that uses servlets and mysql. I'd like to create
I need to create a table in MySQL which stores the different currency symbols
UPDATED: I am using MySQL statement to create a view: I need to show
I'm having the following problem with 2 MySQL tables that have a relation: I
I have a model that was generated for MySQL 5 but now I need
I need create a very big project, the project has a lot of tables
QUESTION How can I create a MySQL table so, that a field's default value
I have 2 MySQL tables with the following schemas for a web site that's

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.