MySQL 5.7 change a datetime column to default NULL from 0000-00-00 00:00:00

So I started working on one of my Laravel projects, due to its large size, I was not able to perform migrations. In this case, we decided to directly export the MySQL database from the live server to our local server.

During this process, we faced the below errors.

  1. Not able to update the date column field in MySQL to default NULL.
  2. SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘0’ for key ‘PRIMARY’

I faced the issue of updating the Table column of type Date/Time to NULL. There were 100’s of rows in the table. I have to update the column value to NULL in it.

If there were 10-20 rows in the table I can update that manually as well. But, as the number of rows is big. So, I found the below way to simplify the changes. Adding it here so you can do that too.


How to change a datetime or date column in MySQL to default NULL.?

I found on the internet we can do it by NO_ZERO_DATE compatibility mode as well, BUT what if we don’t want to go for it.

Without using the NO_ZERO_DATE compatibility mode, We can also fixe the incorrect datetime value: ‘0000-00-00 00:00:00’.

/** ---
* To set an existing column in MySQL 5.7 from 0000-00-00 00:00:00 to NULL as default
* We will be following 3 iterations to get this task done.
--- */
/* Step 1) Set all 0000-00-00... to a valid but a date which is rarely used i.e 5001-01-01 value: */UPDATE my_table SET my_datetime_col = '5001-01-01 00:00:00' WHERE CAST(my_datetime_col AS CHAR(20)) = '0000-00-00 00:00:00';

I am casting CHAR(20) because I have both values i.e date and time in my database table column.

/* Step 2) Modify the column to DEFAULT: NULL */ALTER TABLE my_table MODIFY COLUMN my_datetime_col DATETIME NULL;

/* Step 3) Revert the valid rarely used value to NULL */UPDATE my_table SET my_datetime_col = NULL WHERE my_datetime_col = '5001-01-01 00:00:00';

For Date Columns only – just changed the CAST(my_date_col AS CHAR(20))  to CAST(my_date_col AS CHAR(10))


/** ---
* For DATE Columns
*//* Step 1) Set all 0000-00-00... to a valid but a date which is rarely used i.e  5001-01-01 value: */UPDATE my_table SET my_date_col = '5001-01-01' WHERE CAST(my_date_col AS CHAR(10)) = '0000-00-00';

/* Step 2) Modify the column to DEFAULT: NULL */ALTER TABLE my_table MODIFY COLUMN my_date_col DATE NULL;

/* Step 3) Revert the valid nonsense value to NULL */UPDATE my_table SET my_date_col = NULL WHERE my_date_col = '1001-01-01';

2. SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘0’ for key ‘PRIMARY’

This case arise when we exported the MySQL database directly from live server to our local database. As, the database is large, i was not able to do the migrations.

Error : SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘0’ for key ‘PRIMARY’.  

Cause: This issue arose because while exporting from the live server Primary key with AUTO_INCREMENT clause was not exported correctly with the tables. 


Solution: We should add the AUTO_INCREMENT clause to the PRIMARY KEY of the table. Below is the syntax for the same

Syntax is

ALTER TABLE `TABLE_NAME`
CHANGE COLUMN `COLUMN_NAME` `COLUMN_NAME` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

So, in the below manner, we resolved the issue.

ALTER TABLE `test_has_selected_questions`
CHANGE COLUMN `id` `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

Note: If there is any row with the id value “0”. Please delete the row first, then go for running the above command.


Read more on MySQL Interview Questions and Answers!


 

jyoti rani

Recent Posts

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

2 days ago

Confused About Where to Buy Toys in India for your little one? We’ve Got You Covered

Looking for the perfect toy for your child can feel overwhelming with so many choices in today’s market. Whether you're…

3 days ago

A New Age of Play: Exploring the Magic of a Toy Shop in Delhi

In the heart of every child lies a world bursting with imagination — and the key to unlocking that world…

4 days ago

Creating Smiles with Play: The Magic of a Toy Store in Noida

In the fast-paced world of growing cities and digital lifestyles, children need spaces where their imagination is free to roam.…

5 days ago

A World of Wonder: Inside the Most Delightful Toy Store in Delhi

In a city as vibrant and diverse as Delhi, there’s a magic that lives beyond the monuments, food stalls, and…

6 days ago

The Magic of Play: Exploring the Best Toy Shop in Noida and Beyond

In the vibrant and rapidly developing city of Noida, playtime is getting smarter, more imaginative, and more inclusive than ever…

1 week ago