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

后端 未结 26 2075
野的像风
野的像风 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:37

    this is indeed terrible news.here is a long pending bug/feature request for this. that discussion also talks about the limitations of timestamp data type.

    I am seriously wondering what is the issue with getting this thing implemented.

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

    While defining multi-line triggers one has to change the delimiter as semicolon will be taken by MySQL compiler as end of trigger and generate error. e.g.

    DELIMITER //
    CREATE TRIGGER `MyTable_UPDATE` BEFORE UPDATE ON `MyTable`
    FOR EACH ROW BEGIN
            -- Set the udpate date
        Set new.UpdateDate = now();
    END//
    DELIMITER ;
    
    0 讨论(0)
  • 2020-11-22 02:39

    If you are trying to set default value as NOW(),MySQL supports that you have to change the type of that column TIMESTAMP instead of DATETIME. TIMESTAMP have current date and time as default..i think it will resolved your problem..

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

    In version 5.6.5, it is possible to set a default value on a datetime column, and even make a column that will update when the row is updated. The type definition:

    CREATE TABLE foo (
        `creation_time`     DATETIME DEFAULT CURRENT_TIMESTAMP,
        `modification_time` DATETIME ON UPDATE CURRENT_TIMESTAMP
    )
    

    Reference: http://optimize-this.blogspot.com/2012/04/datetime-default-now-finally-available.html

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

    MySQL (before version 5.6.5) does not allow functions to be used for default DateTime values. TIMESTAMP is not suitable due to its odd behavior and is not recommended for use as input data. (See MySQL Data Type Defaults.)

    That said, you can accomplish this by creating a Trigger.

    I have a table with a DateCreated field of type DateTime. I created a trigger on that table "Before Insert" and "SET NEW.DateCreated=NOW()" and it works great.

    I hope this helps somebody.

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

    For me the trigger approach has worked the best, but I found a snag with the approach. Consider the basic trigger to set a date field to the current time on insert:

    CREATE TRIGGER myTable_OnInsert BEFORE INSERT ON `tblMyTable`
        FOR EACH ROW SET NEW.dateAdded = NOW();
    

    This is usually great, but say you want to set the field manually via INSERT statement, like so:

    INSERT INTO tblMyTable(name, dateAdded) VALUES('Alice', '2010-01-03 04:30:43');
    

    What happens is that the trigger immediately overwrites your provided value for the field, and so the only way to set a non-current time is a follow up UPDATE statement--yuck! To override this behavior when a value is provided, try this slightly modified trigger with the IFNULL operator:

    CREATE TRIGGER myTable_OnInsert BEFORE INSERT ON `tblMyTable`
        FOR EACH ROW SET NEW.dateAdded = IFNULL(NEW.dateAdded, NOW());
    

    This gives the best of both worlds: you can provide a value for your date column and it will take, and otherwise it'll default to the current time. It's still ghetto relative to something clean like DEFAULT GETDATE() in the table definition, but we're getting closer!

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