다양한 답변을 결합 :
MySQL은 5.5 년, DEFAULT CURRENT_TIMESTAMP
그리고 ON UPDATE CURRENT_TIMESTAMP
에 추가 할 수 없습니다 DATETIME
만에 TIMESTAMP
.
규칙 :
1) TIMESTAMP
테이블 당 최대 하나의 열을 자동으로 (또는 수동으로 [ My addition ]) 초기화하거나 현재 날짜 및 시간으로 업데이트 할 수 있습니다. (MySQL 문서).
따라서 오직 하나만 in 또는 절을 TIMESTAMP
가질 수 있습니다CURRENT_TIMESTAMP
DEFAULT
ON UPDATE
2) 제 1 NOT NULL
TIMESTAMP
명시없이 열 DEFAULT
같은 값을 created_date timestamp default '0000-00-00 00:00:00'
암시 적으로 설명한다 DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
, 따라서 후속 TIMESTAMP
열이 주어지지 않을 수 CURRENT_TIMESTAMP
의 DEFAULT
또는 ON UPDATE
절
CREATE TABLE `address` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`village` int(11) DEFAULT NULL,
`created_date` timestamp default '0000-00-00 00:00:00',
-- Since explicit DEFAULT value that is not CURRENT_TIMESTAMP is assigned for a NOT NULL column,
-- implicit DEFAULT CURRENT_TIMESTAMP is avoided.
-- So it allows us to set ON UPDATE CURRENT_TIMESTAMP on 'updated_date' column.
-- How does setting DEFAULT to '0000-00-00 00:00:00' instead of CURRENT_TIMESTAMP help?
-- It is just a temporary value.
-- On INSERT of explicit NULL into the column inserts current timestamp.
-- `created_date` timestamp not null default '0000-00-00 00:00:00', // same as above
-- `created_date` timestamp null default '0000-00-00 00:00:00',
-- inserting 'null' explicitly in INSERT statement inserts null (Ignoring the column inserts the default value)!
-- Remember we need current timestamp on insert of 'null'. So this won't work.
-- `created_date` timestamp null , // always inserts null. Equally useless as above.
-- `created_date` timestamp default 0, // alternative to '0000-00-00 00:00:00'
-- `created_date` timestamp,
-- first 'not null' timestamp column without 'default' value.
-- So implicitly adds DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP.
-- Hence cannot add 'ON UPDATE CURRENT_TIMESTAMP' on 'updated_date' column.
`updated_date` timestamp null on update current_timestamp,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=132 DEFAULT CHARSET=utf8;
INSERT INTO address (village,created_date) VALUES (100,null);
mysql> select * from address;
+-----+---------+---------------------+--------------+
| id | village | created_date | updated_date |
+-----+---------+---------------------+--------------+
| 132 | 100 | 2017-02-18 04:04:00 | NULL |
+-----+---------+---------------------+--------------+
1 row in set (0.00 sec)
UPDATE address SET village=101 WHERE village=100;
mysql> select * from address;
+-----+---------+---------------------+---------------------+
| id | village | created_date | updated_date |
+-----+---------+---------------------+---------------------+
| 132 | 101 | 2017-02-18 04:04:00 | 2017-02-18 04:06:14 |
+-----+---------+---------------------+---------------------+
1 row in set (0.00 sec)
다른 옵션 (그러나 updated_date
첫 번째 열) :
CREATE TABLE `address` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`village` int(11) DEFAULT NULL,
`updated_date` timestamp null on update current_timestamp,
`created_date` timestamp not null ,
-- implicit default is '0000-00-00 00:00:00' from 2nd timestamp onwards
-- `created_date` timestamp not null default '0000-00-00 00:00:00'
-- `created_date` timestamp
-- `created_date` timestamp default '0000-00-00 00:00:00'
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=132 DEFAULT CHARSET=utf8;
CURRENT_TIMESTAMP
inDEFAULT
또는ON UPDATE
절로 열을 정의 할 수 없습니다TIMESTAMP
!