-1

So I tried to do a ALTER TABLE command to my table to add a date column that stores what time a post was made. Whenever I enter the SQL code, it pops up this error in PhpMyAdmin. I'm a beginner and I would really like if someone could help me.

Original code:

ALTER TABLE posts 
ADD date datetime not null;

Error that pops up: #1292 - Incorrect date value: '0000-00-00' for column 'website' . 'posts' . 'date' at row 1

Shadow
  • 33,525
  • 10
  • 51
  • 64
CHUCKVM25
  • 1
  • 2

2 Answers2

0

Give a default value

ALTER TABLE posts ADD `date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP

Or if default value is not desired, add the column allowing NULL, update with appropriate values, and change the column to NOT NULL

ALTER TABLE posts ADD `date` datetime
;
UPDATE posts 
SET `date` = NOW() -- or any suitable values
;
ALTER TABLE posts CHANGE `date` `date` datetime NOT NULL
;
ProDec
  • 5,390
  • 1
  • 3
  • 12
0

You are adding a column, that can't be null. So what value do the existing rows get?

You need to either specify a default value, or allow null until its populated somehow.

danblack
  • 12,130
  • 2
  • 22
  • 41