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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:25:48+00:00 2026-06-04T20:25:48+00:00

I am creating a table labeled categories where main catgory(parent column) contains 0 and

  • 0

I am creating a table labeled categories where main catgory(parent column) contains 0 and where subcategory contains the ID of the parent category. I heard it is called Referencing. My question: Do i have this table strucutred properly? Or is there a better way like implementing a traversal tree or similar method?

CREATE TABLE `categories` (
`primary_id` int(11) NOT NULL auto_increment,
`master_id` int(11) NOT NULL default '0',
`name` varchar(50) NOT NULL default '',
PRIMARY KEY (`c_id`)
)

INSERT INTO `categories` (`primary_id`, `master_id`, `name`) VALUES 
(1, '0', 'Games'),
(2, '0', 'Technology'),
(3, '0', 'Science'),
(4, '0', 'Pop Culture'),
(5, '0', 'Jobs/Services'),
(6, '0', 'Blogs'), 
(7, '1', 'Computer'),
(8, '1', 'Game Console'),
(9, '2', 'Cellphone'),
(10, '2', 'Audio/Video/Photos'),
(11, '2', 'Laptop'),
(12, '2', 'Others >'),
(13, '3', 'Human Body'),
(14, '3', 'Ocean Life'),
(15, '3', 'Plant Life'),
(16, '3', 'Animal Life'),
(17, '4', 'Artist'),
(18, '4', 'Upcoming'),
(19, '5', 'Jobs'),
(20, '5', 'Tutoring'),
(21, '5', 'Seminars'),
(22, '5', 'Haircuts'),
(23, '9', 'Iphone'),
(24, '9', 'Android'),
(25, '9', 'Windows Mobile'),
(26, '11', 'Toshiba'),
(27, '11', 'HP'),
(28, '11', 'Apple'),
  • 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-04T20:25:50+00:00Added an answer on June 4, 2026 at 8:25 pm

    If you use the InnoDB engine, you can enforce referential integrity for your tables. That way, you can make sure you never insert a category which does not have matching parent id, cascade delete child categories when you delete the parent, etc. Change your create statement to:

    CREATE TABLE `categories` (
    `primary_id` int(11) NOT NULL auto_increment,
    `master_id` int(11) NOT NULL default '0',
    `name` varchar(50) NOT NULL default '',
    PRIMARY KEY (`c_id`),
    KEY `fk_master_id1` (`master_id`),
    CONSTRAINT `fk_master_id1` FOREIGN KEY (`master_id`) REFERENCES `master` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    

    While this strategy works sufficiently well for most cases, you might also want to look at other strategies for implementing tree-like structures and the challenges that come with querying them effectively. See this answer on SO for more information on alternative designs for storing tree-like structures in MySQL.

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

Sidebar

Related Questions

Let's say I'm creating a table foo with a column bar that should be
I have a simple question in creating table in database. Here's my code: $query=CREATE
We document our SQL Server database by creating table and column level Description extended
I'm creating table like this: <table> <tr> <th>Column</th> </tr> @foreach (var item in someList)
I am creating table with sqlalchemy. user = Table('users', Metadata, Column('datecreated', TIMESTAMP, server_default=text('CURRENT_TIMESTAMP')), Column('datemodified',
I am creating table with AjaxFallbackDefaultDataTable. I want to add image to each column
I am getting this error message while creating table with one of the column
I am creating table like this: CREATE TABLE foobar (id uniqueidentifier, foo text, bar
Hi Following is the syntax for creating table in mysql. I want to create
After creating a table (by migration), I want to insert some entries directly. How

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.