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

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

    Take for instance If I had a table named 'site' with a created_at and an update_at column that were both DATETIME and need the default value of now, I could execute the following sql to achieve this.

    ALTER TABLE `site` CHANGE `created_at` `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
    
    ALTER TABLE `site` CHANGE `created_at` `created_at` DATETIME  NULL DEFAULT NULL;
    
    ALTER TABLE `site` CHANGE `updated_at` `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
    
    ALTER TABLE `site` CHANGE `updated_at` `updated_at` DATETIME NULL DEFAULT  NULL;
    

    The sequence of statements is important because a table can not have two columns of type TIMESTAMP with default values of CUREENT TIMESTAMP

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

    I was able to solve this using this alter statement on my table that had two datetime fields.

    ALTER TABLE `test_table`
      CHANGE COLUMN `created_dt` `created_dt` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
      CHANGE COLUMN `updated_dt` `updated_dt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
    

    This works as you would expect the now() function to work. Inserting nulls or ignoring the created_dt and updated_dt fields results in a perfect timestamp value in both fields. Any update to the row changes the updated_dt. If you insert records via the MySQL query browser you needed one more step, a trigger to handle the created_dt with a new timestamp.

    CREATE TRIGGER trig_test_table_insert BEFORE INSERT ON `test_table`
        FOR EACH ROW SET NEW.created_dt = NOW();
    

    The trigger can be whatever you want I just like the naming convention [trig]_[my_table_name]_[insert]

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

    You can use triggers to do this type of stuff.

    CREATE TABLE `MyTable` (
    `MyTable_ID`  int UNSIGNED NOT NULL AUTO_INCREMENT ,
    `MyData`  varchar(10) NOT NULL ,
    `CreationDate`  datetime NULL ,
    `UpdateDate`  datetime NULL ,
    PRIMARY KEY (`MyTable_ID`)
    )
    ;
    
    CREATE TRIGGER `MyTable_INSERT` BEFORE INSERT ON `MyTable`
    FOR EACH ROW BEGIN
            -- Set the creation date
        SET new.CreationDate = now();
    
            -- Set the udpate date
        Set new.UpdateDate = now();
    END;
    
    CREATE TRIGGER `MyTable_UPDATE` BEFORE UPDATE ON `MyTable`
    FOR EACH ROW BEGIN
            -- Set the udpate date
        Set new.UpdateDate = now();
    END;
    
    0 讨论(0)
  • 2020-11-22 02:35

    I'm running MySql Server 5.7.11 and this sentence:

    ALTER TABLE table_name CHANGE date_column datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
    

    is not working. But the following:

    ALTER TABLE table_name CHANGE date_column datetime NOT NULL DEFAULT '1000-01-01 00:00:00'
    

    just works.

    As a sidenote, it is mentioned in the mysql docs:

    The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

    even if they also say:

    Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00').

    0 讨论(0)
  • 2020-11-22 02:35
    CREATE TABLE `testtable` (
        `id` INT(10) NULL DEFAULT NULL,
        `colname` DATETIME NULL DEFAULT '1999-12-12 12:12:12'
    )
    

    In the above query to create 'testtable', i used '1999-12-12 12:12:12' as default value for DATETIME column colname

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

    If you have already created the table then you can use

    To change default value to current date time

    ALTER TABLE <TABLE_NAME> 
    CHANGE COLUMN <COLUMN_NAME> <COLUMN_NAME> DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
    

    To change default value to '2015-05-11 13:01:01'

    ALTER TABLE <TABLE_NAME> 
    CHANGE COLUMN <COLUMN_NAME> <COLUMN_NAME> DATETIME NOT NULL DEFAULT '2015-05-11 13:01:01';
    
    0 讨论(0)
提交回复
热议问题