MySQL #1364 - 字段“column_name”没有默认值 - 无法插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39674404/
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
MySQL #1364 - Field 'column_name' doesn't have a default value - Can't insert into DB
提问by NIKO_B
I recently moved my MySQL database to a new server and it has given me some problems i have newer experienced with MySQL before. My table columns are set to "Default => None" and my table has been generating the default value depending on the Datatype. But now when i try to insert into a table i'm getting this error message: "#1364 - Field 'column_name' doesn't have a default value" and then nothing is inserted into the table.
我最近将我的 MySQL 数据库移到了一个新服务器,它给了我一些我以前在 MySQL 上遇到过的新问题。我的表列设置为“默认 => 无”,并且我的表已根据数据类型生成默认值。但是现在当我尝试插入表时,我收到此错误消息:“#1364 - 字段 'column_name' 没有默认值”,然后表中没有插入任何内容。
What can i do to make the "Default" choose it's own value?
我该怎么做才能让“默认”选择它自己的价值?
回答by AwesomeGuy
It's not saving into database definetly because the field 'column_name' (and maybe some others) is checked as "NOT NULL". It means that the value of that field must be something other that NULL (NULL - no data at all)
它没有明确地保存到数据库中,因为字段“column_name”(可能还有其他一些)被检查为“NOT NULL”。这意味着该字段的值必须是 NULL 以外的值(NULL - 根本没有数据)
Marking fields as not null is usually a great way to ensure that some data will always be present in the field. Depending on your needs, you can also mark it as NULL so it will never throw an error and will save into DB without the need for anything to be inserted into a specified field.
将字段标记为非空通常是确保某些数据始终存在于字段中的好方法。根据您的需要,您还可以将其标记为 NULL,这样它就永远不会抛出错误并将保存到数据库中,而无需将任何内容插入到指定字段中。
It means you have 2 options:
这意味着您有 2 个选择:
Mark your field as NULL (first check if your field is required to have some value or not).
ALTER TABLE
your_table
CHANGE COLUMNyour_field
your_field
VARCHAR(250) NULL;Add a default value to the field so if no data is provided on insert, it will put something you defined. For example:
ALTER TABLE
your_table
CHANGE COLUMNyour_field
your_field
VARCHAR(250) NOT NULL DEFAULT 'some_default_value';And ofcourse match your field type to the field your are going to change.
将您的字段标记为 NULL(首先检查您的字段是否需要具有某些值)。
ALTER TABLE
your_table
CHANGE COLUMNyour_field
your_field
VARCHAR(250) NULL;为该字段添加一个默认值,这样如果插入时没有提供数据,它将放入您定义的内容。例如:
ALTER TABLE
your_table
CHANGE COLUMNyour_field
your_field
VARCHAR(250) NOT NULL DEFAULT 'some_default_value';当然,将您的字段类型与您将要更改的字段相匹配。