I'm afraid I don't quite understand what peace95 is talking about.
I don't see any place in raghavshukla's post where he even mentioned any primary key violation.
raghavshukla: The real problem is that you should *NEVER NEVER NEVER* have a delimited list in a single DB field.
Also, I do *NOT* see that you have a need for a many-to-many table here. It's just a one-to-many, but it's a bit deceiving.
Example:
Code:
Table: Employees
empid :: empname
1 :: adam
2 :: bob
3 :: candy
4 :: doug
5 :: earl
6 :: fran
Table: Subordinates
empid :: sub_empid
3 :: 2
3 :: 5
2 :: 1
2 :: 4
5 :: 6
That is:
-- Candy manages both Bob and Earl
-- Bob manages both Adam and Doug
-- Earl manages Fran
See? Simple as that.
NOW...
Now the REAL problem comes when you need to build an oranization tree from a set of tables like that.
The easiest way to do this is to know, ahead of time, the maximum "depth" or your tree.
Here, our depth does not exceed 2 (that is, two levels of subordinates).
And we can build the tree thus:
Code:
SELECT 3 AS level, empid, empname
FROM employees WHERE empid NOT IN (
SELECT empid FROM subordinates )
UNION
SELECT 2 AS level, empid, empname
FROM employees WHERE empid IN (
SELECT empid FROM subordinates
WHERE empid NOT IN (
SELECT empid FROM subordinates )
)
UNION
SELECT 1 AS level, empid, empname
FROM employees WHERE empid IN (
SELECT empid FROM subordinates
WHERE empid IN (
SELECT empid FROM subordinates )
)
But... But you can see that such a query can get really really clumsy pretty quickly.
If you were using SQL Server, you *could* use a recursive stored procedure. But it's not clear to me that's a good idea even when it's possible.
A better way, I think, is to rethink the entire design. Use the same kind of table one might use for, say, a threaded forum. [This forum is *NOT* threaded...it's a simple flat system.]