教程 > sql 教程 > 阅读:20

sql not null 约束——迹忆客-ag捕鱼王app官网

默认情况下,列可以包含 null 值。如果我们不希望列具有 null 值,那么就需要在此列上定义这样的约束,指定不允许该列使用 null。

null 与没有数据不同,它代表未知数据。

示例

例如,以下 sql 创建一个名为 customers 的新表并添加五列,我们指定其中三列 id, name 和 age不能为 null

create table customers(
   id   int              not null,
   name varchar (20)     not null,
   age  int              not null,
   address  char (25) ,
   salary   decimal (18, 2),       
   primary key (id)
);

如果是已经创建了 customers 表,那么我们又要想对 salary 字段添加 not null 约束,这要怎么办呢?这里我们就要使用 alter table 语句来对表结构进行修改了。如下所示

alter table customers
   modify salary  decimal (18, 2) not null;

查看笔记

扫码一下
查看教程更方便
网站地图