University database: ER diagram and SQL

A university database in 9 tables: departments, degree programs, instructors, courses and their prerequisites, terms, sections, students and enrollments. 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
  • 9 tables
  • 11 relationships
ER diagram of the university database: dbo.Department with dbo.Program, dbo.Instructor and dbo.Course, dbo.CoursePrerequisite linking dbo.Course to itself, dbo.Term, dbo.Section, dbo.Student and dbo.Enrollment between sections and students, 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

Nine tables in four groups: the departments and what they run, the course catalogue, the timetable, and the students.

  • dbo.DepartmentDepartments by code, such as CS.
  • dbo.ProgramA degree program, such as a BSc in Computer Science, run by one department.
  • dbo.InstructorOne row per instructor, found by employee number, in one department.
  • dbo.CourseCourses by code, with their credits, each owned by one department.
  • dbo.CoursePrerequisiteWhich course must come before which; its key is the pair.
  • dbo.TermA term by code, such as 2026FA, with its start and end dates.
  • dbo.SectionOne offering of a course in a term, taught by one instructor, with a capacity.
  • dbo.StudentOne row per student, found by student number, admitted to one program.
  • dbo.EnrollmentA student in a section, with the letter grade once it is given.

How the tables relate

  • A department has zero or more programs , instructors and courses, and each of those belongs to exactly one department: its DepartmentId is NOT NULL, so the line has nothing at the department’s end .
  • Courses are many-to-many with themselves: dbo.CoursePrerequisite references dbo.Course twice, through CourseId and PrerequisiteId, and both columns form its primary key. Both lines are solid and the box has round corners .
  • A course has zero or more sections, and each section is in one term with one instructor.
  • Sections and students are many-to-many: dbo.Enrollment holds one row per pair, keyed by both columns, a composite key.
  • A student belongs to exactly one program; a program has zero or more students.

Why it is built this way

A course and its sections are separate tables

The catalogue describes CS201 once: its title, credits and prerequisites. Each term the course is taught, it gets sections, and a student enrolls in a section, not in the course. The alternate key (CourseId, TermId, SectionCode) refuses a second section 01 of CS201 in the same term with Msg 2627.

A student takes a section once

The primary key (SectionId, StudentId) on dbo.Enrollment refuses the same student in the same section twice with Msg 2627, and an enrollment in a section that doesn’t exist fails with Msg 547, the foreign key’s referential integrity check. Grade stays NULL until the grade is given; W marks a student who withdrew.

A course can’t be its own prerequisite

The check CK_CoursePrerequisite_PrerequisiteId refuses a course as its own prerequisite with Msg 547. A check sees one row, so a longer loop, CS201 before CS101 and CS101 before CS201, passes; the application looks for loops when a prerequisite is added. The schema doesn’t hold a student back from a course whose prerequisites they haven’t passed either: the application checks that at enrollment.

Capacity is a number the application enforces

Nothing in the schema refuses the 41st student in a section of 40; the application counts before it enrolls. The seats left in each section of a term, not counting students who withdrew:

SELECT c.Code, s.SectionCode,
    s.Capacity - COUNT(e.StudentId) AS SeatsLeft
FROM dbo.Section AS s
JOIN dbo.Course AS c ON c.CourseId = s.CourseId
LEFT JOIN dbo.Enrollment AS e
    ON e.SectionId = s.SectionId
    AND (e.Grade IS NULL OR e.Grade <> 'W')
WHERE s.TermId = @TermId
GROUP BY c.Code, s.SectionCode, s.Capacity;

The SQL Server script

university.sql · 134 linesDownload
-- Table [dbo].[Department]
CREATE TABLE [dbo].[Department] (
    [DepartmentId] INT            IDENTITY (1, 1) NOT NULL,
    [Code]         VARCHAR (10)   NOT NULL,
    [Name]         NVARCHAR (100) NOT NULL,
    CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED ([DepartmentId] ASC),
    CONSTRAINT [AK_Department_Code] UNIQUE NONCLUSTERED ([Code] ASC)
);


GO

-- Table [dbo].[Term]
CREATE TABLE [dbo].[Term] (
    [TermId]   INT          IDENTITY (1, 1) NOT NULL,
    [Code]     VARCHAR (10) NOT NULL,
    [StartsOn] DATE         NOT NULL,
    [EndsOn]   DATE         NOT NULL,
    CONSTRAINT [PK_Term] PRIMARY KEY CLUSTERED ([TermId] ASC),
    CONSTRAINT [CK_Term_EndsOn] CHECK (EndsOn > StartsOn),
    CONSTRAINT [AK_Term_Code] UNIQUE NONCLUSTERED ([Code] 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,
    [DepartmentId] INT            NOT NULL,
    CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED ([CourseId] ASC),
    CONSTRAINT [CK_Course_Credits] CHECK (Credits > 0),
    CONSTRAINT [FK_Course_Department_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [dbo].[Department] ([DepartmentId]),
    CONSTRAINT [AK_Course_Code] UNIQUE NONCLUSTERED ([Code] ASC)
);


GO

-- Table [dbo].[Instructor]
CREATE TABLE [dbo].[Instructor] (
    [InstructorId]   INT            IDENTITY (1, 1) NOT NULL,
    [EmployeeNumber] VARCHAR (20)   NOT NULL,
    [FullName]       NVARCHAR (200) NOT NULL,
    [DepartmentId]   INT            NOT NULL,
    CONSTRAINT [PK_Instructor] PRIMARY KEY CLUSTERED ([InstructorId] ASC),
    CONSTRAINT [FK_Instructor_Department_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [dbo].[Department] ([DepartmentId]),
    CONSTRAINT [AK_Instructor_EmployeeNumber] UNIQUE NONCLUSTERED ([EmployeeNumber] ASC)
);


GO

-- Table [dbo].[Program]
CREATE TABLE [dbo].[Program] (
    [ProgramId]    INT            IDENTITY (1, 1) NOT NULL,
    [Code]         VARCHAR (10)   NOT NULL,
    [Name]         NVARCHAR (200) NOT NULL,
    [Degree]       VARCHAR (10)   NOT NULL,
    [DepartmentId] INT            NOT NULL,
    CONSTRAINT [PK_Program] PRIMARY KEY CLUSTERED ([ProgramId] ASC),
    CONSTRAINT [CK_Program_Degree] CHECK (Degree IN ('BSc', 'BA', 'MSc', 'MA', 'PhD')),
    CONSTRAINT [FK_Program_Department_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [dbo].[Department] ([DepartmentId]),
    CONSTRAINT [AK_Program_Code] UNIQUE NONCLUSTERED ([Code] ASC)
);


GO

-- Table [dbo].[CoursePrerequisite]
CREATE TABLE [dbo].[CoursePrerequisite] (
    [CourseId]       INT NOT NULL,
    [PrerequisiteId] INT NOT NULL,
    CONSTRAINT [PK_CoursePrerequisite] PRIMARY KEY CLUSTERED ([CourseId] ASC, [PrerequisiteId] ASC),
    CONSTRAINT [CK_CoursePrerequisite_PrerequisiteId] CHECK (PrerequisiteId <> CourseId),
    CONSTRAINT [FK_CoursePrerequisite_Course_CourseId] FOREIGN KEY ([CourseId]) REFERENCES [dbo].[Course] ([CourseId]),
    CONSTRAINT [FK_CoursePrerequisite_Course_PrerequisiteId] FOREIGN KEY ([PrerequisiteId]) REFERENCES [dbo].[Course] ([CourseId])
);


GO

-- Table [dbo].[Section]
CREATE TABLE [dbo].[Section] (
    [SectionId]    INT         IDENTITY (1, 1) NOT NULL,
    [CourseId]     INT         NOT NULL,
    [TermId]       INT         NOT NULL,
    [SectionCode]  VARCHAR (5) NOT NULL,
    [InstructorId] INT         NOT NULL,
    [Capacity]     SMALLINT    NOT NULL,
    CONSTRAINT [PK_Section] PRIMARY KEY CLUSTERED ([SectionId] ASC),
    CONSTRAINT [CK_Section_Capacity] CHECK (Capacity > 0),
    CONSTRAINT [FK_Section_Course_CourseId] FOREIGN KEY ([CourseId]) REFERENCES [dbo].[Course] ([CourseId]),
    CONSTRAINT [FK_Section_Instructor_InstructorId] FOREIGN KEY ([InstructorId]) REFERENCES [dbo].[Instructor] ([InstructorId]),
    CONSTRAINT [FK_Section_Term_TermId] FOREIGN KEY ([TermId]) REFERENCES [dbo].[Term] ([TermId]),
    CONSTRAINT [AK_Section_CourseId_TermId_SectionCode] UNIQUE NONCLUSTERED ([CourseId] ASC, [TermId] ASC, [SectionCode] 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,
    [Email]         NVARCHAR (254) NOT NULL,
    [ProgramId]     INT            NOT NULL,
    [AdmittedOn]    DATE           NOT NULL,
    CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED ([StudentId] ASC),
    CONSTRAINT [FK_Student_Program_ProgramId] FOREIGN KEY ([ProgramId]) REFERENCES [dbo].[Program] ([ProgramId]),
    CONSTRAINT [AK_Student_StudentNumber] UNIQUE NONCLUSTERED ([StudentNumber] ASC)
);


GO

-- Table [dbo].[Enrollment]
CREATE TABLE [dbo].[Enrollment] (
    [SectionId]  INT      NOT NULL,
    [StudentId]  INT      NOT NULL,
    [EnrolledOn] DATE     NOT NULL,
    [Grade]      CHAR (1) NULL,
    CONSTRAINT [PK_Enrollment] PRIMARY KEY CLUSTERED ([SectionId] ASC, [StudentId] ASC),
    CONSTRAINT [CK_Enrollment_Grade] CHECK (Grade IN ('A', 'B', 'C', 'D', 'F', 'W')),
    CONSTRAINT [FK_Enrollment_Section_SectionId] FOREIGN KEY ([SectionId]) REFERENCES [dbo].[Section] ([SectionId]),
    CONSTRAINT [FK_Enrollment_Student_StudentId] FOREIGN KEY ([StudentId]) REFERENCES [dbo].[Student] ([StudentId])
);


GO

Questions

What tables does a university database need?

Students, courses, instructors and enrollments at the least. Departments, programs, prerequisites, terms and sections come next. This design has all nine.

Why does a student enroll in a section, not a course?

A course runs in many terms, often in more than one section a term, each with its own instructor and capacity. The grade belongs to the section the student sat.

How is it different from a student management system?

A school database keeps classes, school years and daily attendance; see the student management system template. A university database adds programs, terms, sections and prerequisites.

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