guys I've been trying to create create a school results management system.There is a STUDENT table and many COURSE tables. Each student must be linked to all Courses tables. e.g JOHN, MARY,PETER must partake in MATHEMATICS, ENGLISH, ELECTROPHYSICS, PROGRAMMING etc
My problem is how to define this relationship in mysql and at same time let these students fetch only their own results in all the courses.
I don't know if you have a requirement to have each course in a separate COURSE table but that doesn't make much sense. You should have a STUDENT table and a COURSES table. Then, you will need a STUDENT_COURSES table to show which courses a student has taken or must take. Your table structure should be something like:
STUDENT: Id, Name, Gender, etc
STUDENT_COURSES: Id, Student_Id, Course_Id, Taken, Grade
COURSES: Id, Course_Name
When a new student is created, you would need to add records to the STUDENT_COURSES table to insert the new STUDENT.Id into the Student_Id field and the Course_Id for each of the required courses. The default value for Taken would be false and the Grade would be Null or Empty String.
This is a very simplistic example and would not work in a real world solution because you would need to have Instructors, Locations, Etc. to make anything useful. But hopefully this will help you get started.
Related
I am developing a 'online students discussion platform' for a certain university, i want a student to post a question to only students belonging in the same faculty so that if ICT student posts a question, the system extracts all the students in ICT faculty from the students table, attaches them to the post and inserts them into the table receivers.
The system is extracting all students from the students table but it is inserting only one student in the table receivers.
TABLES:
-students(regno,name,faculty_code) PRIMARY KEY regn
-receivers(id, regno,message_id)
how to do it?
Instead of having a table for receivers, a better approach would be for you to have a "faculty_code" field in the "message" table which would be populated with the faculty_code of the poster. So for each student, extract messages where student.faculty_code is equal to message.faculty_code.
That way a student only sees messages posted by students in the same faculty as him/her.
Okay so im new to databases, and have created a site with a users table, and i also hace a list table, where suers can insert list items, however when they log in everyones list is appearing, how can i link the user table to the lists table, is it creating the same field in each one and using a foreign key? Sorry I am very new to this. Appreciate any help
I think you can just use user_id on both tables to fix this. Let me give an example:
Table A (user_id, username, password)
Table B (list_item_id, user_id , any_other_attribute)
When you design your tables like this a simple sql call will do what you need like:
SELECT 'list_item_id','any_other_attribute' FROM Table B Where user_id=$user_id
Where $user_id is the user_id of the one's who loginned your system.
Also by your question, i suggest you to read about these : 'sessions' , 'sql queries' , 'generating sql query results' on your choice of programming language.
It calls MANY MANY relationnship. There mus be 1 table with fields user_id and field_id that will join this 2 tables
i have a table which consists of student details like roll no, Name, age. Now i have a table for storing the students who have attended and not attended on the given day and given class. I will be using a form with checklist and names automatically displayed using the student details table, i.e. All the name name and roll no will get displayed with a checkbox to each name. If it checked, it mean the particular student has attended the class and if not checked, then the student didn't attended. These detail should get update in the attendance table. How to do it using php and MySQL?
Use a many to many relation in MySQL.
You have a database table with students, like:
Table classes
Student (primary)
StudentName
StudentAge
(further info)
Create a database table for the Classes with they can attend, like:
Table classes
ClassID (primary)
ClassName
ClassDate
(further info)
Then create a many to many table, like:
Table attendances
ClassID (primary)
StudentID (primary)
To get the students that attended the class do the following query:
<?php
$lstrQuery = "SELECT `students`.* FROM `students`
INNER JOIN `attendances` ON `attendances`.`StudentID` = `students`.`StudentID`
AND `attendances`.`ClassID` = {YOUR_CLASS_ID} ";
$loResult = mysql_query($lstrQuery);
while($laRow = mysql_fetch_assoc($loResult))
{
// do whatever you like
}
?>
Is this what you had in mind?
I currently have two tables in which stores the attendances of a student in a course. I have the hub_attendance table which stores the total attendances of a student and the hub_attendance_lesson where it stores the attendance of each lesson that a student has or has not attended. I'm not sure if this is correct or if I'm doing anything wrong, I'm a beginner in databases!
hub_attendance:
id
student_id
course_id
total_lessons
total_lessons_so_far
total_attended
total_absent
total_excused_absent
total_late
total_excused_late
hub_attendance_lesson:
id
lesson_id
course_id
student_id
date
attended
absent
excused_absent
late
excused_late
EDIT:
So I've gotten rid of the first table completely and this is my new single table.
Hub_Attendance:
id
lesson_id
course_id
student_id
date
attendance
As Dutchie432 said, you don't need the first table because it introduces unnecessary redundancy and you can count those statistics on the fly. Such aggregate tables can be a good solution if performance is an issue, but they should be used only as a last resort.
About the second table - you have separate fields attended, absent, excused_absent,
late and excused_late. Aren't these mutually exclusive? So only one of them can be true for one row? If so, you may be better off with one enumeration field called for example attendance, which would take different values for each of those states. In that way you could't have rows where none of the flags, or more than one flag, is set.
Here's what you need:
**Course**
id, name, etc...
**Lesson**
id, courseid, name, etc...
**Attendance**
id, studentid, lessonid, lateness, etc...
**Enrolment**
id, courseid, studentid, startdate, etc...
You need the enrolment table to know that students should be on a course even if they never turn up for lessons. The attendance table will allow you to have many students per lesson and many lessons per student. This is a many-to-many table. Any aggregation and counting can be done in SQL.
If I understand your schema correctly, Your first table can be totally eliminated. You should be able to fetch the totals using MySQL.
select count(id) as total_late from hub_attendance_lesson where late=true and student_id=TheUserId
Remove this first table, every total can be fetch using SQL :
SELECT count(id) AS absent
FROM hub_attendance_lesson
WHERE lesson_id = <your lesson id>
AND absent = <false / true>
I guess you'll be able to adapt this code for your needs.
I'm trying to record test/quiz scores in a database. What's the best method to do this when there might be a lot of tests and users?
These are some options I considered: should I create a new column for each quiz and row for users, or does this have its limitations? Might this be slow? Should i create a new row for each user & quiz? Should I stick to my original 'user' database and encode it in text?
Elaborating a little on the plan: JavaScript Quiz, submits score with AJAX, and a script sends it to the database. I'm new with php so i'm not sure about a good approach.
Any help would be greatly appreciated :) this is for a school science fair
I'd suggest 3 data tables in your database: students, tests, and scores.
Each student needs to have fields for an ID and whatever else (name, dob, etc) you want to record about them.
Tests should have fields for an ID and whatever else (name, date, weight, etc).
Scores should have the student ID, a test ID, and the score (any anything else).
This means you can query a student and join with the scores table to get all the student's scores. You can also join the test table these results to get labels put onto each score and calculate a grade based on scores and weight.
Alternately you can query for a test and join with the scores to get all the scores on a given test to get the class stats.
I would say create a database table, maybe one that lists all students(name, dob, student id), and then one for all tests(score, date, written by). Will only you access the db, or can your students access it too? If the latter is the case, you need to make sure the create accurate security or "views" to ensure the student can only see their own grades at a time (not everyone's).
Definitely do not create dynamic columns! (no column for each quiz). Also adding columns to user table (or generally any table) when they are not identifying the user(or generally any table item) is bad aproach...
This is pretty example of normalization, you should avoid storing any redundant rows. To do that you would create 3 tables and foreign keys to ensure scores are always referencing an existing user and quiz. E.g.:
users - id, nickname, name
quizzes - id, quizName, quizOtherData
scores - id, user_id (references users.id) , quiz_id , (ref. quizzes.id), score
And then add rows to scores table per user per quiz. Additionaly you could create UNIQUE key for columns user_id and quiz_id to disallow users to complete one quiz more times than one.
This will be fast and will not store redundant (unneeded extra) data.
To get results of quiz with id e.g. 4 and user info of people who's submitted this quiz, ordered from highest to lowest score, you would do query like:
SELECT users.*, scores.score
FROM scores RIGHT JOIN users ON(users.id=scores.user_id)
WHERE scores.quiz_id = 4
ORDER BY score DESC
Reason why I used RIGHT join here is because there might be users that didn't do this quiz, however every score always have an existing user&quiz (due to foreign keys
To get overall info of all users, quizes and scores you would do something like:
SELECT *
FROM quizzes
LEFT JOIN scores ON(quizzes.id=scores.quiz_id)
LEFT JOIN users ON(users.id=scores.user_id)
ORDER BY quizzes.id DESC, scores.score DESC, users.name ASC
BTW: If you are new to PHP (or anybody reading this), use PHP's PDO interface to communicate with your database :) AVOID functions like mysql_query, at least use mysqli_query, but for portability I would recommend stay with PDO.