Hi everyone
Hopefully I can describe this issue properly. For ease I'm going to use a hypothetical situation. Lets say I'm working on a program that tracks the number of times 2 employees have worked together.
Here's a week example:
Mon - Tom, Bob, Al
Tue - Tom, Al, Cindy
Wed - Al, Cindy
Thur - Tom, Bob
Fri - Bob, Al
I have a table called Employees:
Employees
id (pk)
Name
+----+-------+
| id | name |
+----+-------+
| 1 | Tom |
+----+-------+
| 2 | Bob |
+----+-------+
| 3 | Al |
+----+-------+
| 4 | Cindy |
+----+-------+
My question is what is the best design to create a many to many relationship on this one table? Here are my thoughts:
Create a reference table which includes a column that increments each time those employees work together:
Employee_Reference (could probably use a better name)
Emp_id_1 (fk)
Emp_id_2 (fk)
Count
Emp_id_1 and Emp_id_2 would both reference the employees id. The problem I see with this is that the table represents the exact same data, thus being redundant:
+----------+----------+-------+
| Emp_id_1 | Emp_id_2 | Count |
+----------+----------+-------+
| 1 | 2 | 2 |
+----------+----------+-------+
| 2 | 1 | 2 |
+----------+----------+-------+
My other thought was to use the exact same reference table layout, but place some restrictions in my business logic where emp_id_1 will always be the smaller of the 2 id fields. That would reduce the redundancy.
I realize that i could have another table where i actually have the work shifts, and I could write a query to group and count records etc, but I really have no need for the specific work shift data, and by having the count in it's own table, will increase performance for me as I'll be querying this table quite often.