Spring Boot Backend Development Course, Database, What is a Database

Welcome to the Spring Boot Backend Development Course. One of the topics of this course is ‘Databases’. When developing web applications, databases are essential, and all data must be stored and managed. In this post, we will start with the basic concepts of databases, and then explore various types, database design, integration with Spring Boot, and how to use SQL in detail.

1. Definition of a Database

A database is a system that helps to store and manage information structurally. Users can perform various tasks such as entering, modifying, deleting, and querying data. Databases are generally needed for efficient storage, retrieval, management, and protection of data. Through databases, multiple users can access data simultaneously, ensuring data integrity.

1.1 Necessity of Databases

Databases are necessary for many reasons. These include:

  • Structured Data Management: Databases provide a structure for organizing and efficiently managing data.
  • Data Integrity: Databases ensure the consistency and accuracy of data.
  • Concurrency Management: They allow multiple users to access and modify data simultaneously.
  • Data Security: Databases protect data by setting access controls and user permissions.

2. Types of Databases

Databases can primarily be classified as follows:

2.1 Relational Databases (RDBMS)

Relational databases store data in tabular form. Each table consists of rows and columns, and their relationships are defined by foreign keys. Notable relational database management systems (RDBMS) include MySQL, PostgreSQL, and Oracle.

2.2 Non-relational Databases (NoSQL)

Non-relational databases are suitable for storing and managing unstructured data. They can store data in formats such as JSON, XML, documents, and key-value pairs, with a flexible schema. Representative non-relational databases include MongoDB, Cassandra, and Redis.

2.3 Graph Databases

Graph databases are optimized for modeling relationships between data. They use nodes and edges to store data and can visually represent complex relationships. Neo4j is a representative example of a graph database.

3. Database Design

Database design is essential for building an efficient database. The database design process includes requirement analysis, conceptual design, logical design, and physical design.

3.1 Requirement Analysis

The first stage of database design is requirement analysis. Here, we identify the information needed by the users and the business requirements.

3.2 Conceptual Design

In the conceptual design stage, the conceptual structure of the data is defined through an ER diagram. This involves identifying ‘entities’, ‘attributes’, and ‘relationships’.

3.3 Logical Design

In the logical design stage, the conceptual design is transformed into a data model (typically a relational model). At this stage, the schema of the database is determined.

3.4 Physical Design

Finally, in the physical design stage, the logical design is specified according to the actual database system. This process involves optimizations considering performance, including indexing and partitioning.

4. Integration of Spring Boot and Databases

Spring Boot provides various features that make it easy to integrate with databases. The process of connecting to a database using Spring Boot includes dependency management, database configuration, defining entity classes, and creating repository interfaces.

4.1 Dependency Management

To communicate with a database in Spring Boot, appropriate dependencies must be added. If you are using Maven, the following dependencies should be added to the `pom.xml` file:



    org.springframework.boot
    spring-boot-starter-data-jpa


    com.h2database
    h2
    runtime


4.2 Database Configuration

To configure the database in Spring Boot, the `application.properties` file must be modified. Here is an example of H2 database configuration:


spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update

4.3 Defining Entity Classes

Entity classes must be defined in Spring Boot that map to database tables. For example, a `User` entity that stores user information could be defined as follows:


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String email;

    // getters and setters
}

4.4 Creating Repository Interfaces

Through the Repository interface, you can interact with the database. The `UserRepository` is defined as follows:


import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository {
}

5. How to Use SQL

SQL (Structured Query Language) is the standard language used to interact with databases. With SQL, you can query, insert, modify, and delete data. The basic SQL syntax is as follows:

5.1 Data Query: SELECT


SELECT * FROM users; 

5.2 Data Insertion: INSERT


INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com'); 

5.3 Data Modification: UPDATE


UPDATE users SET email = 'john.new@example.com' WHERE id = 1; 

5.4 Data Deletion: DELETE


DELETE FROM users WHERE id = 1; 

Conclusion

In this post, we explored the basic concepts of databases, their types, design methods, and integration with Spring Boot. Since databases play a crucial role in backend development, it is important to have a fundamental understanding and practical use of them. In future courses, we plan to cover a variety of topics related to databases, so please look forward to it.