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.
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
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:
Normally you should always use an
inner joinhere.Making a link
When you assign a teacher to a student (or visa versa, that’s the same).
You only need to do:
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
uniqueindex on the link (recommended)Or you can do the check in the insert statement (not really recommended, but it works).
This will only select 548, 785 if that data is not already in the
link_sttable, 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.
You can list all students in a school like so: