Questions about the mysql notnull field

Source: Internet
Author: User
Concerning the notnull field of mysql, I encountered a very bad problem today. for mysql database, an integer field with notnull set, the default value is 2, when I use the insert statement to insert a null value to this field, why is the default integer value 0 automatically generated by the system instead of the default value 2 that I have defined, if I do not question this field about the not null field of mysql
Today, I encountered a very bad problem. for mysql databases, an integer field with not null is set. the default value is 2, when I use the insert statement to insert a null value to this field, why is the default integer value 0 automatically generated by the system instead of the default value 2 that I have defined, if I do not perform any operation on this field, the custom default value will be automatically filled. why? please kindly advise. thank you ....

------ Solution --------------------
Null is also a value, that is, the default value is not used. If you leave it blank, the default value is used.
This is easy to understand.
------ Solution --------------------
Null indicates nothing
Since the value type field is set not null
So nothing is in mathematics, isn't it 0?
------ Solution --------------------
If you customize the default value, do not set it to not NULL. This field is not included during insertion, which is the default value.
If it cannot be empty, you have to insert a value, even if it is a null value. if it is inserted, the default value will be useless.
------ Solution --------------------
Discussion

WorkId 'Int (10) unsigned not null default '3'

Why does it become 0 when I insert a null value to this field?

Reference:

If you customize the default value, do not set it to not NULL. This field is not included during insertion, which is the default value.
If it cannot be empty, you have to insert a value, even if it is a null value. if it is inserted, the default value will be useless.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.