学习笔记(06):mySQL数据库开发教程-域完整性--非空约束

立即学习:https://edu.csdn.net/course/play/4364/77143?utm_source=blogtoedu

# 创建表时指定非空列
create table fk
(
sid int not null,
sname varchar(20) not null,
cardid varchar(20)
)

**SQL Server 指定非空列后,添加有空值的记录不允许添加成功;

mySQL 虽指定该列非空,添加有空值记录并不报错,如该列是数值型则插入0,是字符型则插入‘’(没有内容但不是空Null)。

insert into fk (sid, sname) values (1, null)  --报错,不允许插入空值

# 如何查询没有内容的记录?
select * from fk where sname=''

# 如何查询为空的记录?
select * from fk where cardid is null
# 增加非空约束
alter table fk modify column sid integer(11) NOT NULL

# 取消非空约束,增加默认值
alter table fk modify column sid integer(11) default NULL 
发布了15 篇原创文章 · 获赞 0 · 访问量 91

猜你喜欢

转载自blog.csdn.net/weiying_zh/article/details/105273509