[PostgreSQL] returning 을 사용해서 동시에 여러 테이블에 DML 쿼리 실행하기

식빵·2023년 4월 20일
0

postgresql-memo

목록 보기
14/34
post-thumbnail

INSERT

with temp_01 as (
    insert into user_signup(user_id, nickname)
    values (nextval('seq_user_map_id'), 'daily code')
    returning *
)
insert into user_info(user_id, nickname, signup_time)
select temp_01.user_id, temp_01.nickname, now()
from temp_01

DELETE

with temp_01 as (
    delete from user_info a
    where a.user_id = 123
    returning *
)
delete from user_signup t1
where t1.user_id in (select user_id from temp_01)

UPDATE

with temp_01 as (
    update user_info 
    set nickname = 'changed', modfied_time = now()
    where user_id = 123
    returning *
)
update user_profile a
set nickname = 'changed', modfied_time = now()
from temp_01 b
where a.user_id  = b.user_id
profile
백엔드를 계속 배우고 있는 개발자입니다 😊

0개의 댓글