Why is the mysql field set to notnull?

Source: Internet
Author: User
Why is the mysql field set to notnull? Now, let's explain to you. First, we create a table with the asd field being null and the qwe field being notnull. No Default CREATETABLE 'tb' ('id' INT (10) UNSIGNEDNOTNULLAUTO_INCREMENT, 'asd 'varchar (45) NULL, 'Qi' VARCHAR (45) NO

Why should I set the mysql field to not null? Now, the master will explain to you. First, we CREATE a TABLE. The asd field is null, And the qwe field is not null. No Default create table 'tb' ('id' INT (10) unsigned not null AUTO_INCREMENT, 'asd 'VARCHAR (45) NULL, 'Qi' VARCHAR (45) NO

Why should I set the mysql fieldnot null?
Now, I want to explain this to you.

First, create a table.asdThe field isnull,qweThe field isnot null, No Default Value

CREATE TABLE `tb` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,`asd` VARCHAR(45) NULL,`qwe` VARCHAR(45) NOT NULL,PRIMARY KEY (`idtb`))
Then

insert tb(asd) values('123123123');insert tb(qwe) values('123123123');


Now let's look at this table. Please input the image description. We can see that if the qwe field of not null is not set, it is an empty string. If the asd field is not set, it is a null value. can be imagined. So why should we set not null? Because MYSQL is not php, NULL is not equal to an empty string. For example, if we query the select * from tb where qwe = ''where qwe is empty, enter the image description, but select * from tb where asd ='' indicates all but when we develop the WEB, sure enough, some insert records specify NULL strings, and some insert records do NOT specify a value. If the field is not null, it is displayed: Enter the image description, enter the image description. Therefore, if you want to set not null, no NULL will appear.

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.