MySQL
SQL error
default value
DATE DATETIME issue
database troubleshooting

Error in MySQL when setting default value for DATE or DATETIME

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

MySQL errors around default values for DATE and DATETIME columns usually come from type rules, SQL mode, or both. The fix is rarely to "force" MySQL to accept the value. The real fix is to choose a default expression that the column type and server mode actually allow.

Why the Error Happens

MySQL evaluates defaults at table-definition time, not only when rows are inserted. That means a default must be valid for the column type and valid under the server's SQL mode. A few rules matter most:

  • 'DATE stores only a date like 2026-03-11'
  • 'DATETIME stores date and time like 2026-03-11 14:30:00'
  • 'TIMESTAMP also stores date and time, but it has special automatic behaviors'
  • strict modes reject invalid or zero-like temporal values more aggressively

Two common mistakes are:

  1. assigning a DATETIME style default to a DATE column
  2. using an invalid literal such as 0000-00-00 when strict mode is enabled

Invalid and Valid Defaults

This example shows several definitions that often trigger errors:

sql
1CREATE TABLE bad_examples (
2    only_date DATE NOT NULL DEFAULT CURRENT_TIMESTAMP,
3    created_at DATETIME NOT NULL DEFAULT '2026/03/11 12:00:00',
4    legacy_date DATE NOT NULL DEFAULT '0000-00-00'
5);

There are multiple problems here:

  • 'CURRENT_TIMESTAMP is intended for TIMESTAMP and DATETIME, not a plain DATE column'
  • ''2026/03/11 12:00:00' is not the canonical datetime literal format'
  • ''0000-00-00' may be rejected under strict SQL mode or NO_ZERO_DATE'

Here is a valid version:

sql
1CREATE TABLE events (
2    id INT PRIMARY KEY AUTO_INCREMENT,
3    event_date DATE NOT NULL DEFAULT '2026-03-11',
4    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
5    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
6        ON UPDATE CURRENT_TIMESTAMP
7);

The rule is simple: use a properly formatted constant for DATE, and use CURRENT_TIMESTAMP when you want automatic current date-and-time behavior for DATETIME or TIMESTAMP.

Checking SQL Mode

SQL mode changes what MySQL accepts. Before debugging a schema definition, inspect the active mode:

sql
SELECT @@sql_mode;

Modes such as STRICT_TRANS_TABLES, NO_ZERO_DATE, and TRADITIONAL turn questionable defaults into hard errors instead of warnings. That is usually a good thing because it keeps bad temporal data out of the table.

If an old blog post suggests disabling strict mode globally, treat that as a last resort. It may silence the error, but it also weakens data validation for the whole server.

If You Need "Today" in a DATE Column

The tricky case is a DATE column that should default to the current date. Because CURRENT_TIMESTAMP is a date-and-time value, many people try to use it directly and then wonder why MySQL rejects the definition.

One portable solution is to set the value in application code before insert. Another is to use a trigger:

sql
1CREATE TABLE invoices (
2    id INT PRIMARY KEY AUTO_INCREMENT,
3    invoice_date DATE NOT NULL,
4    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
5);
6
7DELIMITER //
8
9CREATE TRIGGER invoices_before_insert
10BEFORE INSERT ON invoices
11FOR EACH ROW
12BEGIN
13    IF NEW.invoice_date IS NULL THEN
14        SET NEW.invoice_date = CURRENT_DATE;
15    END IF;
16END//
17
18DELIMITER ;

This keeps the schema honest about types. The DATE column receives a date, and the logic that derives "today" is handled explicitly.

Application-Side Inserts

For many teams, the cleanest approach is simply to insert the date from the application. That makes time zone handling and business rules more obvious.

python
1from datetime import date
2import mysql.connector
3
4connection = mysql.connector.connect(
5    host="localhost",
6    user="app",
7    password="secret",
8    database="demo",
9)
10
11cursor = connection.cursor()
12cursor.execute(
13    "INSERT INTO invoices (invoice_date) VALUES (%s)",
14    (date.today().isoformat(),),
15)
16connection.commit()

This is often preferable when "today" must follow application logic rather than the database server's local time.

Common Pitfalls

Using CURRENT_TIMESTAMP on a DATE column is a common mistake. It looks reasonable, but the type semantics are different.

Relying on zero dates like '0000-00-00' creates migration trouble later, especially when strict SQL modes are enabled in production.

Copying examples that disable strict mode can hide the real schema problem and allow invalid temporal data into the database.

Forgetting about time zones causes confusion when a DATETIME or TIMESTAMP default seems "off" by hours in application output.

Summary

  • MySQL default-value errors for temporal columns usually come from type mismatches or SQL mode.
  • Use properly formatted constants for DATE and DATETIME literal defaults.
  • Use CURRENT_TIMESTAMP for automatic current time on DATETIME or TIMESTAMP, not plain DATE.
  • Check @@sql_mode before assuming the server is wrong.
  • If a DATE column should default to today, use application logic or a trigger instead of forcing an incompatible default.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design