Let’s say I have 3 mysql tables – teacher, subject, and class, each table having only two fields id and name
If every teacher can teach every subject in every class, what is a good way to insert this information using SQL into a fourth table to hold that data, if my fourth table has three fields for teacher_id, subject_id, and class_id
Just to make it clear by an example, if I had only two teachers (teacher.id 1 and 2), two subjects (subject.id 1 and 2) and two classes (class.id 1 and 2), data in the fourth table would look something like below.
teacher_id subject_id class_id
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2
Seems like you are looking at a cross join or cartesian product.
http://en.wikipedia.org/wiki/Join_%28SQL%29#Cross_join
In your case
With large sets of data this can take a while to return.