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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:24:48+00:00 2026-05-15T16:24:48+00:00

I am having a little bit of a problem with setting up a mysql

  • 0

I am having a little bit of a problem with setting up a mysql table that will hold a list of categories and subcategories. I am not to sure how to setup the table. Does it need to be 2 separate tables? 1 for the main categories and 1 for the subcategories or can it be all in 1 table? Would something like this work?

Create Table categories (
    category_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    sub_id INT UNSIGNED NOT NULL,
    name VARCHAR(100) NOT NULL,
    PRIMARY KEY (category_id)
)

CREATE TABLE items (
    item_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    description VARCHAR(100) NOT NULL,
    PRIMARY KEY (item_id),
    FOREIGN KEY (category_id) REFERENCES categories (category_id),
    FOREIGN KEY (sub_id) REFERENCES categories (sub_id)
)

Will this work or is this completely wrong? Thanks in advance for the help!

  • 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-15T16:24:49+00:00Added an answer on May 15, 2026 at 4:24 pm

    If you are 100% sure that you’ll only have two levels of categories (main and sub), you can do a few different things. None of them are your proposed solution:

    CREATE TABLE categories (
        id int not null primary key,
        main varchar(64)
        sub varchar(64)
    );
    
    CREATE TABLE objects (
        id int not null primary key,
        category_id int,
        name varchar(64),
        FOREIGN KEY (category_id) REFERENCES categories (id)
    );
    

    Want all vehicles?

    SELECT * 
    FROM objects AS o 
    INNER JOIN categories AS c ON o.category_id = c.id 
    WHERE c.main = 'vehicles';
    

    Want all roflcopters?

    SELECT * 
    FROM objects AS o 
    INNER JOIN categories AS c ON o.category_id = c.id 
    WHERE c.main = 'vehicles' and c.sub='Roflcopters';
    

    If you want something in the “vehicle” category, but not in any of the subcategories of verhicles, just have a category record where main=’vehicles’ with sub NULL.

    Of course, this is not particularly flexible. You’re stuck with just two levels of categorization, and there’s not a lot of business logic embedded in your category model. But it might be sufficient for your needs.


    Two other good, proven, models are the adjacency list model, and the nested set model, both of which are described, with lots of nice example mysql code, over here

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

Sidebar

Related Questions

I'm having a little bit of trouble making a sticky form that will remember
I'm having a little bit of a problem that I thought was related to
I'm having a little bit of a problem scaling my images to a properly
I'm having a little bit of trouble understanding what the problem is here. I
I'm having a little bit of a problem with LabVIEW 6.x concerning the motion
Good day, I am having a little bit of a problem. I want to
I'm having a little bit of problem with my derived class. Basically I have
im having small problem within my file creator class . im a little bit
I am having a little bit problem on the MapView. First of all, no
I'm having a little bit of a problem here, whenever I use an action

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.