brief:
I’m experiencing strange MySQL behavior – on update CURRENT_TIMESTAMP attribute is being added although I don’t want it to be added. I want to find out why is this happening – is it a matter of MySQL server or MySQL Workbench I’m using (v5.2.38).
detail:
I’ve modelled the database structure using EER diagrams, an example table is below:
CREATE TABLE IF NOT EXISTS `privilege` (
`id` INT NOT NULL ,
`name` VARCHAR(64) NOT NULL COMMENT 'privilege name (just a label)' ,
`created_at` TIMESTAMP NOT NULL COMMENT 'when the privilege was created' ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
The above script fragment is created using Export > Forward Engineer SQL Create Script inside WorkBench. The created_at column is siginifant here. It is NOT NULL and there is no default value defined for the timestamp when a record is defined. So I guess, that if someone tries to insert a record without defining created_at, an error will be raised.
I run this script inside MySQL server to create the whole structure. And the created structure is different – show create table privilege returns the following:
CREATE TABLE `privilege` (
`id` int(11) NOT NULL,
`name` varchar(64) NOT NULL COMMENT 'privilege name (just a label)',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP COMMENT 'when the privilege was created',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Where did “on update current_timestamp” come from? I’m 100% sure that I didn’t choose any appropriate option, so MySQL should not create anything he’s not asked to.
Does anyone have an idea why those clauses are added?
As documented under Automatic Initialization and Updating for
TIMESTAMP: