MySQL + SQL · Lesson 117
School Database Project in MySQL
In this lesson: Complete school management database in MySQL — ER design, tables, keys, sample data and report queries. Use for projects.
The project
A school database is the classic DBMS mini project. It brings together everything: entities, primary and foreign keys, one-to-many and many-to-many relationships, and real queries like report cards and fee summaries. We will design it, create it, and query it end to end.
💡 Entities: students, classes, subjects, marks, fees — linked by keys into one clean database.
Designing the tables
- classes — id, class_name.
- students — id, name, class_id (FK → classes).
- subjects — id, subject_name.
- marks — id, student_id (FK), subject_id (FK), marks.
- fees — id, student_id (FK), amount, status.
One class has many students (one-to-many). Marks link students and subjects (many-to-many via the marks table).
Creating the schema
CREATE TABLE classes (
id INT PRIMARY KEY AUTO_INCREMENT,
class_name VARCHAR(20)
);
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
class_id INT,
FOREIGN KEY (class_id) REFERENCES classes(id)
);
CREATE TABLE subjects (
id INT PRIMARY KEY AUTO_INCREMENT,
subject_name VARCHAR(30)
);
CREATE TABLE marks (
id INT PRIMARY KEY AUTO_INCREMENT,
student_id INT, subject_id INT, marks INT,
FOREIGN KEY (student_id) REFERENCES students(id),
FOREIGN KEY (subject_id) REFERENCES subjects(id)
);
Sample data
INSERT INTO classes (class_name) VALUES ('Class 10');
INSERT INTO students (name, class_id) VALUES ('Aman', 1), ('Diya', 1);
INSERT INTO subjects (subject_name) VALUES ('Math'), ('Science');
INSERT INTO marks (student_id, subject_id, marks) VALUES
(1, 1, 88), (1, 2, 76), (2, 1, 95), (2, 2, 90);
Now we have a small but complete dataset to query.
Useful queries
-- students in a class SELECT s.name, c.class_name FROM students s JOIN classes c ON s.class_id = c.id; -- number of students per class SELECT c.class_name, COUNT(s.id) AS total FROM classes c LEFT JOIN students s ON s.class_id = c.id GROUP BY c.id; -- average marks per subject SELECT sub.subject_name, AVG(m.marks) AS avg_marks FROM marks m JOIN subjects sub ON m.subject_id = sub.id GROUP BY sub.id;
A report card query
SELECT s.name,
SUM(m.marks) AS total,
ROUND(AVG(m.marks), 2) AS average,
CASE WHEN AVG(m.marks) >= 90 THEN 'A'
WHEN AVG(m.marks) >= 75 THEN 'B'
ELSE 'C' END AS grade
FROM students s
JOIN marks m ON m.student_id = s.id
GROUP BY s.id, s.name;
+------+-------+---------+-------+
| name | total | average | grade |
+------+-------+---------+-------+
| Aman | 164 | 82.00 | B |
| Diya | 185 | 92.50 | A |
+------+-------+---------+-------+
Common mistakes
- Storing all subjects in the students table. Use a separate marks table.
- No foreign keys. Link tables so data stays consistent.
- Repeating class details per student. Keep them in the classes table.
- Forgetting LEFT JOIN for empty classes. Use it to count classes with no students.
Summary
- A school database uses students, classes, subjects, marks and fees.
- Link them with foreign keys (student → class, marks → student and subject).
- Store marks one row per student per subject.
- Query with JOIN, GROUP BY and CASE for real reports.
- It demonstrates all the core DBMS concepts in one project.
Next, build the report card query in depth, or try the library database project.
Frequently Asked Questions
How do I design a school database in MySQL?
Identify the main entities — students, classes, subjects, marks, fees — make each a table with a primary key, and link them with foreign keys. For example, students reference a class, and marks reference both a student and a subject.
What tables does a school database need?
A typical school database has students, classes, subjects, marks (or exams), and fees tables. Students link to classes, marks link students to subjects, and fees link to students. This covers enrolment, results and payments.
How do students and classes relate in a school database?
It is usually one-to-many: one class has many students, and each student belongs to one class. So the students table has a class_id foreign key pointing to the classes table.
How do I store marks in a school database?
Store one row per student per subject in a marks table, with student_id, subject_id and the marks. This normalized design makes it easy to total, average and grade, and to add subjects without changing the table.
Is a school database a good DBMS project?
Yes. A school database is a classic mini project because it uses primary and foreign keys, one-to-many and many-to-many relationships, and real queries like report cards and fee summaries. It demonstrates all the core DBMS concepts.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
