Skip to content
Bible, Lee, Data

Bible, Lee, Data

Faith, software, data, and everyday life.

SQLAll posts

Engineering/Database

How to Read SQL Execution Plans with EXPLAIN

When a SQL query is slow, the first instinct is often to add an index.That may solve the problem, but it may also miss the real cause. The query might already have a usable index that the optimizer decided not to use. The join order may be inefficient, the optimizer may have estimated the wrong number of rows, or the database may be sorting a large intermediate result.Consider the following quer..

Engineering/Database

How Subqueries and CTEs Work in SQL

As SQL queries become more complex, a single table is often not enough to produce the result we need.Suppose we want to answer the following question:Which orders have a total amount greater than the average order amount?Before identifying those orders, the database must first calculate the average.SELECT AVG(total_amount)FROM orders;It must then compare each order with that value.SQL allows us ..

Engineering/Database

Database Indexes and SQL Execution Plans

Suppose we have the following orders table:CREATE TABLE orders ( order_id BIGINT PRIMARY KEY, customer_id BIGINT NOT NULL, order_status VARCHAR(20) NOT NULL, ordered_at DATETIME NOT NULL, total_amount BIGINT NOT NULL);When the table contains only a few hundred rows, the following query returns almost immediately:SELECT *FROM ordersWHERE customer_id = 1001;The situation changes whe..

Engineering/Database

Why Does Database Normalization Matter?

Database normalization is often introduced through a short list of rules:First Normal Form: atomic valuesSecond Normal Form: remove partial dependenciesThird Normal Form: remove transitive dependenciesBCNF: every determinant must be a candidate keyThese definitions are useful when preparing for an exam.On their own, however, they do not explain how to examine a real table, identify what is wrong..

Engineering/Backend

[Spring Boot] JPA(Java Persistence API), Hibernate, Spring Data JPA

JPA(Java Persistence API)는 자바의 ORM(Object-Relational Mapping) 기술을 쉽게 구현하도록 도와주는 API이다.JpaRepository를 상속하는 인터페이스에 메서드 이름만 작성하면, JPA가 구현체를 생성하고 필요한 쿼리문을 자동으로 처리한다. 따라서 개발자는 SQL을 작성할 필요 없이 간단한 메서드 명칭만으로도 데이터베이스를 조작할 수 있다. JPA는 엔티티(Entity)라는 클래스를 이용하여 객체를 데이터베이스에 매핑한다. 엔티티는 개발자에게 테이블 또는 레코드와 유사한 개념이다. 객체를 이용하여 매핑을 처리하므로, 개발자는 객체지향적인 코드를 작성할 수 있다.JPA를 사용하면 CRUD(Create, Read, Update, Delete) 작업을 간편하게 ..

Engineering/Backend

[Spring] 이상형 월드컵 웹 애플리케이션 개발 과정

사용자에게 여러 가지 선택지를 제시하고, 그 중에서 선호하는 것을 선택하여 대결을 진행하는 웹 기반 이상형 월드컵을 만들어 보도록 하자.이상형 월드컵을 사용자가 진행할 때 하나의 페이지에서 화면이 바뀌면서 진행되어야 하므로 데이터를 유지하기 위해 세션 객체를 사용해 보려고 한다.Spring 프로젝트에서 사용자가 선택한 이상형 월드컵 결과를 저장하고 관리하는 시스템을 구현하면서 코드를 검토하고 각 계층에서 사용한 기술을 점검해 보려고 한다. 💡Spring FrameworkSpring Framework는 자바 기반의 엔터프라이즈 응용 프로그램을 개발하기 위한 전체적인 인프라를 제공하는 경량 프레임워크로, 의존성 주입(Dependency Injection)과 관점 지향 프로그래밍(Aspect-Oriented..

Career/Activities

[대재미: A1] SPARCS Service Hackathon 2024 데이터베이스

eXERD로 ERD(Entity Relationship Diagram) 데이터 모델링을 진행하였다. 데이터베이스에서 ERD는 효율적으로 데이터 관계를 확인하기 위해서 필수로 수행해야 하는 작업이지만, 해커톤 기간임을 고려하여 최대한 간단하게 작성하였다.해커톤에서 다른 개발자들의 데이터 모델링을 확인해 보니 eXERD보다는 ERDCloud를 사용하는 듯했다. 하나의 ERD에 많은 사람들이 접속할 수 있다고 해서 바꿔 보려고 한다.데이터 모델링 이후에는 DBeaver로 DDL(데이터 정의어), DML(데이터 조작어)을 작성하였다. DDL은 데이터베이스의 구조를 정의하는 데 사용되며, 테이블 생성, 변경, 삭제 등의 작업을 포함한다. 그리고 DML은 데이터를 검색, 삽입, 수정, 삭제하는 작업을 다루며, 테이..

Engineering/Database

[JDBC] Statement: PreparedStatement, CallableStatement

🍁PreparedStatementStatement와 PreparedStatement 차이Statement는 정적 SQL을 만들 때 사용하며, PreparedStatement는 동적 SQL를 만들 때 사용한다.매개변수가 없으면 정적 쿼리, 매개변수가 있으면 동적 쿼리를 의미한다. 정적 쿼리와 동적 쿼리정적 SQLString sql = "INSERT INTO tblAddress (seq, name, age, gender, address, regdate) VALUES (seqAddress.nextVal, 'Sopia', 21, 'f', '서울시 강남구 대치동', default)";String sql = "INSERT INTO tblAddress (seq, name, age, gender, address, r..