简体   繁体   English

如何修复这个 MYSQL//Phpmyadmin 错误?

[英]How do I fix this MYSQL//Phpmyadmin error?

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.所以我尝试对我的表执行 ALTER TABLE 命令以添加一个日期列来存储发布的时间。 Whenever I enter the SQL code, it pops up this error in PhpMyAdmin.每当我输入 SQL 代码时,它都会在 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' .弹出的错误:#1292 - 日期值不正确:“网站”列的“0000-00-00”。 'posts' . '帖子' 。 'date' at row 1第 1 行的“日期”

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或者,如果不需要默认值,请添加允许NULL的列,使用适当的值更新,并将该列更改为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
;

You are adding a column, that can't be null .您正在添加一列,该列不能为null So what value do the existing rows get?那么现有的行得到什么值呢?

You need to either specify a default value, or allow null until its populated somehow.您需要指定一个默认值,或者在以某种方式填充之前允许null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM