Home > Database > Mysql Tutorial > DATETIME Default Value: NOW() or '0000-00-00 00:00:00'?

DATETIME Default Value: NOW() or '0000-00-00 00:00:00'?

Patricia Arquette
Release: 2025-01-03 22:24:40
Original
655 people have browsed it

DATETIME Default Value: NOW() or '0000-00-00 00:00:00'?

Default Value for Datetime Datatype: NOW() or '0000-00-00 00:00:00'?

To set the default value for datetime datatype, you can consider the following options:

Current Default Value: '0000-00-00 00:00:00'

  • This is the default value used by MySQL for datetime columns without an explicit default.
  • You can modify an existing table to set this value with the following SQL statement:
ALTER TABLE users MODIFY lastVisitDate datetime DEFAULT '0000-00-00 00:00:00';
Copy after login

Dynamic Default Value: NOW()

  • From MySQL version 5.6.5 onwards, you can use a dynamic default value to set the datetime to the current time:
CREATE TABLE users (
    registerDate datetime DEFAULT CURRENT_TIMESTAMP
);
Copy after login
  • You can also modify an existing table with a DATETIME column to use NOW() as the default:
ALTER TABLE users MODIFY registerDate datetime DEFAULT CURRENT_TIMESTAMP;
Copy after login

Note: Prior to MySQL 5.6.5, dynamic default values for datetime were not supported. You could use the TIMESTAMP datatype instead, but it only allowed one auto-updated timestamp column per table.

The above is the detailed content of DATETIME Default Value: NOW() or '0000-00-00 00:00:00'?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template