ryan_mortier — 2010-09-27T13:26:31-04:00 — #1
I'm making a database for safety training and come across something I can't quite figure out how to do considering best practice.
I have a table like so:
My question is I have several locations where the required field may or may not pertain to.
So should I setup the table like so:
I have about 15ish locations.
Not sure how to go about this.
I do have a separate table for the list of locations.
ryan_mortier — 2010-09-29T08:29:08-04:00 — #2
Alright, I think I understand now, so I should always start with one first.
One employee can have one job title. - Wrong
One job title can have many employees. - Right (Therefore the many table is the employee table)
Is this correct?
guido2004 — 2010-09-29T08:32:16-04:00 — #3
One employee can have one job title. - so jobs is the '1' table.
One job title can have many employees. - so employees is the 'many' table.
ryan_mortier — 2010-09-29T08:33:54-04:00 — #4
Got it, thanks for explaining it for me.
guido2004 — 2010-09-29T08:20:47-04:00 — #5
The first 'look' is the correct one You take one from a table, and see how many there can be in the other one.
So, the employee table is the 'many' one in this case.
ryan_mortier — 2010-09-29T08:17:27-04:00 — #6
I'm going to add onto this thread since I don't want to make a new thread for a (probably) silly question.
Question is: How do I determine which table is the many table in a many to one or one to many relationship?
Each employee can only hold one job title but each job title can be held by several employees (such as a receptionist position).
However, if we look at it this way:
Several employees can hold a job title but only one job title can be assigned to each employee.
I think I'm just getting my logic messed up a bit from over thinking this.
I would think the job title table would be the many table?
Sorry, I'm new to this but eager to learn.
ryan_mortier — 2010-09-27T13:29:10-04:00 — #7
Actually, I just thought of another way to do this.
I could make a new table called course_required like so:
Is this a better way of doing this?
ryan_mortier — 2010-09-27T13:40:03-04:00 — #8
Correct, the required field would typically be a checkbox for yes/no.
I'm still trying to figure out how to link that properly to the course table though.
guido2004 — 2010-09-27T14:18:04-04:00 — #9
rguy84 — 2010-09-27T13:36:35-04:00 — #10
so you have say 5 different courses, and 15 locations (I assume you mean like buildings on a campus, or different towns). Not each town needs every course...
A table like what you have in post 2 would be best. I would have req_id, course_id, and branch_id. I assume your required field meant yes it is required?
rguy84 — 2010-09-27T14:14:18-04:00 — #11
ryan_mortier — 2010-09-27T14:13:38-04:00 — #12
Oh, I understand now. Perfect, thank you.
guido2004 — 2010-09-27T14:10:56-04:00 — #13
You don't need a required field in the course_required table, unless you also want to store non required courses in that table
Edit: what Ryan says
rguy84 — 2010-09-27T14:09:55-04:00 — #14
Read my post again. Course_required holds IDs of a course and location where that course is REQUIRED in that location, so all the rows would say yes cause Yes course 101 is required at location a, that is why it would be in a table listing the location with the required courses or in diffferent words all the courses with the required locations listed...
rguy84 — 2010-09-27T13:48:33-04:00 — #15
Stop and think. The course-required table holds the courses REQUIRED by a location, wouldn't a field asking if course is required be redundant?
ryan_mortier — 2010-09-27T13:53:23-04:00 — #16
edit: ^^ beaten
So I actually don't need required in the courses table.
It would link like so:
and the select statement might look like:
SELECT name FROM courses
INNER JOIN course_required ON course.id = course_id
INNER JOIN locations ON location_id = locations.id
WHERE required = yes;
Am I doing this right?