How to design a simple database - mysql

I want to model a student, teacher, class relationship. Every student is associated with one teacher (the teacher can have many students). There are only three classes. The way I think of this is that there are three tables:
Student Table -> (student_id, student_name, class_id)
Teacher Table -> (student_id, student_name, class_id)
Class Table -> (class_id, class_name)
I'm not sure how to show the student-teacher relationship within the tables. How would we know which teacher is assigned to which student?

This can be accomplished with some simple joins.
Assuming that you want to find all the students associated with a certain teacher, you would start off by grabbing the row for the teacher. You would then join in the classes that the teacher teaches. Finally, you would join in the students that are in those classes.
This is known as a many-to-many relationship, and is an important concept in databases.
select
t.student_name, -- I suspect this col might actually be named teacher_name
s.student_name,
from
-- Find the classes that a teacher teaches
teacher_table t join class_table c on (t.class_id=c.class_id)
-- Find the students in those classes
join student_table s on (s.class_id=c.class_id)
where
t.student_id = ? -- Again, I suspect this should be "teacher_id"

This is a few more tables than you want, but several of the .NET examples that Microsoft has created revolve around a similar relational database.
Here is a link to that database:
https://msdn.microsoft.com/en-us/library/bb399731(v=vs.100).aspx
In this example, the student and the teacher are both kept in the person table and are related to the course table through two different Joining tables . . student grade and course instructor.
And here is the Contoso University Schema with link:
https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

Related

mySQL: how to go along 3 different tables, referring to different column in each table but using primary keys

I'm trying to find out which schools had students that did not complete their exams in 2018. So I've got 3 tables set up being: ExamInfo, ExamEntry and Students. I'm going to try use the ExamInfo table to get information from the Students table though, I obviously only want the student information that did not complete their exam in 2018. Note: I'm looking for students that attended, though did not complete the exam, with this particular exam you can look at completed exam as passed exam.
Within ExamInfo I have the columns:
ExamInfo_Date --when exam took place, using to get year() condition
ExamInfo_ExamNo --unique student exam ID used to connect with other tables
ExamInfo_Completed --1 if completed, 0 if not.
...
Within ExamEntry I have the related columns:
ExamEntry_ExamNo --connected to ExamInfo table
ExamEntry_StudentId --unique studentId used to connect to Students table
ExamEntry_Date -- this is same as ExamInfo_Date if any relevance.
...
Within Students I have following columns:
Students_Id --this is related to ExamEntry_StudentId, PRIMARY KEY
Students_School --this is the school of which I wish to be my output.
...
I want my output to simply be a list of all schools that had students that did not complete their exams in 2018. Though my issue is with getting from the ExamInfo table, to finding the schools of which students did not complete their exam.
So far I've:
SELECT a.Students_School, YEAR(l.ExamInfo_Date), l.ExamInfo_Completed
FROM ExamInfo l ??JOIN?? Students a
WHERE YEAR(l.ExamInfo_Date) = 2018
AND l.ExamInfo_Completed = 0
;
I'm not even sure if going through the ExamEntry table is necessary. I'm sure I'm meant to use a join, though unsure of how to appropriately use it. Also, with my 3 different SELECT columns, I only wish for Students_School column to be output:
Students_School
---------------
Applederry
Barnet Boys
...
Clearly, you need a JOIN -- two in fact. Your table has exams, students, and a junction/association table that represents the many-to-many relationship between these entities.
So, I would expect the FROM clause to look like:
FROM ExamInfo e JOIN
ExamEntry ee
ON ee.ExamEntry_ExamNo = e.ExamNo JOIN
Students s
ON ee.ExamEntry_StudentId = s.Students_Id

Table Relations sql, php

I'm building a database driven management system in php. I've created two separate tables. One table has a list of student names and personal information. The other table is used to store notes. Example: If a student comes in to get advised, the adviser can create one or more notes for the student they're helping. What's the best way to link a note with a student id so the adviser can see the old notes and new notes for the student they're advising: foreign key, inner join, or full outer join?
u askin for this ?
student table : student_id ,student_name
notes table : note_id , student_id, Date, Note

MySQL Attendance System , Problems with joining tables, duplicate id's for 1 primary key

Ok So I am creating a Attendance System for a College and I am having trouble doing the tables in MySQL
The Problem I'm having is Grouping a group of students together for one class. So in MySQL
TABLE NAME: CourseGroup: courseGroup_id , student_id(FK), course_id(FK)
I want a number of students belonging to one class, this in hindsight will enable a teacher to undertake a register for that class group
In MySQL I have set the 'courseGroup_id as a unique field, however when I try to link 'courseGroup_id' in the Attendance table it only enables me to select 'student_id' and 'course_id'
Is it possible to select the courseGroup_id and display students for one class
My Attendance table looks like this:
Attendance: week_number, day, time, courseGroup_id, present
I want to be able to view all the students that belong to one class
I’m a bit lost with your terminology, you speak of “class id” but don’t reefer to that in your question – instead you use “course”.
I think the best option you can do here is create another table enabling a many to many relationship as many students can be registered to one or many courses. This type of table will enable a mapping between students and courses.
For example, if you create the mapping table “student_courses” with
student_id INT
course_id INT
You can then select students by course (joining the student table)
SELECT * from student_courses sc
INNER JOIN students s
ON s.student_id = sc.srudent_id
WHERE sc.course_id = ?CourseId
You can also reverse it and show the courses by for an individual course:
SELECT * from student_courses sc
INNER JOIN courses cs
ON cs.courset _id = sc.course_id
WHERE sc.student_id = ?StudentId
It is also worth pointing out that there should be two index on the student_courses, indexing both student_id and course_id for performance reasons.

Transition (many to many join) tables in MySQL; allowing for duplicate entries to link 1 column to multiple other columns in different table

Ok, so I am going to try and be specific as possible but my MySQL skills are pretty weak. So here is the situation:
I have 2 tables: Donor and Students. A donor can be linked to as many students as they want and each student can be linked to as many donors as donors want to "claim" them. So if I have Sally, a student, she can have Jim, a donor, and Jeff, a donor, be linked to her. So, I have all my students in one table and all my donors in another table. I need to put them together show the students name, id and the id of all the donors that the student is linked to.
Currently my tables are: Donor with DonorID, FirstName, LastName, DonorType, StreetAddress, etc. Then Students with: StudentID, FirstName, LastName and DonorID. However, that only allows me to link a student with one donor. So, I was thinking I need to make a transition table that would allow me to show the StudentID, FirstName(of student), LastName(of student), and the DonorID that "claims" that student and allow for me to duplicate StudentID and put a different DonorID in the 2nd, 3rd, 4th, so on and so on entries of the same student.
So, I guess my question is how do transition tables work in MySQL; I believe I am going to need to work with the JOIN function and join the two tables together but after reading about that on tizag.com I am even more confused. I have worked with Access where when you create a transition table you can pull the PK from each table and create a composite key using the two keys from the other tables but I am not quite sure how to do that in MySQL; does it work essentially the same and I should be pulling the PK from each table and linking them together in the 3rd, transition, table?
Marvin's right.
You need three tables to pull this off.
Donor
DonorID
FirstName
LastName
DonorType
StreetAddress
etc.
Student
StudentID
FirstName
LastName
etc.
Then you need Student_Donor. This is often called a join table, and implements the many-to-many relationship.
StudentID (PK) (FK to Student)
DonorID (PK) (FK to Donor)
DonorOrdinal
If StudentID = 5 has four donors with ID = 6,7,11,15, then you'l have these rows
StudentId DonorId DonorOrdinal
5 6 1
5 7 2
5 11 3
5 15 4
The DonorOrdinal column allows you to specify a student's primary donor and secondary donors. I can't tell from your question how important that is, but it's helpful to be able to order these things. Remember this: formally speaking, a SQL SELECT query returns rows in an unpredictable order unless you also specify ORDER BY.
If you want to display your students and their donors, you'll need this query:
SELECT s.StudentID, s.FirstName, s.LastName,
sd.DonorOrdinal,
d.DonorType, d.DonorID, d.FirstName, d.LastName
FROM student s
LEFT JOIN student_donor sd ON s.StudentID = sd.StudentID
LEFT JOIN donor d ON sd.DonorID = d.DonorID
ORDER BY s.StudentID, sd.DonorOrdinal, d.DonorID
This will show you all students (whether having donors or not per LEFT JOIN) in order of ID, then their donors in order of DonorOrdinal.
Yes, pretty much. What you are talking about is a Many to Many relationship. Your donor table should not include a reference to the student table (and vice versa). You'll need a new table (what you are calling a transition table) that contains the DonorId and StudentId. This can comprise your primary key or you can use another column for the primary key that is an auto_increment.

How do I join multiple tables in SQL whose objects will be modeled using inheritance?

For my university assignment I have to design some basic managment system for sicknesses and all for a school. I have decided to model some basic inheritance in the form of
Person --> Student
Person --> Staff
Person --> Guardian
Person (PersonID, FirstName, LastName)
Student (StudentID (Which references the PersonID), ... )
The reason i decided to do this as I modeled this first in UML and had the inheritance in this.
I have another table which stored Incidents which have both StudentID, StaffID and GuardianID. However I was wondering how I would create a join in mysql which would display all three inherited people's names?
e.g.
Student.FirstName Student.LastName, Staff.FirstName, Staff.LastName etc...
How would I do this?
Or am i doing this completely wrong this way?
Thanks in advance.
http://pastebin.com/m263dd7 - Link to my DDL for the tables.
I have no problem with the database design you've described. It's always a bit awkward to model inheritance in SQL, but you've used the least problematic solution.
Here's a query to answer your question about retrieving the names of a student and staff member for a given incident:
SELECT ps.FirstName, ps.LastName, pf.FirstName, pf.LastName
FROM Incidents i
JOIN Students s USING (student_id)
JOIN Persons ps ON (s.student_id = ps.person_id)
JOIN Staff f USING (staff_id)
JOIN Persons pf ON (f.staff_id = pf.person_id)
WHERE i.incident_id = ?;
I'm assuming the Incidents table looks includes columns such as:
CREATE TABLE Incidents (
incident_id SERIAL PRIMARY KEY,
student_id INT NOT NULL,
staff_id INT NOT NULL,
FOREIGN KEY (student_id) REFERENCES Students(student_id),
FOREIGN KEY (staff_id) REFERENCES Staff(staff_id)
);
Realistically, I'd expect some kind of many-to-many relationship between incidents and each of staff and students. Otherwise, an incident can involve only one student and one staff member?
This is not right. You should have a Person class, and other classes would determine that a certain Person is a Student, Staff, etc. What happens if you have a staff person that is also a student? What happens if the Student graduates?
It is a classic example of impedance mismatch between the relational model and the OO model.
You could have for example three tables:
PERSON
PersonId
LastName
FirstName
STUDENT
StudentId
PersonId
STAFF
StaffId
PersonId
I dont know what the real usage, but it is not a good idea to use inheritence just for reusability.
At first sight, having a Person class in a university management system (something similar) does not seem right.
It would be better you mention the objective/purpose of the assignment - what is is supposed to do.