为什么mysql字段要设置为not null?
Jun 07, 2016 pm 03:47 PM为什么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
字段为null
,qwe
字段为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`) )
insert tb(asd) values('123123123'); insert tb(qwe) values('123123123');
现在 我们来查看这个表 请输入图片描述 可以看出 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出现了。

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

deepseek why can't you log in deepseek login portal

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

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

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

Bitcoin: The 'barometer' of global liquidity?

BitMEX: Best option strategy after a big sell-off
