This project is a simple Maven + SQLite project for practicing basic database functionality.
You will work with two tables:
coursesstudents
The students table has a foreign key that connects each student to a course.
students.course_id → courses.course_id
This means every student must belong to an existing course.
Your project should look like this:
student-db-test/
│
├── pom.xml
├── README.md
│
├── student.db
│
└── src/
└── main/
├── java/
│ └── com/
│ └── example/
│ └── DatabaseDemo.java
│
└── resources/
├── create.sql
├── insert.sql
└── query.sql
The file student.db is the SQLite database file. It is created automatically when the Java program runs.
This file creates the database tables.
This file inserts data into the tables.
This file is used to practice database queries.
Run the program by right-clicking on DatabaseDemo class and choosing: Run DatabaseDemo.main()
The program will run these files in order:
create.sql
insert.sql
query.sql
The output will be printed in the terminal.
Complete the table creation.
Checklist:
- Create a
coursestable. - Create a
studentstable. - Add a 'primary key' to both tables.
- Add a 'foreign key' from
students.course_idtocourses.course_id. - Make student 'name' required.
- Make student 'email' required and unique.
- Make student 'age' 18 or older.
- Make course 'name' required.
- Make course 'credits' greater than 0.
Insert data into the tables.
Checklist:
- Insert course data first.
- Insert 3 courses.
- Insert 5 students.
- Make sure all students are at least 18 years old.
- Make sure all emails are unique.
- Make sure every student uses a valid course ID.
Practice SQL queries.
Checklist:
- Select all courses.
- Select all students.
- Select students older than 20.
- Show students with their course names using
JOIN. - Count students in each course.
- Update one student's age.
- Update one student's course.
- Delete one student.
- Show the final result.
Every time the program runs, create.sql drops and recreates the tables.
This means old data is removed and fresh data is inserted again.
This means you tried to insert a student with a course_id that does not exist in the courses table.
Fix it by using a valid course_id.
This means you inserted an email that already exists.
Fix it by using a different email.
This means you inserted an invalid value.
For example, student age must be 18 or older.
This means a required value was missing.
For example, student name and email cannot be null.
By completing this project, you will learn how to:
- create database tables
- use primary keys
- use foreign keys
- insert data
- select data
- join two tables
- update records
- delete records