作用:限定某一列的值不能为 null

关键词: not null

常用的方式,直接再创建表的时候添加约束

  1. create table nn(
  2. id int,
  3. username varchar(32) not null
  4. );
  5. insert into nn values(1,'tom');-- 成功
  6. insert into nn values(2,'tom');-- 成功
  7. insert into nn values(3,null);-- 失败 username不能为null

总结

  1. 在开发中一般不添加not null
  2. 可以使用java代码来控制非空