相对MySQL数据库

时间:2012-08-07 01:16:20

标签: php mysql database

我有一个MySQL数据库表,其构建如下。 User database 类下面的数字表示该类的“级别”,例如高,中或低,0表示不参加该类。

我需要构建另一个表来保存每个类的作业分配,但是我对于如何构建该表有点迷失,特别是表示行的确切列必须是什么,因为每个类没有特定的东西表示它。

1 个答案:

答案 0 :(得分:1)

正确规范化的结构将有一个单独的课程表,给每个人一个id,另一个表通过包括学生ID,课程ID和级别将学生放入课程。

CREATE TABLE students (
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(64) NOT NULL,
  password VARCHAR(64), 
  email VARCHAR(...)
  UNIQUE KEY (username)
);

CREATE TABLE courses (
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(64) NOT NULL
);

/* If a record doesn't exist for a course and student, 
   the student isn't enrolled in that course. Otherwise,
   the level is defined here */
CREATE TABLE enrollments (
  id INT NOT NULL PRIMARY KEY,
  student_id INT NOT NULL,
  course_id INT NOT NULL,
  level INT NOT NULL,
  /* Each student may be enrolled only once per course */
  UNIQUE KEY (student_id, course_id),
  FOREIGN KEY (student_id) REFERENCES students (id),
  FOREIGN KEY (course_id) REFERENCES courses (id)
);

最后,您可以为每个课程中分配的作业创建一个表格:

CREATE TABLE assignments (
  id INT NOT NULL PRIMARY KEY,
  course_id INT NOT NULL,
  description TEXT
  /*... other columns related to the assignment as necessary*/
);

如果有必要,学生可以完成作业:

CREATE TABLE student_assignments (
  student_id INT NOT NULL,
  assignment_id INT NOT NULL,
  assignment_body TEXT, /* or whatever... */
  /* Or to track when completed */
  submitted_timestamp TIMESTAMP,
  PRIMARY KEY (student_id, assignment_id),
  FOREIGN KEY (assignment_id) REFERENCES assignments (id),
  FOREIGN KEY (student_id) REFERENCES students (id)
);