Hello,
You have a JOBS table which has events and different jobs
You have a STAFF table which has staffs that has names, etc ..
Now we have a relation between JOBS and STAFF, so your saying you have a ONE-to-MANY relation where a JOB has many STAFFS. So you have to implement a Relation Table.
We want to make sure a job has many staff, and one staff per job, so you have to make a relation ( where I will name employees) for some job. I will explain what I mean in a bit!.
You stated in your question that you want to link those two tables, and you want to know who have some jobs that has no staff. And you want to know who has some jobs that has 20 staff. A 'staffed' table could be used (or anything you want to call it) can do that, you just assign which STAFF_ID has which JOB_ID, that way you will NORMALIZE your table . Normalization is explained in the link I gave you below.
Initial Design
==============================
Existing Tables:
JOBS Table: jobID | jobName | jobDescription
-> where primarykey = jobID
STAFF Table: staffID | staffName
-> where primarykey = staffID
A New Table we will add:
EMPLOYEES Table: staffID | jobID
-> where primarykey = (staffID, jobID)
-> the reason why we want to make a group primary key cause we do not want duplicates of the same person having the same job, plus searching on that pk index is very quick.
Physical Design
==============================
CREATE TABLE jobs (
jobID int AUTO_INCREMENT,
jobName varchar 20,
jobDesc varchar 20,
PRIMARY KEY(jobID) );
INSERT INTO jobs VALUES (1, 'software developer', 'sql queries');
INSERT INTO jobs VALUES (2, 'buisness analyst', 'deals with businesss');
INSERT INTO jobs VALUES (3, 'secretary', 'deals with businesss');
CREATE TABLE staff (
staffID int AUTO_INCREMENT,
staffName varchar 20
PRIMARY KEY(jobID) );
INSERT INTO staff VALUES(1,'m0');
INSERT INTO staff VALUES(2,'Amanda);
INSERT INTO staff VALUES(3,'Mike');
INSERT INTO staff VALUES(4,'Joe');
CREATE TABLE staffed (
jobID int,
staffID int,
PRIMARY KEY(jobID,staffID),
FOREIGN KEY(jobID) REFERENCES jobs(jobID),
FOREIGN KEY(staffID) REFERENCES staff(staffID));
// m0 has job 1
INSERT INTO staffed VALUES(1,1);
// mike has job 1
INSERT INTO staffed VALUES(1,3);
// amanda has job 3
INSERT INTO staffed VALUES(3,2);
// joe has job 2
INSERT INTO staffed VALUES(2,4);
Discussion how to query this...
=============================
lets select all employees who has job1 ...
SELECT staffName FROM staff s
LEFT JOIN staffed st ON s.staffID = st.staffID
LEFT JOIN job j ON j.jobID = st.jobID
^^ Notice we are joining tables that ONLY have the relation where it meets in staffed table. When I did 'staffed st' it is temporarly renaming that table to st so I can present it better. So instead of doing JOIN staffed ON staff.staffID = staffed.staffID, i can do it in short form like; JOIN staffed st ON s.staffID = st.staffID
If you need more info, let us know, and we will be glad to help. I was a Database Teachers assistance during my undergrad :)-
Take a look at the following source to learn more about normalization