개발자의 서재

[mysql]current_timestamp(), on update current_timestamp() 본문

DB

[mysql]current_timestamp(), on update current_timestamp()

ironmask431 2022. 6. 3. 11:02

테이블마다 보통 공통적으로 쓰는 timestamp 컬럼으로 createdAt, updatedAt (등록일시, 수정일시)

가 있다. timstamp 컬럼에 current_timestamp(), on update current_timestamp() 옵션을 주면 

이때 insert 나 update 시 쿼리에 포함시키지 않아도, 자동으로 입력시간, 수정시간이 입력된다. 

 

createdAt 컬럼에는 current_timestamp() 만 

updatedAt 컬럼에는 current_timestamp() , on update current_timestamp()  을 둘다 주어서 

보통 사용한다. 

 

alert 쿼리 예시

ALTER TABLE 테이블명 MODIFY COLUMN 컬럼명 
timestamp DEFAULT current_timestamp() on update current_timestamp()

 

 

jpa 사용시에는 insert, update 쿼리에 해당 필드를 포함 시킬 필요 없으므로 읽기 전용으로 만들기위해 

해당 필드에 아래 옵션을 넣어주면,  insert, update 시 쿼리에 해당 필드는 포함되지 않는다. 

@Column(insertable = false, updatable = false)
private LocalDateTime createdAt;

'DB' 카테고리의 다른 글

docker로 local에 mariaDB 설치해보기  (0) 2022.05.23
[mariaDB] CREATE 문 예시들  (0) 2022.03.18
[Oracle] 자주쓰는 SQL(DDL,DCL) 정리  (0) 2022.03.06
Comments