Table Relations sql, php - mysql

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

Related

How to design a simple database

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

Access 2013 multi value field

I'm creating a table for a college major. The table is called major. The columns will be majorID, majorName, and requiredCourses.
In Access how can I make requiredCourses a multivalue field? Required courses will be around 20 courses.
Thank you for your help.
You need to create a one-to-many relationship. The way is is usually done is like this:
You need to create a new table for the courses. Call it course. The table will contain CourseID, CourseName, etc. CourceID will be a primary key of this table
You will need to create another table that will act as a link between your major and your course tables. The table can be called something like majorCourses. The table will contain at least these two fields: majorID and courseID (you can of course add more fields, like dateAdded, isInactive, etc.).
To link your tables you will need to JOIN these tables, like this:
SELECT m.majorID, m.majorName, c.courseID, c.CourseName
FROM major m
INNER JOIN (majorCourses mc INNER JOIN course c ON mc.courseID = c.courseID)
ON m.majorID = mc.majorID

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.

Database: Tables design/structure

I am new to database structure and design. Currently, I am in the process of creating a course catalog that will match course description according to course name and date. I have sketched one table describing the courses, which include course_code, name and every other relevant information. Then I sketched another table linking those courses to when they will be taught.
I am missing classes that are classified as all_year. Also I am missing a way how to label the courses under a major. Since hypothetically a course can belong to several majors, putting the data from one into the other would force you to duplicate data. Any ideas how I would implement this two things to my tables design? Or suggestion in how to restructure my design. If possible please show me a query to execute in my phpmyadmin DB.
Example of table courses
id serial
course_code text
description text
Example of table course_dates
id serial
course_id serial
year date
semester
Example of table majors
major_id int
course_id int
So a populated database could contain the following:
Table courses
id course_code description
1 INF1000 "Basic programming"
2 INF1001 "More basic programming"
Table course_dates (0 for spring 1 for fall)
id course_id year semester
1 1 2012 0
2 1 2013 1
3 2 2013 1
To link courses to majors - this is a one to many relationship (one course to many majors) - you want to use a linking table that has this type of structure:
table courses_majors
major_id int
course_id int
Remember to index this table as well - its very important. Then you can populate it and have one course even go to many majors and many course to one major (many to many relationship).
Then you can run a join on the tables across this table:
select * from courses left join courses_majors on courses.id = courses_majors.course_id left join majors on courses_majors.majors_id = majors.id
Of course you can add a where clause, etc.
The other way is to create a table of majors:
majors
id int
name varchar
Then add a major_id to your courses table - this will just give you a one to one relationship from courses to majors, but many courses can join a major.
As for Yearly, I would just add a field in the database to account for this, probably a tiny int and just make it 0 or 1.

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.