Łączenie różnych odpowiedzi:
W MySQL 5.5 DEFAULT CURRENT_TIMESTAMP
i ON UPDATE CURRENT_TIMESTAMP
nie można go dodawać, DATETIME
ale tylko na TIMESTAMP
.
Zasady:
1) najwyżej jedna TIMESTAMP
kolumna na tabelę może być automatycznie (lub ręcznie [ Moje dodanie ]) zainicjowana lub zaktualizowana do bieżącej daty i godziny. (Dokumenty MySQL).
Więc tylko jeden TIMESTAMP
może mieć CURRENT_TIMESTAMP
w DEFAULT
lub ON UPDATE
klauzulę
2) Pierwsza NOT NULL
TIMESTAMP
kolumna bez wyraźnej DEFAULT
wartości jak created_date timestamp default '0000-00-00 00:00:00'
zostaną niejawnie dali DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
i stąd kolejne TIMESTAMP
kolumny nie może być udzielona CURRENT_TIMESTAMP
na DEFAULT
lub ON UPDATE
klauzuli
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)
Inna opcja (ale updated_date
to pierwsza kolumna):
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
wDEFAULT
lubON UPDATE
klauzuli raz tam kolumna zTIMESTAMP
typem danych, bez względu na to, czy to ma dodatkową klauzulę!