mysql can not insert because no default value?

Most probably, the default for column price is missing in the second database. To check this you should output your table structure:

describe database2.t_product;

OR

show create table database2.t_product;

and check if the default is defined.

You can alter your table and add the missing default constraint like this:

ALTER TABLE database2.t_product MODIFY COLUMN decimal(6,2) NOT NULL DEFAULT 0

EDIT

Based on comments and specification (data type default values), I think there is a difference in sql_mode of the MySQL:

For data entry into a NOT NULL column that has no explicit DEFAULT clause, if an INSERT or REPLACE statement includes no value for the column, or an UPDATE statement sets the column to NULL, MySQL handles the column according to the SQL mode in effect at the time:

If strict SQL mode is enabled, an error occurs for transactional tables and the statement is rolled back. For nontransactional tables, an error occurs, but if this happens for the second or subsequent row of a multiple-row statement, the preceding rows will have been inserted.

If strict mode is not enabled, MySQL sets the column to the implicit default value for the column data type.

So, if strict mode is not enabled for the first database, INSERT/UPDATE is allowed and storing the default value of that type (a 0 decimal)


ERROR 1364 (HY000): Field 'price' doesn't have a default value

price decimal(6,2) NOT NULL,

Set price to null or assign a default value

EDIT:

This is caused by the STRICT_TRANS_TABLES SQL mode.

Open phpmyadmin and goto More Tab and select Variables submenu. Scroll down to find sql mode. Edit sql mode and remove STRICT_TRANS_TABLES Save it.

OR

You can run an SQL query within your database management tool, such as phpMyAdmin:

-- verify that the mode was previously set:
SELECT @@GLOBAL.sql_mode;
-- update mode:
SET @@GLOBAL.sql_mode= 'YOUR_VALUE';

OR

Find the line that looks like so in the mysql conf file:

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

Comment above line out and restart mysql server

Tags:

Mysql