조회·변경

조회는 SELECT, 행을 바꾸는 문장은 WHERE가 범위를 정합니다.

조회

select id, title
from posts
where title like '%빌라%'
order by id desc
limit 20;

like의 대소문자 구분은 표의 콜레이션을 따른다. Postgres의 ilike는 없다.

추가·수정·삭제

insert into posts (title, body)
values ('첫 글', '본문');

update posts
set title = '수정한 제목'
where id = 1;

delete from posts
where id = 1;

where가 없으면 모든 행이다. where가 있으면 그 조건에 맞는 행만 나온다. update와 delete는 where 없이 실행하지 않는다.

두 표

작성자 표를 나누면 join으로 붙인다. 어느 값을 어느 표에 둘지는 PostgreSQL · 관계·정규화의 규칙과 같다.

select posts.id, posts.title, authors.email
from posts
join authors on authors.id = posts.author_id
where posts.id = 1;

JPA 메서드 findByTitleContaining이 만드는 문장도 이런 select다. Java에서 그 메서드를 호출하는 자리는 Spring Boot · API에 있다.