ER model of school system - entity-relationship

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.

Related

Draw ER digram for similar case

1.Several students from different departments may participate one project. Each project has at least one student member.
2.Each student may or may not participate the projects, but one student can participate several projects at the same time.
In 1st statement, the relationship of student and project is M-1. But in the 2nd statement, the relationship of student and project is 1-M
If I want to express their relationship in a single ER digram, should I use the M-1 or 1-M? Or is my ER diagram wrong?
ER Relation

Entity Relationship diagram: composite attributes vs entity

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.

What is a many to many relationship?

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.

Core Data saving array and dictionaries - best practices

Below is an illustration of the kind of data I want to save in Core data. Every city has many schools , every school has many grades and every grade has many students and their details.
I have read a couple of things about Core data and have got it up and starting. But I'm not able to understand how to save an array in core data and is it a good way to do that in the similar case of the illustration?
If i want to save for a particular school an array of total students for that particular grade, would it be a good practice? If yes, is the method provided in this link good to follow?
EDIT : All cities, all schools and all students have same attributes. Whereas each grade has different attributes. So if there are data for 10 grades, there may be 10 types of array for grades.
Also, what if i have a one to many relation between school and students? IE depending on my login i decide whether i need to save school and grades or school and students. How would the relationship be now?
You should use core data with one to many relationship. This would be your entity structure.
UPDATE:
In case you have several grades with different attributes, you can define another entity "GradeType", which contains details of each grades
UPDATE 2:
Let me write down considerations in this scenario.
1. A city can have multiple schools in it, but a school can be only in one city (Branches will have different address ;) ).
2. A school may offer multiple subjects. same subject can be taught in multiple cities.
3. A school may contain multiple students while a student can be enrolled only in one school.
4. A student can register for multiple subjects, while same subject can be registered by multiple students.
5. There can be multiple grades possible for a subject.(lets say 4: A, B, C & D). Similarly, many subjects will follow the same grading system.(A in history, B in Geology etc).
6. A student can have multiple grades. However, the number of grades will be equal to number of subject he/she opted for.
Based on above consideration, this would be your dataModel.
Here Grades Entity will have entries like this:
grade A for physics is scored by these students.
grade A for biology is scored by these students.
…
…
grade B for physics is scored by these students.
grade B for biology is scored by these students.
…
… N So on
Let me know if more info needed.
Dont do it the way shown in that link. Create core data entities for each of them (city,school,grade,student). Add relationship between those entities (Eg: City ->> school which means one to many relationships). Check this link http://www.raywenderlich.com/14742/core-data-on-ios-5-tutorial-how-to-work-with-relations-and-predicates. Refer apple document https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coredata/articles/cdRelationships.html as well. Take your time with core data modelling. Hope it helps

Core Data one-to-many relation saving entity confusion

I am working on a project where I am using core data, but bit confused how to use it corectly.
I have two entities students and college
students has one-to-one relationship to college. That is every student will have one college.
college has one-to-many relationship to student. that is college can have lot's of students.
However, in my app I am letting the user to add the student first without any college info. So I am keeping the relationship to college optional. In a 2nd view, I let the user update the college information of a student.
Now I am while adding a new student I am just updating the basic field related to student (firs name, last name, age, etc.). Now in 2nd view while updating college info I am not sure what is the correct way to do it considering the relationship.
The first view pass the student object to 2nd view and I am updating it as follows:
College* college = (College*)[NSEntityDescription insertNewObjectForEntityForName:#"College" inManagedObjectContext:self.managedObjectContext];
college.name = #"name"
college.address = #"address"
[college addStudentObject:self.student];
Am I doing it correctly?
what should be the correct way:
Adding the college object in 2nd view and connecting the entity by adding the student object in the student relation of a college?
Retrieving the student object first then getting the empty college object from the college relation. Then updating the college object with data and then save the context.
I am not sure if the 2nd option make any sense but in case of first option, if I add a new college, the new college get connected with the student but the old college remain in the database as redundant data.
In this scenario, I would probably create another entity, StudentEnrollment with one to one relationship to both student and college. It would probably make it easier to manage student's courses, grades, etc as well as transfers to another college.

Resources