Student management system database: ER diagram and SQL

A student management system database in 7 tables: teachers, classes, students, courses, enrollments, grades and attendance. The ER diagram, what each table holds, and the SQL Server script, ready for a DBMS project or a real app and open to change in ERDsmith.

  • SQL Server
  • 7 tables
  • 7 relationships
ER diagram of the student database: dbo.Teacher with dbo.Class and dbo.Course, dbo.Student in a class, dbo.Enrollment between students and courses, and dbo.Grade and dbo.Attendance under each enrollment, with their keys and relationships.
Drawn by ERDsmith from the script below. Open it to move tables, add columns or pick the IDEF1X look preset.

The tables

Seven tables in three groups: the people and their classes, the courses students take each year, and what each course records.

  • dbo.TeacherOne row per teacher, found by employee number.
  • dbo.ClassA class such as 10A, each name once, with its class teacher.
  • dbo.StudentOne row per student, found by student number, in exactly one class.
  • dbo.CourseSubjects by code, with their credits and the teacher who gives them.
  • dbo.EnrollmentA student taking a course in a school year.
  • dbo.GradeThe final mark of an enrollment, at most one, and the day it was given.
  • dbo.AttendanceOne mark per enrollment per school day: present, absent or late.

How the tables relate

  • A class has zero or more students , and each student is in exactly one class: ClassId is NOT NULL, so the line has nothing at the class’s end .
  • A teacher leads zero or more classes and gives zero or more courses.
  • Students and courses are many-to-many through dbo.Enrollment, one row per student, course and school year.
  • An enrollment has zero or one grade : the grade’s primary key is the enrollment’s key, so its line is solid and its box has round corners .
  • An enrollment has zero or more attendance rows, keyed by (EnrollmentId, AttendedOn), a composite key.

Why it is built this way

A course once per school year

The alternate key (StudentId, CourseId, SchoolYear) on dbo.Enrollment refuses the same student in the same course twice in one year with Msg 2627. A student who repeats the course next year gets a new row with the next year’s number. The enrollment still has an id of its own, a surrogate key, so grades and attendance carry one column instead of three.

One final grade, from 0 to 100

dbo.Grade is keyed by EnrollmentId, so a second final grade for the same enrollment fails with Msg 2627, and the check CK_Grade_Mark refuses a mark of 120 with Msg 547. A course not graded yet has no row. A student’s report card for one school year, with a NULL mark where the grade is still missing:

SELECT c.Code, c.Title, g.Mark
FROM dbo.Enrollment AS e
JOIN dbo.Course AS c ON c.CourseId = e.CourseId
LEFT JOIN dbo.Grade AS g ON g.EnrollmentId = e.EnrollmentId
WHERE e.StudentId = @StudentId
  AND e.SchoolYear = @SchoolYear;

One attendance mark a day

The primary key (EnrollmentId, AttendedOn) keeps one mark per course per day: marking the same student absent after marking them present fails with Msg 2627. Correcting a mark is an update of that row. The status is P, A or L, held by a check.

Every student is in a class

ClassId is NOT NULL, so a student without a class fails with Msg 515. The student number is an alternate key, the number on the student’s ID card, while StudentId is the key the other tables carry.

The SQL Server script

student.sql · 99 linesDownload
-- Table [dbo].[Teacher]
CREATE TABLE [dbo].[Teacher] (
    [TeacherId]      INT            IDENTITY (1, 1) NOT NULL,
    [EmployeeNumber] VARCHAR (20)   NOT NULL,
    [FullName]       NVARCHAR (200) NOT NULL,
    [Email]          NVARCHAR (254) NULL,
    CONSTRAINT [PK_Teacher] PRIMARY KEY CLUSTERED ([TeacherId] ASC),
    CONSTRAINT [AK_Teacher_EmployeeNumber] UNIQUE NONCLUSTERED ([EmployeeNumber] ASC)
);


GO

-- Table [dbo].[Class]
CREATE TABLE [dbo].[Class] (
    [ClassId]   INT          IDENTITY (1, 1) NOT NULL,
    [Name]      VARCHAR (10) NOT NULL,
    [TeacherId] INT          NOT NULL,
    CONSTRAINT [PK_Class] PRIMARY KEY CLUSTERED ([ClassId] ASC),
    CONSTRAINT [FK_Class_Teacher_TeacherId] FOREIGN KEY ([TeacherId]) REFERENCES [dbo].[Teacher] ([TeacherId]),
    CONSTRAINT [AK_Class_Name] UNIQUE NONCLUSTERED ([Name] ASC)
);


GO

-- Table [dbo].[Course]
CREATE TABLE [dbo].[Course] (
    [CourseId]  INT            IDENTITY (1, 1) NOT NULL,
    [Code]      VARCHAR (10)   NOT NULL,
    [Title]     NVARCHAR (200) NOT NULL,
    [Credits]   TINYINT        NOT NULL,
    [TeacherId] INT            NOT NULL,
    CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED ([CourseId] ASC),
    CONSTRAINT [CK_Course_Credits] CHECK (Credits > 0),
    CONSTRAINT [FK_Course_Teacher_TeacherId] FOREIGN KEY ([TeacherId]) REFERENCES [dbo].[Teacher] ([TeacherId]),
    CONSTRAINT [AK_Course_Code] UNIQUE NONCLUSTERED ([Code] ASC)
);


GO

-- Table [dbo].[Student]
CREATE TABLE [dbo].[Student] (
    [StudentId]     INT            IDENTITY (1, 1) NOT NULL,
    [StudentNumber] VARCHAR (20)   NOT NULL,
    [FullName]      NVARCHAR (200) NOT NULL,
    [BirthDate]     DATE           NOT NULL,
    [Email]         NVARCHAR (254) NULL,
    [ClassId]       INT            NOT NULL,
    CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED ([StudentId] ASC),
    CONSTRAINT [FK_Student_Class_ClassId] FOREIGN KEY ([ClassId]) REFERENCES [dbo].[Class] ([ClassId]),
    CONSTRAINT [AK_Student_StudentNumber] UNIQUE NONCLUSTERED ([StudentNumber] ASC)
);


GO

-- Table [dbo].[Enrollment]
CREATE TABLE [dbo].[Enrollment] (
    [EnrollmentId] INT      IDENTITY (1, 1) NOT NULL,
    [StudentId]    INT      NOT NULL,
    [CourseId]     INT      NOT NULL,
    [SchoolYear]   SMALLINT NOT NULL,
    [EnrolledOn]   DATE     NOT NULL,
    CONSTRAINT [PK_Enrollment] PRIMARY KEY CLUSTERED ([EnrollmentId] ASC),
    CONSTRAINT [FK_Enrollment_Course_CourseId] FOREIGN KEY ([CourseId]) REFERENCES [dbo].[Course] ([CourseId]),
    CONSTRAINT [FK_Enrollment_Student_StudentId] FOREIGN KEY ([StudentId]) REFERENCES [dbo].[Student] ([StudentId]),
    CONSTRAINT [AK_Enrollment_StudentId_CourseId_SchoolYear] UNIQUE NONCLUSTERED ([StudentId] ASC, [CourseId] ASC, [SchoolYear] ASC)
);


GO

-- Table [dbo].[Attendance]
CREATE TABLE [dbo].[Attendance] (
    [EnrollmentId] INT      NOT NULL,
    [AttendedOn]   DATE     NOT NULL,
    [Status]       CHAR (1) NOT NULL,
    CONSTRAINT [PK_Attendance] PRIMARY KEY CLUSTERED ([EnrollmentId] ASC, [AttendedOn] ASC),
    CONSTRAINT [CK_Attendance_Status] CHECK (Status IN ('P', 'A', 'L')),
    CONSTRAINT [FK_Attendance_Enrollment_EnrollmentId] FOREIGN KEY ([EnrollmentId]) REFERENCES [dbo].[Enrollment] ([EnrollmentId])
);


GO

-- Table [dbo].[Grade]
CREATE TABLE [dbo].[Grade] (
    [EnrollmentId] INT            NOT NULL,
    [Mark]         DECIMAL (5, 2) NOT NULL,
    [GradedOn]     DATE           NOT NULL,
    CONSTRAINT [PK_Grade] PRIMARY KEY CLUSTERED ([EnrollmentId] ASC),
    CONSTRAINT [CK_Grade_Mark] CHECK (Mark BETWEEN 0 AND 100),
    CONSTRAINT [FK_Grade_Enrollment_EnrollmentId] FOREIGN KEY ([EnrollmentId]) REFERENCES [dbo].[Enrollment] ([EnrollmentId])
);


GO

Questions

What tables does a student management system need?

Students, courses and enrollments at the least. Teachers, classes, grades and attendance come next. This design has all seven.

How do I work out a student's attendance?

Count the rows in dbo.Attendance for the student's enrollments in that year, and the ones with status P or L among them.

How is it different from a university database?

A school keeps classes, school years and daily attendance. A university has programs, terms, sections with a capacity, and prerequisites: see the university database template.

Can I change the design?

Yes. Open it in ERDsmith, free in the browser: add a table, rename a column, and the keys and lines follow.

Does it work with MySQL or PostgreSQL?

The script is written for SQL Server. The tables, keys and relationships carry over to other databases; the types and the IDENTITY columns need that database's own syntax.

More templates and reading