:knuppel2:
(for many to many, you will need a linking table - has someone taught you this?)
hell no, stupid teacher didn't teach my class shit.
Ok, I'll try my best to explain what a linking table is in this medium (it's quite a difficult concept to grasp without face to face interaction).
Consider three students - 'A', 'B' and 'C'
and two classes - 'ENG' and 'METHODS'
A student can be enrolled in one or both classes, and both classes can have as many students as they want.
This would imply two tables, a 'STUDENT' table and a 'CLASS' table, which would contain the information above:
mysql> SELECT * FROM student;
+------------+------------------+
| STUDENT_CD | STUDENT_NAME |
+------------+------------------+
| A | ABA JONES |
| B | BOB THE BUILDER |
| C | CHARLIE BROWN |
+------------+------------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM class;
+--------------+----------------------+
| CLASS_CD | CLASS_NAME |
+--------------+----------------------+
| ENG | ENGLISH |
| METHODS | MATHEMATICAL METHODS |
+--------------+----------------------+
2 rows in set (0.00 sec)
mysql>
Now, consider how you would implement the fact that a) a student can have many classes and b) a class can have many students. Note that we do not know *exactly* how many people a class will have with certainty, so that rules out adding extra fields (studentA, studentB ...studentn) and making it a foreign key. The same is true for the reverse.
This is also bad normalisation (well-forming a database) practice.
Nor can we have a class foreign key in the Student table and have multiple Student records the more classes they are in as this would violate the primary key's uniqueness.
If we set both the Class foreign key in the Student table AND the Student's ID as a composite primary key, this will work (and vice versa). However we will run the risk of what are known as data redundancy errors. Take this for example:
Suppose student A were enrolled in both ENG and METHODS. We have added a composite primary key field to the Student table 'Class_cd' which is also a foreign key to the Class table. The table should now look like this:
mysql> SELECT * FROM student WHERE student_cd = 'A';
+------------+------------------+--------------+
| STUDENT_CD | STUDENT_NAME | CLASS_CD |
+------------+------------------+--------------+
| A | ABA JONES | ENG |
| A | ABA JONES | METHODS |
+------------+------------------+--------------+
2 rows in set (0.00 sec)
mysql>
I now want to change student A's name as they've become married. While doing a blanket 'change name to 'ABA RODGERS' against all student As, we run the risk of a program or database user inadvertently changing just the one. Now we have inconsistent data.
The 'best practice' solution is similar to the above, but to create a NEW table to achieve it. This is known as the
linking table. This is simply a table that has foreign keys from Class and Student, but both combine to be a composite primary key (to ensure uniqueness between each 'instance' of a Student's participation in a Class).
Going back to the tables as they were before:
mysql> SELECT * FROM student;
+------------+------------------+
| STUDENT_CD | STUDENT_NAME |
+------------+------------------+
| A | ABA JONES |
| B | BOB THE BUILDER |
| C | CHARLIE BROWN |
+------------+------------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM class;
+--------------+----------------------+
| CLASS_CD | CLASS_NAME |
+--------------+----------------------+
| ENG | ENGLISH |
| METHODS | MATHEMATICAL METHODS |
+--------------+----------------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM class_instance;
+------------+--------------+
| STUDENT_CD | CLASS_CD |
+------------+--------------+
| A | ENG |
| A | METHODS |
+------------+--------------+
Relationship:

The use of the linking table will allow the database to remain consistent whilst expressing the fact that a Student may have many classes, and a Class may have many Students. The data (as in, if you wanted to retrieve the class listing with proper student details) can be re-retrieved by using a concept known as 'joins'. The Student table can be joined onto the Class_Instance table where Class_Cd = 'ENG', for example.