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

前端 未结 11 2138
逝去的感伤
逝去的感伤 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 10:10

    As of MySQL 5.6 its easy-peasy... give it a try:

    create table tweet ( 
        id integer not null auto_increment primary key, 
        stamp_created timestamp default now(), 
        stamp_updated timestamp default now() on update now(),
        message varchar(163)
    )
    
    0 讨论(0)
  • 2020-11-27 10:13

    i think this is the better query for stamp_created and stamp_updated

    CREATE TABLE test_table( 
        id integer not null auto_increment primary key, 
        stamp_created TIMESTAMP DEFAULT now(), 
        stamp_updated TIMESTAMP DEFAULT '0000-00-00 00:00:00' ON UPDATE now() 
    ); 
    

    because when the record created, stamp_created should be filled by now() and stamp_updated should be filled by '0000-00-00 00:00:00'

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

    There is a trick to have both timestamps, but with a little limitation.

    You can use only one of the definitions in one table. Create both timestamp columns like so:

    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() 
    ); 
    

    Note that it is necessary to enter null into both columns during insert:

    mysql> insert into test_table(stamp_created, stamp_updated) values(null, null); 
    Query OK, 1 row affected (0.06 sec)
    
    mysql> select * from test_table; 
    +----+---------------------+---------------------+ 
    | id | stamp_created       | stamp_updated       |
    +----+---------------------+---------------------+
    |  2 | 2009-04-30 09:44:35 | 2009-04-30 09:44:35 |
    +----+---------------------+---------------------+
    2 rows in set (0.00 sec)  
    
    mysql> update test_table set id = 3 where id = 2; 
    Query OK, 1 row affected (0.05 sec) Rows matched: 1  Changed: 1  Warnings: 0  
    
    mysql> select * from test_table;
    +----+---------------------+---------------------+
    | id | stamp_created       | stamp_updated       | 
    +----+---------------------+---------------------+ 
    |  3 | 2009-04-30 09:44:35 | 2009-04-30 09:46:59 | 
    +----+---------------------+---------------------+ 
    2 rows in set (0.00 sec)  
    
    0 讨论(0)
  • 2020-11-27 10:18

    You can have them both, just take off the "CURRENT_TIMESTAMP" flag on the created field. Whenever you create a new record in the table, just use "NOW()" for a value.

    Or.

    On the contrary, remove the 'ON UPDATE CURRENT_TIMESTAMP' flag and send the NOW() for that field. That way actually makes more sense.

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

    For mysql 5.7.21 I use the following and works fine:

    CREATE TABLE Posts ( modified_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP )

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