使用null值
null 值就是没有值或缺值。允许 null 值的列也允许在插入行时不给出该列的值。不允许 null 值的列不接受该列没有值的行,换句话说,在插入或更新行时,该列必须有值。
每个表列或者是 null 列,或者是 not null 列,这种状态在创建时由表的定义规定。请看下面的例子:
输入:
create table orders
(
order_num int not null auto_increment,
order_date datetime not null,
cust_id int not null,
primary key (order_num)
)engine = innodb;
分析:这条语句创建本书中所用的 orders 表。 orders 包含3个列,分别是订单号、订单日期和客户id。所有3个列都需要,因此每个列的定义都含有关键字 not null 。这将会阻止插入没有值的列。如果试图插入没有值的列,将返回错误,且插入失败。
下一个例子将创建混合了 null 和 not null 列的表:
输入:
create table vendors
(
vend_id int not null auto_increment,
vend_name char(50) not null,
vend_address char(50) null,
vend_city char(50) null,
vend_state char(5) null,
vend_zip char(10) null,
vend_country char(50) null,
primary key (vend_id )
)engine = innodb;
分析:这条语句创建本书中使用的 vendors 表。供应商id和供应商名字列是必需的,因此指定为 not null 。其余5个列全都允许 null 值,所以不指定 not null 。 null 为默认设置,如果不指定 not null ,则认为指定的是 null 。
mysql null和空的区别
理解 null 不要把 null 值与空串相混淆。 null 值是没有值,它不是空串。如果指定 '' (两个单引号,其间没有字符),这在 not null 列中是允许的。空串是一个有效的值,它不是无值。 null 值用关键字 null 而不是空串指定。
以上就是关于mysql数据表中null值的详解的详细内容。