mysql automatically store record creation timestamp

前端 未结 5 850
清歌不尽
清歌不尽 2020-11-30 03:39

Is there some way mysql can store timestamp automatically in a record row whenever that it is created. I was trying to use timestamp(data type) with current_timestamp as def

5条回答
  •  遥遥无期
    2020-11-30 04:21

    Set the DEFAULT constraint to use CURRENT_TIMESTAMP:

    CREATE TABLE ...
      your_date_column DATETIME DEFAULT CURRENT_TIMESTAMP
      ...
    

    For an existing table, use the ALTER TABLE statement:

    ALTER TABLE your_table
    ALTER COLUMN date_column SET DEFAULT CURRENT_TIMESTAMP
    

    Unless you specify a value to for the date_column, the default will be the date & time the INSERT statement was run. NULL and DEFAULT or valid values to use the default constraint otherwise, assuming the column is nullable.

提交回复
热议问题