mysql - 我可以将 VARCHAR 默认值设置为 NULL 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3600960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:00:29  来源:igfitidea点击:

mysql - can I set VARCHAR default value as NULL?

mysqlmysql-error-1067

提问by tzmatt7447

In mysql, i tried changing an existing table like this:

在 mysql 中,我尝试像这样更改现有表:

  ALTER TABLE  `etexts` CHANGE  `etext`  `etext` VARCHAR( 100 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT NULL

I got the response:

我得到了回应:

  #1067 - Invalid default value for 'etext' 

Why?

为什么?

回答by Lekensteyn

It's contradictive... NOT NULL, but make it default NULL...
Remove DEFAULT NULLand change NOT NULLto NULL:

这是矛盾的... NOT NULL,但将其设为默认NULL...
删除DEFAULT NULL并更改NOT NULLNULL

ALTER TABLE  `etexts` CHANGE  `etext`  `etext` VARCHAR( 100 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL;

回答by StuartLC

You can't have a NOT NULL column defaulting to NULL.

您不能将 NOT NULL 列默认为 NULL。

If you want it to be NULLable then

如果你希望它是可空的,那么

... COLLATE latin1_swedish_ci NULL 

NULLABLE columns will default to NULL automagically if no value is provided for the column

如果没有为列提供值,NULLABLE 列将自动默认为 NULL