When import mysql data suggest Field id does not have a default value solutions

forward from:

https://www.cnblogs.com/os-python/p/6773499.html  

https://www.jb51.net/article/42883.htm

---------------------------------------------------------------------------------------------------

  • Project uses django + mysql
  • Used in linux is mysql5.7, import the data suggest: Field * does not have a default value
  • Want to solve the problem you need to know in mysql5.7, strict mode is enabled:
  • /etc/mysql/my.cnf found in the configuration file:
  • sql-model=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
  • Osamuaratame为: sql-mode = NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION
  • Then restart the mysql, re-synchronization model to the database

This problem occurs reasons:

  1. In mysql5.0.2 ago, mysql is not harsh on the value of inspection is illegal, for data entry and coerces them to legal values.
  2. 5.0.2 In a future release, retains the previous default behavior, but you can choose the value of more traditional approach is bad, so that the server can refuse to give up the statement and poor value occurs.

mysql default behavior and strict new sql mode and differences:

  1. If you are not using strict mode, the following situation is legal. If an incorrect value into a column, such as a null value into the non-null columns, or oversized data into numeric columns, mysql sets the column as the most likely value, rather than throwing an error message.
  2. If the view is stored over a range of values ​​into a numeric column, mysql server saves 0 (the smallest possible value) instead, or the maximum possible value.
  3. For strings, mysql stores either the empty string, or string may save more portions of the column.
  4. If you plan will not save the beginning of the string value into a numeric column, mysql will save 0.
  5. mysql allows saving a specific value to the incorrect dates and DATE DATETIME column (eg: "2000-02-31" or "2000-02-00"). The idea is that instead of sql server to verify the date of your values. If mysql can store a date value and retrieve exactly the same value, mysql will be able to save a given value of it. If the date is wrong (outside the server's ability to store it) will save a special date values ​​in the column "0000-00-00" instead.
  6. If the view is saved null value to the column does not accept null values ​​for single-row insert statement, an error will occur. For multi-row insert statement or insert into ... select statement, mysql server stores the implicit default value for the column data type. In general, for the numeric types, it is zero, for string types, and the empty string ( ""), date and time for the type "zero".
  7. If you insert statement does not specify a value for a column, if the column definition includes an explicit default clause, mysql inserts its default value. If no such default clause in the definition, mysql implicitly Default data type of a column inserted.
  8. The reason for using the rules described before that, before the statement started, can not check these conditions. If you experience this problem after updating a comfortable flight, we can not just roll back, because the storage engine may not support rollback. Planting statement is not a good choice, in this case, the update is complete a "half", which is perhaps the worst-case scenario. For this example, a better approach is to "do the best possible," as if nothing had happened so continue.

In a future version 5.0.2 mysql, you can use STRICT_TRANS_TABLES or STRICT_ALL_TABLES sql mode, choose a more stringent approach.

STRICT_TRANS_TABLES works:

  1. For things storage engines, bad data anywhere in the statement value Junhui cause the statement to abort and roll back.
  2. For non-volatile storage engine of things, if something goes wrong the first line and then to insert or update statement aborts. (In this case, the statement can be considered not change the table, just like the same things table). Error occurs after the first line does not lead to forgo executing a statement. Instead, bad data values ​​will be adjusted, and a warning, rather than an error. In other words, the use of STRICT_TRANS_TABLES, mysql error value causes rollback operation, if possible, all updates done so far.

To perform more stringent checks, enable STRICT_ALL_TABLES. In addition to non-transactional storage engine, it STRICT_TRANS_TABLES equivalent, even for bad data in rows following the first row, can lead to errors generated by executing a statement to give up. This means that if an error occurs in the non-transactional table or insert multiple rows into the update process, the update is only partial results, completed the front row will be inserted or updated, but the point behind the behavior of error is not. For non-transactional tables, in order to avoid this from happening, a single statement can be used, or the energy conversion warnings rather than error is used at STRICT_TRANS_TABLES. To prevent the emergence of the first case of problem, do not use mysql to check the contents of the column. The safest way is to let the application is responsible, passes only legal values ​​to the database.

Once you have strict mode options, use the insert or update ignore ignore rather than insert or update without ignore will treat the error as a warning.

After the above operation is not resolved, it is not mysql configuration problem.

  1. Table see if it is not connected to a database table that you want to operate
  2. That is, when creating the table id is not set increment (that's my mistake, because before had any contact with MySQL, so there is this error may curriculum, students should come to know)
  • Add When you create a table: create table table1 (id int auto_increment primary key, ...)
  • After you create a table added: alter table table1 add id int auto_increment (table1 is the table name; primary key increment field, be sure to set as primary key.)
  • Many times the data in the table id hope not to start at 1, like qq, id 10000 from the beginning: alter table users AUTO_INCREMENT = 10000;
  • And the statement also applies to the id modify an existing table, such as the large quantities of data to delete, want to return id 654321 123456 from the beginning: alter table users AUTO_INCREMENT = 123456;

 

Guess you like

Origin blog.csdn.net/liuhongbin2011net/article/details/89632861