MySQL #1364 - Field 'column_name' doesn't have a default value - Can't insert into DB [duplicate]

[亡魂溺海] 提交于 2019-12-01 15:34:55

问题


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.

What can i do to make the "Default" choose it's own value?


回答1:


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)

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.

It means you have 2 options:

  1. Mark your field as NULL (first check if your field is required to have some value or not).

    ALTER TABLE your_table CHANGE COLUMN your_field your_field VARCHAR(250) NULL;

  2. 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 COLUMN your_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.



来源:https://stackoverflow.com/questions/39674404/mysql-1364-field-column-name-doesnt-have-a-default-value-cant-insert-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!