AI Hints
Home›Spring Boot Tutorial›Why a Database, The H2 Database and What JPA Is
Spring Boot · Lesson 46 of 120

Why a Database

Data that survives.

⏱ 2 min read▶ Video at 1:01:46🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

Our books live in a Java map, in memory. When the app stops, they're gone. Real apps keep their data in a database.

  • A map in memory
  • Gone on restart
  • Use a database

Memory and a database

Data in memory disappears when the app stops, and only one app can see it. A database keeps data safely on disk. Many apps, and many users, can use it at once. And it can search millions of rows very quickly.

In memory

  • Lost on restartOne app onlySlow to search big data

Database

  • Kept on diskShared safelyFast searching

Tables, rows, and columns

A relational database stores data in tables. A table is like a spreadsheet. Each column is one field, like title, or price. And each row is one record, like one book.

idtitleauthorprice
1DuneFrank Herbert9.99
2CirceMadeline Miller10.99
3The HobbitJ.R.R. Tolkien8.99

SQL

We talk to the database in a language called SQL. Create table makes a table, with typed columns. Insert adds a row. And select reads rows, with an optional where condition.

books.sqlsql
CREATE TABLE book (
    id     INT PRIMARY KEY,
    title  VARCHAR(100),
    price  DECIMAL(6,2)
);
INSERT INTO book VALUES (1, 'Dune', 9.99);
SELECT title, price FROM book WHERE price < 10;

Summary

A database keeps data safe on disk, in tables of rows and columns. We talk to it with SQL. Next, the H2 database.

Key takeaways
  • Tables
  • Rows and columns
  • SQL
Next up: The H2 Database — a database inside your app