MySQL
[MySQL] TIMESTAMP
건휘맨
2024. 5. 16. 11:02
디폴트 값으로 now() 지정:
TIMESTAMP 컬럼을 생성할 때 디폴트 값을 now() 지정하면,
해당 컬럼에 새로운 레코드가 삽입될 때 자동으로 현재 시각이 저장
Apply하면 CURRENT_TIMESTAMP으로 변환
디폴트 값으로 now() 지정 및 업데이트 시 NOW()로 갱신:
TIMESTAMP 컬럼을 생성할 때 now() on update now() 지정하면,
해당 컬럼에 새로운 레코드가 삽입될 때와
해당 레코드가 수정될 때 모두 자동으로 현재 시각으로 갱신
Apply하면 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP으로 변환
-- timestamp 설정 전
insert into comment
(content, createdAt)
values
('좋아요', now());
insert into comment
(content, createdAt)
values
('맛없어요', now());
-- timestamp 설정 후
insert into comment
(content)
values
('진짜 맛있네요~!');
insert into comment
(content)
values
('별 5개 입니다');
update comment
set content = '별 3개 입니다'
where id = 4;
select *
from comment;