首页 > 解决方案 > 如何创建具有多年历史的学校数据库?

问题描述

我正在制作一个模拟学校数据库,我在纸上有一个基本模型,但是当我考虑不同的日期以及如何创建学期/季度甚至年份之间的关系时,事情变得非常复杂,因为这样做没有意义在我的班级表中具有相同的班级,但日期不同。这会令人困惑,对吗?我对我的问题进行了快速的谷歌搜索,主要是为我的数据库找到了一种称为日期维度的方法。甚至不确定这是否是我需要的。

几乎我想要的只是组织/设置我的表之间的关系以存储多年的相似数据,即:具有相同 id 但日期不同的班级,不同的学生,仅在我需要特定年份或如果需要,所有年份。

ATM 我的架构是这样的。

students --table with all students
------------
student_id serial
first_name varchar(20)
last_name archer(20)

class -- bridge table 
------------
class_id  integer
class_name text
student_id integer
grade   varchar(2)

classes --table with all classes
------------
class_id serial
dept_id integer
class_name text

student_history --bridge table with a calander?
------------------
class_id integer
class_name text
grade    varchar(2)

标签: sqldatabasepostgresql

解决方案


我会这样:

-- this holds the students
create table student (id, name);

-- this holds the courses (for example, "Intro to CS", which is
-- offered every year)
 create table course (id, department_id, name);

-- this table shows what studends took which courses in which 
-- semesters, and what grade they got.  Because you have this, you
-- don't need a history table
create table enrollment (student_id, course_id, semester, grade)

如果您想存储诸如“谁教课”之类的内容,它可能会变得更加复杂,但这可以解决您的迫切需求。


推荐阅读