Having both a Created and Last Updated timestamp columns in MySQL 4.0

前端 未结 11 2137
逝去的感伤
逝去的感伤 2020-11-27 09:19

I have the following table schema;

CREATE TABLE `db1`.`sms_queue` (
  `Id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `Message` VARCHAR(160) NOT NULL DEFAUL         


        
相关标签:
11条回答
  • 2020-11-27 09:57

    This is how can you have automatic & flexible createDate/lastModified fields using triggers:

    First define them like this:

    CREATE TABLE `entity` (
      `entityid` int(11) NOT NULL AUTO_INCREMENT,
      `createDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
      `lastModified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
      `name` varchar(255) DEFAULT NULL,
      `comment` text,
      PRIMARY KEY (`entityid`),
    )
    

    Then add these triggers:

    DELIMITER ;;
    CREATE trigger entityinsert BEFORE INSERT ON entity FOR EACH ROW BEGIN SET NEW.createDate=IF(ISNULL(NEW.createDate) OR NEW.createDate='0000-00-00 00:00:00', CURRENT_TIMESTAMP, IF(NEW.createDate<CURRENT_TIMESTAMP, NEW.createDate, CURRENT_TIMESTAMP));SET NEW.lastModified=NEW.createDate; END;;
    DELIMITER ;
    CREATE trigger entityupdate BEFORE UPDATE ON entity FOR EACH ROW SET NEW.lastModified=IF(NEW.lastModified<OLD.lastModified, OLD.lastModified, CURRENT_TIMESTAMP);
    
    • If you insert without specifying createDate or lastModified, they will be equal and set to the current timestamp.
    • If you update them without specifying createDate or lastModified, the lastModified will be set to the current timestamp.

    But here's the nice part:

    • If you insert, you can specify a createDate older than the current timestamp, allowing imports from older times to work well (lastModified will be equal to createDate).
    • If you update, you can specify a lastModified older than the previous value ('0000-00-00 00:00:00' works well), allowing to update an entry if you're doing cosmetic changes (fixing a typo in a comment) and you want to keep the old lastModified date. This will not modify the lastModified date.
    0 讨论(0)
  • 2020-11-27 10:00

    My web host is stuck on version 5.1 of mysql so anyone like me that doesn't have the option of upgrading can follow these directions:

    http://joegornick.com/2009/12/30/mysql-created-and-modified-date-fields/

    0 讨论(0)
  • 2020-11-27 10:06

    From the MySQL 5.5 documentation:

    One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.

    Changes in MySQL 5.6.5:

    Previously, at most one TIMESTAMP column per table could be automatically initialized or updated to the current date and time. This restriction has been lifted. Any TIMESTAMP column definition can have any combination of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses. In addition, these clauses now can be used with DATETIME column definitions. For more information, see Automatic Initialization and Updating for TIMESTAMP and DATETIME.

    0 讨论(0)
  • 2020-11-27 10:06

    If you do decide to have MySQL handle the update of timestamps, you can set up a trigger to update the field on insert.

    CREATE TRIGGER <trigger_name> BEFORE INSERT ON <table_name> FOR EACH ROW SET NEW.<timestamp_field> = CURRENT_TIMESTAMP;
    

    MySQL Reference: http://dev.mysql.com/doc/refman/5.0/en/triggers.html

    0 讨论(0)
  • 2020-11-27 10:09

    This issue seemed to have been resolved in MySQL 5.6. I have noticed this until MySQL 5.5; here is an example code:

    DROP TABLE IF EXISTS `provider_org_group` ;
    CREATE TABLE IF NOT EXISTS `provider_org_group` (
      `id` INT NOT NULL,
      `name` VARCHAR(100) NOT NULL,
      `type` VARCHAR(100) NULL,
      `inserted` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `insert_src_ver_id` INT NULL,
      `updated` TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
      `update_src_ver_id` INT NULL,
      `version` INT NULL,
      PRIMARY KEY (`id`),
      UNIQUE INDEX `id_UNIQUE` (`id` ASC),
      UNIQUE INDEX `name_UNIQUE` (`name` ASC))
    ENGINE = InnoDB;
    

    Running this on MySQL 5.5 gives:

    ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
    

    Running this on MySQL 5.6

    0 row(s) affected   0.093 sec
    
    0 讨论(0)
  • 2020-11-27 10:09
    create table test_table( 
    id integer not null auto_increment primary key, 
    stamp_created timestamp default '0000-00-00 00:00:00', 
    stamp_updated timestamp default now() on update now() 
    ); 
    

    source: http://gusiev.com/2009/04/update-and-create-timestamps-with-mysql/

    0 讨论(0)
提交回复
热议问题