1) Yes, if a foreign key from Project to Department is meanigful and valid then it should be added.
2) Presumably each Investigator is
either a Professor
or a Student? That explains why your "both ways" query returned no rows, since it will only return rows for Investigators who are both. If you wanted all Investigators you need an OR rather than an AND:
Code:
select e.dept_name, count(a.pro_id)
from project a, pro_assign b , investigator c , student d , department e, professor f, dept_assign g
where a.pro_id= b.pro_id
and b.inv_id = c.inv_id
and ( (c.inv_id = d.stu_id and d.dept_id = e.dept_id)
or (c.inv_id = f.prof_id and f.prof_id = g.prof_id and g.dept_id = e.dept_id)
)
(As an aside, it helps if you apply some formatting to your SQL. It would also be better to use mnemonic table aliases like "d" or "dept" for "department", rather than just running through the alphabet!)
Which query is the "proper" way to link the tables depends on what question you are trying to answer: they are all valid, but answer different questions:
1) How many projects are there per department, associated with investigators who are
students from that department?
2) How many projects are there per department, associated with investigators who are
both students and professors from that department? (A: 0 rows)
3) How many projects are there per department, associated with investigators who are
professors from that department?
4) How many projects are there per department, associated with investigators who are
either students or professors from that department?
Write the SQL that answers the question you need answered!