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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:36:55+00:00 2026-06-12T08:36:55+00:00

I have a table structure for categories, Each category can have many children, but

  • 0

I have a table structure for categories,

Each category can have many children, but can only be a child of one category.

I am deciding whether I should just add a parent_id column to the table, or whether I should add a reference table that just has the parent_id and child_id and maps them together.

If it is important a category can have no parent, in which case it is a root category.

The most common use cases for this table will be:

Selecting one category
Selecting all categories
Selecting all children of a category
Selecting the parent of a category
Selecting the child tree of a category

As far as I can tell there is no benefit of having a reference table for this setup? can anyone see any reason for using a reference table?

  • 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-12T08:36:56+00:00Added an answer on June 12, 2026 at 8:36 am

    If you add a reference table, you create an n:n relationship, which you don’t want.

    So just add a parent_id to the table.
    Make it nullable so you can define a category as the root.

    Everything you want to select is quite easy, except for the child tree, but an extra table won’t help with that. In Oracle you got connect by to select tree-like data, but MySQL unfortunatly doesn’t support that, although alternative solutions are often requested and provided.

    There are some obstacles:

    1. Since you cannot make parent_id unique (multiple childs can have the same parent), you will have to add a trigger to enforce only one category being the root, although maybe you can live without that check for the moment.

    2. You could theoretically create a loop: Make a the parent of b, b the parent of c, and c the parent of a. To check if this is the case, you should follow the path to the root. If on that path you’ll find any category twice, you’re in trouble. I think you could use a trigger to validate this as well, although maybe you can live without that check for the moment. It all depends on how you edit your data, but if you are going to query a full tree, you don’t want to get into endless loops because of corrupt data.

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

Sidebar

Related Questions

I have a standard recursive table of categories , each category can have a
I have a categories table Category [ID, Title] and a Parent/Child table CategoryParent [ID,CategoryID,ParentCategoryID]
I have a CATEGORIES table that links to an ITEMS table (one to many).
I have a table for product category that has a hierarchical structure. Each Category_ID
I have an table structure like this mysql> SELECT id, name, parent_id FROM categories;
I have a table called Categories with this structure: CREATE TABLE `Categories` ( `CatID`
I have a table with following structure: categories id name parent_id I want to
i have table structure with 3 colums (column1, column2, column3) and i want to
I have table structure: ous.txt 1452 1793 out.txt 36796 14997 ouw.txt 478 4247 3
I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE

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.