SQL NOT NULL 約束
sql not null 約束
not null 約束強制列不接受 null 值,強制字段必須包含非null值。如果不向字段添加值,就無法插入新記錄或者更新記錄。
在默認(rèn)的情況下,數(shù)據(jù)庫中表的列接受 null 值。
下面的 sql 強制 "id" 列、 "lastname" 列以及 "firstname" 列不接受 null 值:
create table persons ( id int not null, lastname varchar(255) not null, firstname varchar(255) not null, age int );
1. 添加 not null 約束
在一個已創(chuàng)建的表的 "age" 字段中添加 not null 約束如下所示:
alter table persons modify age int not null;
2. 刪除 not null 約束
在一個已創(chuàng)建的表的 "age" 字段中刪除 not null 約束如下所示:
alter table persons modify age int null;