Spring Boot Backend Development Course, What is RDB

Today, we will explore relational databases (RDB) that are essential to understand when doing backend development using Spring Boot. Databases are one of the most important parts of software projects, and RDB is widely used globally as a database model. In this article, we will explain the concepts, characteristics, and integration methods of RDB in detail.

1. What is a Relational Database (RDB)?

A relational database is a type of database that organizes and stores data in tabular form. Each table consists of rows and columns, and data can be linked through the relationships between tables. RDB uses Structured Query Language (SQL) to define and manipulate data.

1.1 History of RDB

In the early 1960s, Edgar F. Codd proposed the relational model for databases, laying the foundation for RDB. In the 1970s, IBM developed a relational database system based on SQL, leading to the evolution of RDBMS (Relational Database Management System). Today, various RDBMS such as MySQL, PostgreSQL, Oracle Database, and Microsoft SQL Server are in use.

1.2 Components of RDB

  • Table: The basic unit where data is stored. Each table contains data related to a specific subject.
  • Row: Represents each data record in the table. Each row corresponds to specific data.
  • Column: Defines the attributes belonging to each row. For example, in a user table, ‘name’, ’email’, etc. can be defined as columns.
  • Primary Key: A single column or a combination of columns that uniquely identifies each row.
  • Foreign Key: A column that establishes a relationship between two tables. It references the primary key of another table.

2. Main Features of RDB

Relational databases have several features that have made RDB the standard for data management.

  • Data Integrity: Ensures the accuracy and consistency of data. For example, foreign key constraints can maintain the integrity of data relationships.
  • Normalization: The process of structuring data to minimize redundancy and maintain integrity.
  • ACID Properties: Guarantees the atomicity, consistency, isolation, and durability of transactions, enhancing database stability.
  • SQL Support: Data retrieval, insertion, modification, and deletion can be performed through SQL. SQL is the standard query language for relational databases.
  • Implementation of Business Rules: RDB allows the implementation of business rules at the database level. For example, constraints can be set to only allow data that meets specific conditions to be entered.

3. Spring Boot and RDB

Spring Boot is a Java-based framework that supports fast and easy application development. This framework also simplifies the integration with RDBMS. Here’s how to connect to an RDB using Spring Boot.

3.1 Adding Spring Boot Dependencies

When creating a Spring Boot project, you need to add dependencies suitable for the RDBMS you are using in the `pom.xml` or `build.gradle` file. For example, if you are using MySQL, you can add the following dependency.



    mysql
    mysql-connector-java
    8.0.26

3.2 Configuring the Data Source

In Spring Boot, you must configure the information for database connection in the application.properties file. Here is an example of configuring a MySQL database.


spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

3.3 Creating Entity Classes

Create entity classes that map to database tables. For example, you can define a User entity to store user information as follows.


import javax.persistence.*;

@Entity
@Table(name = "users")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "username")
    private String username;

    @Column(name = "email")
    private String email;

    // Getters and setters
}

3.4 Creating Repository Interfaces

To handle CRUD (Create, Read, Update, Delete) operations for the entities, you can define repository interfaces using Spring Data JPA.


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

public interface UserRepository extends JpaRepository {
    User findByUsername(String username);
}

3.5 Creating Services and Controllers

Create service classes to handle business logic and use them to create controllers that provide RESTful APIs. For example:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public List getAllUsers() {
        return userService.getAllUsers();
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        return userService.createUser(user);
    }

    // Other methods
}

4. Use Cases of RDB

Relational databases are used in various fields. Some key use cases include:

  • Security-related Data Recording: Used to store user personal information or transaction data safely and accurately.
  • eCommerce Sites: Efficiently manages various data such as products, customers, and orders by storing them in relational databases.
  • Financial Management: Ideal for structurally storing and managing tax, revenue, and expenditure information.
  • Medical Record Management: Effectively manages patient medical records and treatment histories.

5. Conclusion

RDB is very useful as a database management system that efficiently stores and manages data while maintaining data integrity and consistency. Using Spring Boot simplifies the integration with relational databases, allowing for rapid application development. I hope this course enhances your understanding of the concepts of RDB and its integration with Spring Boot.