Why can I insert null into mysql field which is varchar not null

I created a table in mysql

create table test (id int primary key not null auto_increment, vs varchar(255) not null); 

at startup

 insert into test (vs) values (null); 

Gives an error message:

 ERROR 1048 (23000): Column 'vs' cannot be null 

But when I try to insert two lines

 insert into test (vs) values (null),(null); 

It works, and the result is:

 mysql> select * from test; +----+----+ | id | vs | +----+----+ | 1 | | | 2 | | +----+----+ 

The vs field is not NULL, I wonder if this is a function.

+5
source share
1 answer

This is not a mistake or function. It is just how it works in this case. This behavior is described in MySQL Documentation - Restrictions on Invalid Data .

If you try to save NULL in a column that is not NULL, an error occurs for single-line INSERT statements . For multiple INSERT statements or for INSERT INTO ... SELECT statements, MySQL Server maintains an implicit default value for the column data type. In general, this is a number 0 for numeric types, an empty string ('') for string types, and a "zero" value for date and time types. Implicit defaults are discussed in section 11.7, "Default Values ​​for a Data Type."

This happens when you turn off strict mode.

+5
source

Source: https://habr.com/ru/post/1271633/


All Articles