简体   繁体   中英

What is the best way to organize class data in databases?

I'm making a bit of software for a school that contains all the students' grades and allows them to check on it, and all of that stuff. I have tables in a database for each of the four grades that contain the students and their info - except I also need to store class data, like the URLs for the class blogs. I was wondering what the best practices were for storing that data, because I can't place them in with all the students. Here are the tables in the database:

Teachers (stores teachers & their emails, passwords, etc)
Alpha (stores all the alpha kids)
Beta (stores all the beta kids)
Gamma (gamma kids)
Delta (delta kids)

Personally, I wouldn't have a table for EACH grade. Why? Because all the data is the same regardless of grade. What if you wanted to add a column like "date_student_graduted" or "age_of_student" ... You would have to update all 4 of those tables.

Instead, create 1 table that stores ALL students and have a column that identifies their grade.

If you want to now associate students with a particular class, you would create a relation between them by giving each student and class a particular id (you are probably already doing this).

You will create a column in the students database that says something like "class_id" and then put the class id the student is apart of.

Now, for example, if you wanted to select a class and all the students within that class, you would first select the class, and then have a loop that goes into the database and selects each student if their class_id matches the current class id.

//Pseudo code

//select all classes
$classes = mysql_query('SELECT * FROM classes');

foreach($classes as $class) {

  $class_id = $class['class_id'];

  $students = mysql_query("SELECT * FROM students where class_id='$class_id' ");

  echo "list of students in the class $class[name] <br>";

  foreach($students as $stu) {

    echo "$stu[name] <br>";

  }




}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM