Study Notes (06): mySQL database development tutorial - domain integrity - not null constraint

Learning immediately: 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 after the specified non-null columns, add nulls not allowed to add record success;

Although mySQL specify the non-empty column, add error record is not a null value, such as the column is numeric 0 is inserted, is inserted into the character '' (not empty but no content 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 

 

 

Published 15 original articles · won praise 0 · Views 91

Guess you like

Origin blog.csdn.net/weiying_zh/article/details/105273509