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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:26:31+00:00 2026-05-23T08:26:31+00:00

When building a category navigation system for a business directory with a many to

  • 0

When building a category navigation system for a business directory with a many to many relationship, I understand that it is good practise to create a mapping table.

Category Table ( CategoryId, CategoryName )
Business Table ( BusinessId, BusinessName )
Category Mapping Table ( BusinessId, CategoryId )

When I join the Category table and Business table to create the mapping table would this then give me a table which contains every possible business and category relationship?

I have 800 categories and 1000 business listings. Would that then give me a table containing 800,000 possible relationships. If so how would I focus on only the relationships that exist? Would I have to go through all listings (800,000) marking them as true or false?

I have been getting really confused about this so any help would be much appreciated.

  • 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-23T08:26:32+00:00Added an answer on May 23, 2026 at 8:26 am

    When using many-to-many relationships, the only realistic way to handle this is with a mapping table.

    Lets say we have a school with teachers and students, a student can have multiple teachers and visa versa.

    So we make 3 tables

    student
      id unsigned integer auto_increment primary key
      name varchar
    
    teacher
      id unsigned integer auto_increment primary key
      name varchar
    
    link_st
      student_id integer not null
      teacher_id integer not null
      primary key (student_id, teacher_id)
    

    The student table will have 1000 records
    The teacher table will have 20 records
    The link_st table will have as many records as there are links (NOT 20×1000, but only for the actual links).

    Selection
    You select e.g. students per teacher using:

    SELECT s.name, t.name 
    FROM student
    INNER JOIN link_st l ON (l.student_id = s.id)   <--- first link student to the link-table
    INNER JOIN teacher t ON (l.teacher_id = t.id)   <--- then link teacher to the link table.
    ORDER BY t.id, s.id
    

    Normally you should always use an inner join here.

    Making a link
    When you assign a teacher to a student (or visa versa, that’s the same).
    You only need to do:

    INSERT INTO link_st (student_id, teacher_id) 
       SELECT s.id, t.id 
       FROM student s 
       INNER JOIN teacher t ON (t.name = 'Jones')
       WHERE s.name = 'kiddo'
    

    This is a bit of a misuse of an inner join, but it works as long as the names are unique.
    If you know the id’s, you can just insert those directly of course.
    If the names are not unique this will be a fail and should not be used.

    How to avoid duplicate links
    It’s very important to avoid duplicate links, all sorts of bad things will happen if you have those.
    If you want to prevent inserting duplicate links to your link table, you can declare a unique index on the link (recommended)

    ALTER TABLE link_st
      ADD UNIQUE INDEX s_t (student_id, teacher_id); 
    

    Or you can do the check in the insert statement (not really recommended, but it works).

    INSERT INTO link_st (student_id, teacher_id) 
      SELECT s.id, t.id
      FROM student s
      INNER JOIN teacher t ON (t.id = 548)
      LEFT JOIN link_st l ON (l.student_id = s.id AND l.teacher_id = t.id)
      WHERE (s.id = 785) AND (l.id IS NULL)
    

    This will only select 548, 785 if that data is not already in the link_st table, and will return nothing if that data is in link_st already. So it will refuse to insert duplicate values.

    If you have a table schools, it depends if a student can be enrolled in multiple schools (unlikely, but lets assume) and teachers can be enrolled in multiple schools. Very possible.

    table school
      id unsigned integer auto_increment primary key
      name varchar
    
    table school_members
      id id unsigned integer auto_increment primary key
      school_id integer not null
      member_id integer not null
      is_student boolean not null
    

    You can list all students in a school like so:

    SELECT s.name
    FROM school i
    INNER JOIN school_members m ON (i.id = m.school_id)
    INNER JOIN student s ON (s.id = m.member_id AND m.is_student = true)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here's the scenario, I'm building a theme that would display sub category of
I am building a system in which the user can build thre own navigation
I am building a website that is basically a small Content Management System. (in
I'm building a page that shows a LINQ query result as a table. Setup
Thinking of building a nested category system using chained strings for lack of a
building a site using PHP and MySQL that needs to store a lot of
Building a website that has English & Japanese speaking users, with the Japanese users
I'm building a nested drop down navigation for products and I want to automatically
I'm building a App that need to list the categories and subcategories of a
I am building small app that will only display products in various categories. And

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.