MySQL 添加一个默认值不为空的列。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10960506/
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 13:45:41  来源:igfitidea点击:

Adding a column whose value is not null by default .

mysql

提问by Manoj Nayak

I have to add a column whose default value is not null by default to the table after particular column using Alter table.

我必须使用 Alter 表在特定列之后的表中添加一个默认值不为空的列。

ALTER TABLE tblechecklistrevision ADD COLUMN IWorkFlowOrder INT(10) DEFAULT NOT NULL AFTER fState;

When I Execute the query I will get the below error

当我执行查询时,我会收到以下错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT NULL AFTER fState' at line 1

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的“NOT NULL AFTER fState”附近使用的正确语法

回答by Nadir Sampaoli

You should remove DEFAULT:

你应该删除DEFAULT

ALTER TABLE tblechecklistrevision 
    ADD COLUMN IWorkFlowOrder INT(10) NOT NULL AFTER fState;

DEFAULT is for setting initial value to new rows where a value for that column isn't specified, when you write ...INT(10) NOT NULLwhat you mean is actually that that column can never contain a NULL, not only at initialization time.

DEFAULT 用于为未指定该列的值的新行设置初始值,当您写下...INT(10) NOT NULL您的意思时,实际上该列永远不能包含 NULL,不仅在初始化时。

回答by StPiere

If you want the default value not to equal NULL(example 0) you can do:

如果您希望默认值不等于NULL(示例 0),您可以执行以下操作:

ALTER TABLE tblechecklistrevision 
    ADD COLUMN IWorkFlowOrder INT(10) NOT NULL DEFAULT 0 AFTER fState