Combinazione di varie risposte:
In MySQL 5.5 DEFAULT CURRENT_TIMESTAMP
e ON UPDATE CURRENT_TIMESTAMP
non può essere aggiunto DATETIME
ma solo su TIMESTAMP
.
Regole:
1) al massimo una TIMESTAMP
colonna per tabella potrebbe essere inizializzata automaticamente (o manualmente [La mia aggiunta ]) o aggiornata alla data e ora correnti. (Documenti MySQL).
Quindi solo uno TIMESTAMP
può avere CURRENT_TIMESTAMP
in DEFAULT
o ON UPDATE
clausola
2) La prima NOT NULL
TIMESTAMP
colonna senza un DEFAULT
valore esplicito come created_date timestamp default '0000-00-00 00:00:00'
verrà implicitamente assegnata una DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
e quindi le TIMESTAMP
colonne successive non possono essere fornite o CURRENT_TIMESTAMP
sulla clausolaDEFAULT
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)
Altra opzione (Ma updated_date
è la prima colonna):
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
oON UPDATE
clausola una volta che c'è una colonna conTIMESTAMP
tipo di dati, non importa se ha una clausola aggiuntiva!