I'm a bit confused on what a many to many relationship is. I'm wondering if the following is a many to many relationship:
A student at a school has many clubs. A club at a school has many students. Let's say that the student has many attributes: firstname, lastname, phone, age, email, etc. A club only has one attribute: a name.
When I make a new club, I want to be able to give the club a name and one or more students. Upon making the club, I want that club to be associated with those students and those students to be associated with that club.
When I make a new student, I want to be able to give the student a firstname, last name, etc, and one or more clubs. Upon making the student, I want that student to be associated with those clubs and those clubs to be associated with that student.
I also want to display a club's students and a student's clubs on their show pages.
I've read that a many to many relationship is when you have a join table that lets you access common attributes of the resulting students and clubs, but there are no common attributes in my case.
Do I have a many to many relationship here? If so, do I use a HABTM or has_many, through relationship?
Actually yes you DO have common attributes.
You stated yourself that a Student has many Clubs
And a Club has many Students.
What is in common? Students and Clubs.
What now follows is to define what a Student and a Club actually are, which you already did.
A Student is a combination of firstname, last name, etc... What you have not specified is what makes a Student UNIQUE. A club also must be defined as to what will make it UNIQUE. While for academic purposes, you could say the name is what makes it unique, in real live, that would probably not be the best solution.
Usually for performance purposes, each student is given a unique Autoincrement ID (which is a number).
Same thing can be done with the Club.
You create a 3rd table which is what creates the Many to Many relation.
In that 3rd table, you have 2 columns. One with the Unique Index for the Student, and the other column with the Unique Index for the Club. You simply add an entry on that table in which you wish to relate a student to a club.
Since you can have many students assigned to the same club, and you can have many clubs assigned to the same student, you have a many to many relation.
Edit: As mentioned in another answer, your 3rd table should also declare the combined indexes as unique, so that you don't add the same entry multiple times.
You have a many to many
Create an id for each table that is unique for that table typically an auto incrementing int.
Then a third table that is a junction/intersect table call it X.
Put a row in X with the student id and club id if the student has the club and vice versa. It would have a unique composite key in table X across both id's in it.
The composite would guarantee no duplicate rows in X
Yes indeed there is a many-to-many relationship here, use HABTM. Also, why do you say that there are no attributes in common? Club names and student names are definitely common attributes in this case.
Related
I am very comfortable with associations but have come across a bit of a unique situation.
I am pulling data from an API. Two models. One is golf clubs, the other golf courses. A club can have multiple courses, and courses belong to a club.
The API already identifies the Club with a unique ID and a unique ID for the club.
How do I set the association so I basically tell it: "Use these pre-existing ID's for your association" rather than generating a unique ID on it's own like it would for a typical association?
Thank you!
There are four requirement!
There are schools, students, and clubs.
Every student attends one and only one school; every school has more than one student.
Every student may optionally join one club; every club has more than one student.
Every club belongs to one school; some schools have more than one club,
some schools have none.
Any student may work as an assistants for only one school; every
school may have one assistant at most.
How to draw the 4th requirement in my picture
It's called relation you will insert a middle relation. So for example
(student)0,1 ---> (assistant) <----0,1(School)
So the trick is to create a new entity. And you throw those relation. A student may be or not be an assistant (0,1 means that) and a school may have or not have one assistant.
I have these entities in ER model from my project:
Student
Professor
Subject
Should I connect them with ternary relationship, or each one with binary relationships? I used ternary relationship. Maybe i don't have the best possible name for the relationship, but it involves grading.
And also if i should use ternary relationship which Ids from connected tables should i use as primary key for the table which will represent relationship in database
Based on your diagram, it seems that each Student/Subject combination determines a Professor. Let's compare this with binary relationships:
A binary relationship between Student and Subject would allow you to record which Students are taking which Subjects, without specifying which Professor. Is that valid for your system, or is it necessary to capture the Professor for every Student/Subject combination?
A binary relationship between Student and Professor would record a Professor for each Student, or possibly a set of of Professors for each Student. Is this useful without knowing which subject each Professor teaches to the Student?
A binary relationship between Subject and Professor would record one or more Professors for each subject. That could be useful to capture what a Professor is able or qualified to teach, as opposed to what they're assigned or scheduled to teach.
I've worked on two school administration systems, and we captured (Student PK, Subject PK) (subjects taken by students) and (Professor PK, Subject PK) (subjects that can be taught be professors), but instead of a ternary relationship (Student PK, Subject PK, Professor), we defined a subject group or section Sections (SectionID PK, Subject, Professor) and student section allocation (Student PK, Subject PK, SectionID). Subject groups or sections provide a convenient place to attach further attributes, like Room number, timetabling arrangements, tuition language, etc.
Lets say a relationship is described as:
A company has many employees
A company has many departments
A department has many employees
So, something like this;
Company -<< Departments >>- Employees
If department table has a basic structure of:
// Pseduocode
company_id // Foreign key
department_id // Primary key
[employees] // Array or collection of employees
If we go back to this phrase;
A Company has many employees
Does this mean that the employee table needs or requires a reference for the company_id too?
So, Employee would be:
employee_id
company_id // I'm not sure if this is requried or not
department_id
I'm intending to hopefully abstract this data into a contracts table in case of employees are freelancers/contracters, etc or have multiple employees.
But for now..
My question is:
Does my employee table require a reference to the company table, or is the company reference implied via the department table?
Many thanks
Question is: do you need a straight connection between Company and Employee? If you do, add it, if not then yes, the connection is implied through Department.
EDIT:
Technically, your Department table does not need a list of Employees. Each row of Employee table has a reference ID to Department, that's enough.
Check this out for more information.
The relationship is implied through the department, so you don't conceptually need it. Adding it, would be an example of denormalisation, and would allow inconsistencies to crop up. For example, you might have company_1 with department_1 and company_2. Now employee_1 might be linked to department_1 but company_2, because of some flaw/bug in the application code. There's no way to express this constraint as a SQL schema, so you'd have to go with more complicated stuff such as triggers, or application code checks etc.
However, sometimes you only need info about companies and employees, but not about departments. If it's really performance critical, doing the extra join with departments in order to find the company for an employee or the employees for a company might not cut it, so you'll just have to live with the denormalisation.
You probably do not need to link Company with Employee. The relation between Departement and Company already do the job. You may need it only if there are existing particular cases requiring this relationship.
So I have this small problem, I am trying to make an ER Diagram and I have a student. For the student entity it says:
Record which school a student is in. A student must be attending one school. All schools contain at least one student.
You are to record the following information schools: their names(must be unique), # of students attending, and the principal name.
When I read this, it seems like "school" is a composite attribute or an entirely separate entity. Since it says "A student must be attending one school. All schools contain at least one student" should I make it an entity because I can't think of how to show this relationship via a composite attribute.
As I see it, school is a separate entity. A composite attribute would be something like a 'name' which consists of a 'first name' and a 'last name'. The school only consists of school and student only consists of student in your example.