How to set multiple TimeStamps for one table in mysql


http://www.jb51.net/article/31872.htm

quote


The default value of the timestamp setting is Default CURRENT_TIMESTAMP; the timestamp setting is automatically updated as the table changes. It is ON UPDATE CURRENT_TIMESTAMP; the following is a detailed introduction to the timestamp
setting. The default value is Default CURRENT_TIMESTAMP
The timestamp setting is automatically updated as the table changes. There can only be at most one field in a table setting CURRENT_TIMESTAMP and two rows setting DEFAULT CURRENT_TIMESTAMP is not acceptable. One more thing to note : CREATE TABLE `device` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `toid` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'toid', `createtime` TIMESTAMP NOT NULL COMMENT 'create time', `updatetime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'last update time', PRIMARY KEY (`id`), UNIQUE INDEX `toid` (`toid`) ) COMMENT='

















COLLATE='utf8_general_ci'
ENGINE=InnoDB;

a setting like this will not work.
The reason is that mysql implicitly sets DEFAULAT CURRENT_TIMESTAMP for the first timestamp field in the table (and NOT NULL is set). So the setting in the above example is actually equivalent to setting two CURRENT_TIMESTAMP.

Analysis Requirements In
a table, there are two fields, createtime and updatetime.
1 When inserting, neither of the two fields in SQL is set, and it will be set to the current time.
2 When updating, neither of the two fields in SQL is set, and the updatetime will be changed to the current time.

This requirement is impossible. . Because you can't avoid setting CURRENT_TIMESTAMP on two fields,

there are several solutions:
1 Use triggers
When insert and update, trigger trigger time setting.
There are people online who use this method. Certainly do not doubt the usability of this method. But for the actual scene, it is undoubtedly to solve small problems and increase the complexity.
2 Set the default of the first timestamp to 0.
The table structure is as follows:
Copy the code as follows:

CREATE TABLE `device` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
Note here that the createtime of the insert operation must be set to null! ! Although I also feel that this method is very unpleasant, it only needs to modify the insert operation slightly to reduce the burden on the sql statement, and it feels worthwhile. This is indeed a minimal and guaranteed way to modify the database. Of course, this method can also be used at the same time as method 1, which can reduce the number of triggers written. 3 Honestly use timestamps in sql statements. This is the most popular choice

















Do not do too much design on the table structure:
copy the code The code is as follows:

CREATE TABLE `device` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`toid` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'toid ',
`createtime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'create time',
`updatetime` TIMESTAMP NOT NULL COMMENT 'last update time',
PRIMARY KEY (`id`),
UNIQUE INDEX `toid` (`toid`)
)
COMMENT= 'Device table'
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

so you need to write specific timestamps during insert and update operations.
insert device set toid=11,createtime='2012-11-2 10:10:10',updatetime='2012-11-2 10:10:10'
update device set toid=22,updatetime='2012-11- 2 10:10:10'
In fact, if you think about it, there is one advantage: current_timestamp is unique to mysql. When the database is transferred from mysql to other databases, the business logic code does not need to be modified.

ps: The choice of these three methods depends entirely on your own considerations. By the way, in the end, I still choose the third method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326207133&siteId=291194637