一张图告诉你SQL中null和not null的用法与区别
/*
本集内容:
1、特殊值NULL
数据库中的null代表不确定的值 也就是不知道是什么
2、NULL和空值的区别
NULL代表不确定未知的 '' 代表空 0 代表的是一个数值
3、怎么查询出NULL和非NULL值
*/
--为空性约束 null 可为空 和 not null 不为空
create table test
(
TID int not null,--这个TID这个字段是不可以为空的
TNAME nvarchar(20) null,--可以为空
TCOUNT int null,
TREMARK nvarchar(20) not null
)
select * from test
insert into test values(1,'zhangsan',10,'')
--添加失败 因为TID不允许为null所以添加失败
insert into test values(null,'lisi',50,'备注')
insert into test values(2,null,50,'备注')
insert into test values(3,null,null,'备注')
--以下添加错误因为TREMARK 不允许为空
insert into test values(3,'小白',null,null)
insert into test(TID,TREMARK) values(20,'hahaha')
--查询null 和 not null
select * from test where TNAME is not null
--查询字段是否是null值 用 is null / is not null
select * from test where TNAME is null
--单纯的查询''
select * from test where TREMARK=''
留言与评论(共有 0 条评论) “” |