MySQL
SQL Error
Database
Troubleshooting
Error Handling

ERROR 1067 42000 Invalid default value for 'created_at'

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 is a widely-used open-source relational database management system (RDBMS). When working with MySQL, users may encounter various errors, one of which is ERROR 1067 (42000): Invalid default value for 'created_at'. This error typically arises when attempting to create or alter a table with a column that has an invalid default timestamp value. Understanding how this error manifests and how to resolve it is crucial for database administrators and developers.

Understanding ERROR 1067 (42000)

The ERROR 1067 (42000) usually occurs in MySQL when you attempt to set an invalid default value for a `DATETIME` or `TIMESTAMP` column. This is particularly common when dealing with columns meant to capture creation times, like `created_at`, without explicitly handling time zone issues or MySQL’s default settings on strict mode.

MySQL Strict Mode

MySQL operates in different SQL modes, and strict mode is one that affects how MySQL handles invalid or missing values in data manipulation statements. When strict mode is enabled, MySQL performs rigorous validation on the data being inserted into a database. If the input data doesn't comply with all of the predefined constraints, MySQL generates an error, instead of attempting to adjust the data and issue a warning.

Causes of ERROR 1067

  1. Incorrect SQL Mode Configuration:
    • If the server is running in strict mode and tries to set an invalid default value, it results in ERROR 1067.
  2. Incompatible Default Values for Timestamps:
    • Assigning invalid default timestamp values such as '0000-00-00 00:00:00' which are outside the permissible range.
  3. Version-specific peculiarities:
    • In MySQL 5.7 and above, handling of default values has changed to comply more strictly with ANSI SQL.
  4. Invalid Data Types:
    • Trying to assign a default value to a `DATETIME` or `TIMESTAMP` column that is not a valid timestamp format.

Practical Example

Consider creating a table with a column `created_at` that stores the timestamp of data entry creation:

  • Avoid Zero Dates:
  • Regularly Update MySQL Versions:
  • Understand SQL Mode Configuration:

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

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

Practice system design

All Rights Reserved.