Java tutorialLesson 16 of 18 · tap to see all lessons
Java tutorial · Beginner
Designing a Project Database in MySQL: Tables, Keys, Sample Data, and CRUD Practice
Design a Student Information Program database step by step: tables, column types, and keys, sample data, CRUD and JOIN practice, and getting ready for JDBC.
- Qyubie Learn Editorial Team
- Published
- 23 min read
Includes 4 exercises and 1 quick check you can do right here.
You now know the parts: tables, types, keys, and CRUD statements. This lesson puts them together on the Student Information Program, the project you're building in lab. You'll design its database from the requirements, create it, fill it with sample data, and practice the exact queries the Java program will need. By the end, the database is ready for lesson 19, where Java connects to it.
By the end of this lesson you will be able to:
- turn a short list of requirements into tables,
- choose columns, data types, and keys for each table,
- write the full setup script and load sample data,
- combine tables with
JOINand summarize them withGROUP BY, and - prepare a MySQL user and connection details for a Java program.
Step 1: Read the requirements
The program must:
- keep a record of each student: student number, name, course, and year level,
- keep the list of subjects offered: code, title, and units,
- enroll students in subjects for a semester, and
- record each student's final grade in each subject.
Step 2: Find the tables
The nouns in the requirements are candidates for tables. Here there are three: student, subject, and enrollment.
Why is enrollment its own table? One student takes many subjects, and one subject has many students. That's a many-to-many relationship, and a database models it with a linking table in between. Each enrollment row connects one student to one subject, and it's also the natural place for the grade, because a grade belongs to that student in that subject, not to either one alone.
students 1 ───< enrollments >─── 1 subjectsRead it as: one student has many enrollments, and one subject has many enrollments.
Quick check
Step 3: Choose columns, types, and keys
| Table | Column | Type | Rule |
|---|---|---|---|
students | id | INT AUTO_INCREMENT | primary key |
student_no | VARCHAR(10) | required, unique | |
last_name, first_name | VARCHAR(50) | required | |
course | VARCHAR(10) | required | |
year_level | INT | default 1 | |
subjects | code | VARCHAR(10) | primary key (natural key) |
title | VARCHAR(100) | required | |
units | INT | required | |
enrollments | id | INT AUTO_INCREMENT | primary key |
student_id | INT | foreign key → students(id) | |
subject_code | VARCHAR(10) | foreign key → subjects(code) | |
semester | VARCHAR(20) | required, like '2025-2026 1st' | |
grade | DECIMAL(3,2) | may be empty until the grade is in |
Two design decisions worth noticing:
- The foreign key columns have the same type as the keys they point to (
INTtoINT,VARCHAR(10)toVARCHAR(10)). gradeallowsNULL, because a student is enrolled before the grade exists.NULLmeans "no value yet", which is different from 0.
A student shouldn't be enrolled in the same subject twice in one semester. A composite unique rule over three columns enforces that: UNIQUE (student_id, subject_code, semester).
Step 4: The setup script
Here is the whole script. In lab, save it as sis_db.sql and run it in MySQL Workbench; you can run it again any time to start fresh, because it drops the old tables first. Press Run to try it here.
Try it · SQL
The order matters. Tables with foreign keys are dropped first and created last, because a table can't point to one that doesn't exist yet, and a table can't be dropped while others still point to it. The same goes for the data: students and subjects are inserted before the enrollments that refer to them.
Sample data should include awkward cases too, like Ben's missing PE 2 grade. Those are the rows that reveal bugs.
Step 5: Practice the queries the program needs
The exercises below all start with the database from Step 4 already set up.
Reading from two tables with JOIN
The enrollments table only holds IDs, so a report that shows names needs data from more than one table. A JOIN combines rows from two tables where a condition matches, usually foreign key = primary key:
Try it · SQL
enrollments egives the table a short alias,e, so you can writee.gradeinstead ofenrollments.grade.ON sub.code = e.subject_codesays which rows belong together: each enrollment is matched with its subject.- When two tables have a column with the same name, prefix it with the alias so SQL knows which one you mean.
Exercise · SQL
CS 122 class list
List the last_name, first_name, and grade of every student enrolled in CS 122, sorted by last_name. Join enrollments with students.
Hint
Match each enrollment to its student with ON s.id = e.student_id. Then WHERE e.subject_code = 'CS 122' ORDER BY s.last_name.
Show a solution
Try it yourself first — you learn more from a wrong attempt than from reading an answer.
SELECT s.last_name, s.first_name, e.grade FROM enrollments e JOIN students s ON s.id = e.student_id WHERE e.subject_code = 'CS 122' ORDER BY s.last_name;
Summaries with GROUP BY
GROUP BY puts rows with the same value into groups, and an aggregate function then gives one result per group:
Try it · SQL
COUNT(*) counts rows, while COUNT(grade) counts only rows that have a grade, which tells you how many grades are still missing.
Exercise · SQL
Units per student
Show each student's student_id and their total units (name the column total_units), sorted by student_id. Units are in subjects, so join it with enrollments, then group by student_id.
Hint
JOIN subjects sub ON sub.code = e.subject_code, then GROUP BY e.student_id ORDER BY e.student_id.
Show a solution
Try it yourself first — you learn more from a wrong attempt than from reading an answer.
SELECT e.student_id, SUM(sub.units) AS total_units FROM enrollments e JOIN subjects sub ON sub.code = e.subject_code GROUP BY e.student_id ORDER BY e.student_id;
Updating and deleting
Exercise · SQL
Encode a missing grade
Ben (student_id 2) now has a PE 2 grade of 1.50. Update only that enrollment, then show subject_code and grade of all of Ben's enrollments, sorted by subject_code.
Hint
Both conditions are needed in the WHERE: student_id = 2 AND subject_code = 'PE 2'. With only student_id = 2, Ben's CS 122 grade would change too.
Show a solution
Try it yourself first — you learn more from a wrong attempt than from reading an answer.
UPDATE enrollments SET grade = 1.50 WHERE student_id = 2 AND subject_code = 'PE 2'; SELECT subject_code, grade FROM enrollments WHERE student_id = 2 ORDER BY subject_code;
What about removing a student? The foreign key won't let you delete a student who still has enrollments:
Try it · SQL
To remove Cara, first delete her enrollments, then her row. Add the two DELETE statements in the right order:
Exercise · SQL
Remove a student
Remove Cara (student id 3) from the database: delete her enrollments first, then her students row. Finish with the SELECT shown, which lists the remaining students.
Hint
DELETE FROM enrollments WHERE student_id = 3; then DELETE FROM students WHERE id = 3;
Show a solution
Try it yourself first — you learn more from a wrong attempt than from reading an answer.
DELETE FROM enrollments WHERE student_id = 3; DELETE FROM students WHERE id = 3; SELECT student_no, first_name FROM students ORDER BY id;
Step 6: Prepare for Java
In lesson 19, your Java program will connect to sis_db. Get these ready now.
1. A database user for the application. Don't let the program log in as root. Create a user that can only work with this one database. Run this in MySQL Workbench, using your own password:
CREATE USER 'sis_app'@'localhost' IDENTIFIED BY 'choose-a-strong-password';
GRANT SELECT, INSERT, UPDATE, DELETE ON sis_db.* TO 'sis_app'@'localhost';The app can read and change rows but can't drop tables, so a bug can't destroy the database structure.
2. The connection details. Java needs five facts:
| Setting | Value |
|---|---|
| host | localhost |
| port | 3306 |
| database | sis_db |
| user | sis_app |
| password | the one you chose |
In JDBC they become a URL plus a user name and password: jdbc:mysql://localhost:3306/sis_db.
3. A Java class for each table. Each row will become an object. You already know how to write these classes:
class Student {
private int id;
private String studentNo;
private String lastName;
private String firstName;
private String course;
private int yearLevel;
Student(int id, String studentNo, String lastName, String firstName, String course, int yearLevel) {
this.id = id;
this.studentNo = studentNo;
this.lastName = lastName;
this.firstName = firstName;
this.course = course;
this.yearLevel = yearLevel;
}
public int getId() { return id; }
public String getStudentNo() { return studentNo; }
public String getFullName() { return firstName + " " + lastName; }
public String getCourse() { return course; }
public int getYearLevel() { return yearLevel; }
}Columns in snake_case (last_name) become fields in camelCase (lastName), and SQL types map to Java types as in lesson 14.
4. The queries. Every query you practiced above becomes a method in Java: "list students", "find a student by number", "update a grade", "delete a student". Keep them in a file as you test them in Workbench; you'll paste them into Java code in lesson 19.
Common mistakes
- A grade column in
studentsorsubjects. In a many-to-many relationship, data about the pair belongs in the linking table. - Foreign key columns with a different type than the key they point to.
- Creating or inserting in the wrong order. Parent tables and rows first; drop child tables first.
- Forgetting the
ONcondition in aJOIN. Without it, every row is paired with every row of the other table. - No sample data for the awkward cases, such as a missing grade, so bugs stay hidden until real users find them.
- Connecting the application as
root.
Summary
- Turn requirements into tables by finding the nouns; a many-to-many relationship needs a linking table.
- Pick types and keys for every column. Foreign keys match the type of the key they point to.
- A setup script drops and creates tables in a safe order and loads sample data, including awkward cases.
JOIN ... ONcombines related tables;GROUP BYwithCOUNT,SUM, orAVGsummarizes them.- Before connecting Java, create a limited database user and note the host, port, database, user, and password.
That completes the database unit. Next lesson: inheritance, polymorphism, abstract classes, and interfaces.