Home Database Mysql Tutorial 为什么mysql字段要设置为not null?

为什么mysql字段要设置为not null?

Jun 07, 2016 pm 03:47 PM
mysql NOT null Why Field set up

为什么mysql字段要设置为 not null ? 现在 大师来跟大家解释解释 首先 我们创建一个表 asd 字段为 null , qwe 字段为 not null ,都无默认 CREATE TABLE `tb` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,`asd` VARCHAR(45) NULL,`qwe` VARCHAR(45) NO

为什么mysql字段要设置为not null?
现在 大师来跟大家解释解释

首先 我们创建一个表 asd字段为nullqwe字段为not null,都无默认值

CREATE TABLE `tb` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`asd` VARCHAR(45) NULL,
`qwe` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idtb`)
)
Copy after login
然后
insert tb(asd) values('123123123');
insert tb(qwe) values('123123123');
Copy after login


现在 我们来查看这个表
请输入图片描述
可以看出
not null 的qwe字段 未设置的时候是个空字符串
null 的asd字段 未设置的时候是个NULL

以上结果 大家应该都可以想像到的。
那么为什么要设置NOT NULL呢
因为 MYSQL不是PHP,NULL不等于空字符串
比如我们查询qwe为空的

select * from tb where qwe=''
可以找到
请输入图片描述
但是

select * from tb where asd=''
则什么都找不到

而我们WEB开发的时候,果然有的地方插入记录 是指定空字符串,有的地方插入记录没有指定值,如果字段没设置NOT NULL,那就是出现:
请输入图片描述

而你查找的时候,只会出现
请输入图片描述

所以 要设置 NOT NULL
这个 就不会有NULL出现了。
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to fine-tune deepseek locally How to fine-tune deepseek locally Feb 19, 2025 pm 05:21 PM

How to fine-tune deepseek locally

deepseek why can't you log in deepseek login portal deepseek why can't you log in deepseek login portal Feb 19, 2025 pm 05:00 PM

deepseek why can't you log in deepseek login portal

Why can't the Bybit exchange link be directly downloaded and installed? Why can't the Bybit exchange link be directly downloaded and installed? Feb 21, 2025 pm 10:57 PM

Why can't the Bybit exchange link be directly downloaded and installed?

Free market software app website Free market software app website Mar 05, 2025 pm 09:03 PM

Free market software app website

Why is Bittensor said to be the 'bitcoin' in the AI ​​track? Why is Bittensor said to be the 'bitcoin' in the AI ​​track? Mar 04, 2025 pm 04:06 PM

Why is Bittensor said to be the 'bitcoin' in the AI ​​track?

gateio exchange app old version gateio exchange app old version download channel gateio exchange app old version gateio exchange app old version download channel Mar 04, 2025 pm 11:36 PM

gateio exchange app old version gateio exchange app old version download channel

Bitcoin: The 'barometer' of global liquidity? Bitcoin: The 'barometer' of global liquidity? Mar 04, 2025 pm 06:39 PM

Bitcoin: The 'barometer' of global liquidity?

BitMEX: Best option strategy after a big sell-off BitMEX: Best option strategy after a big sell-off Mar 04, 2025 pm 06:27 PM

BitMEX: Best option strategy after a big sell-off

See all articles