programing

MySQL TIMESTAMP를 CURRENT_TIMESTAMP가 아닌 기본 NULL로 설정

linuxpc 2023. 8. 24. 21:50
반응형

MySQL TIMESTAMP를 CURRENT_TIMESTAMP가 아닌 기본 NULL로 설정

MySQL을 사용하여 날짜 열과 시간 열로 타임스탬프 열을 만들려고 합니다.날짜 또는 시간 열에 NULL 값이 포함된 경우 MySQL은 자동으로 해당 값을 설정합니다.TIMESTAMP열을 표시합니다.

기본값으로 설정할 수 있는 방법이 있습니까?NULL대신에CURRENT_TIMESTAMP?

다음과 같은 것:

ALTER TABLE customers ADD s_timestamp TIMESTAMP;
UPDATE customers
    SET s_timestamp = timestamp(s_date,s_time) DEFAULT NULL;

이 쿼리를 사용하여 열을 추가합니다.

ALTER TABLE `customers` 
ADD COLUMN `s_timestamp` TIMESTAMP NULL DEFAULT NULL;

업데이트 시에만 현재 타임스탬프를 가져오려면 다음을 수행합니다.

ALTER TABLE `customers` 
ADD COLUMN `s_timestamp` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;

기본적으로 current_timestamp로 생성된 기존 열을 수정하려는 경우 승인된 응답에 추가하려면:

alter table `customers` modify `s_timestamp` timestamp null

이제 열은 더 이상 current_timestamp로 기본 설정되지 않습니다.

효과가 있을 거라 생각했습니다.

ALTER TABLE customers ADD COLUMN s_timestamp TIMESTAMP DEFAULT NULL;

언급URL : https://stackoverflow.com/questions/20520443/mysql-timestamp-to-default-null-not-current-timestamp

반응형