연구개발/MYSQL
mysql { timestamp | datetime } default current_timestamp on update current_timestamp
HEAD1TON
2014. 5. 22. 20:30
728x90
반응형
mysql 5.0 이상 버전에서 (5.0 미만에 메뉴얼이 없는 것으 보아)
timestamp 데이터 타입에 디폴트 값을 넣을 수 있다.
구문은 column_name timestamp default current_timestamp 이다.
timestamp 데이터 타입은 다른 타입과 다르게 기본이 not null 제약조건을 갖는다.
따라서 null 값을 허용하려면 column_name timestamp null default current_timestamp 처럼 null 을 명시해주어야 한다.
제약 조건이 not null 임에도 불구하고 insert 할 때 null 값을 넣을 수가 있다.
이 때에는 자동으로 default 값으로 들어간다.
또한 on update current_timestamp 구문을 추가로 사용할 수 있다.
만약 row 가 update 된다면 자동으로 현재 시간으로 timestamp를 갱신해주는 구문이다.
mysql 5.6 이상 버전에서는 datetime 데이터 타입이도 default, on update 구문을 사용할 수 있게 되었다.
728x90
반응형