How do you set a default value for a MySQL Datetime column?

后端 未结 26 2176
野的像风
野的像风 2020-11-22 01:55

How do you set a default value for a MySQL Datetime column?

In SQL Server it\'s getdate(). What is the equivalant for MySQL? I\'m using MySQL 5.x if tha

相关标签:
26条回答
  • 2020-11-22 02:19

    This is my trigger example:

    /************ ROLE ************/
    drop table if exists `role`;
    create table `role` (
        `id_role` bigint(20) unsigned not null auto_increment,
        `date_created` datetime,
        `date_deleted` datetime,
        `name` varchar(35) not null,
        `description` text,
        primary key (`id_role`)
    ) comment='';
    
    drop trigger if exists `role_date_created`;
    create trigger `role_date_created` before insert
        on `role`
        for each row 
        set new.`date_created` = now();

    0 讨论(0)
  • 2020-11-22 02:20

    IMPORTANT EDIT: It is now possible to achieve this with DATETIME fields since MySQL 5.6.5, take a look at the other post below...

    Previous versions can't do that with DATETIME...

    But you can do it with TIMESTAMP:

    mysql> create table test (str varchar(32), ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> desc test;
    +-------+-------------+------+-----+-------------------+-------+
    | Field | Type        | Null | Key | Default           | Extra |
    +-------+-------------+------+-----+-------------------+-------+
    | str   | varchar(32) | YES  |     | NULL              |       | 
    | ts    | timestamp   | NO   |     | CURRENT_TIMESTAMP |       | 
    +-------+-------------+------+-----+-------------------+-------+
    2 rows in set (0.00 sec)
    
    mysql> insert into test (str) values ("demo");
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from test;
    +------+---------------------+
    | str  | ts                  |
    +------+---------------------+
    | demo | 2008-10-03 22:59:52 | 
    +------+---------------------+
    1 row in set (0.00 sec)
    
    mysql>
    

    CAVEAT: IF you define a column with CURRENT_TIMESTAMP ON as default, you will need to ALWAYS specify a value for this column or the value will automatically reset itself to "now()" on update. This means that if you do not want the value to change, your UPDATE statement must contain "[your column name] = [your column name]" (or some other value) or the value will become "now()". Weird, but true. I hope this helps. I am using 5.5.56-MariaDB

    0 讨论(0)
  • 2020-11-22 02:21

    MySQL 5.6 has fixed this problem.

    ALTER TABLE mytable CHANGE mydate datetime NOT NULL DEFAULT 'CURRENT_TIMESTAMP'
    
    0 讨论(0)
  • 2020-11-22 02:22

    If you set ON UPDATE CURRENT_TIMESTAMP it will take current time when row data update in table.

     CREATE TABLE bar(
            `create_time` TIMESTAMP CURRENT_TIMESTAMP,
            `update_time` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        )
    
    0 讨论(0)
  • 2020-11-22 02:24

    Use the following code

    DELIMITER $$
    
        CREATE TRIGGER bu_table1_each BEFORE UPDATE ON table1 FOR EACH ROW
        BEGIN
          SET new.datefield = NOW();
        END $$
    
        DELIMITER ;
    
    0 讨论(0)
  • 2020-11-22 02:26

    If you are trying to set default value as NOW(), I don't think MySQL supports that. In MySQL, you cannot use a function or an expression as the default value for any type of column, except for the TIMESTAMP data type column, for which you can specify the CURRENT_TIMESTAMP as the default.

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