当前位置:网站首页>数据库系统原理与应用教程(033)—— MySQL 的数据完整性(六):非空(NOT NULL)约束
数据库系统原理与应用教程(033)—— MySQL 的数据完整性(六):非空(NOT NULL)约束
2022-07-20 01:43:00 【睿思达DBA_WGX】
数据库系统原理与应用教程(033)—— MySQL 的数据完整性(六):非空(NOT NULL)约束
一、NULL 的特点
关于 NULL,MySQL 官方解释如下:
NULL columns require additional space in the row to record whether their values are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.(NULL 列需要行中的额外空间来记录它们的值是否为空。对于 myisam 表,每个 NULL 列需要额外的一位,四舍五入到最接近的字节。)
这说明,MySQL 中的 NULL 其实是占用空间的。例如:
/* CREATE TABLE t21( id int primary key, name VARCHAR(10) NOT NULL, addr VARCHAR(100) NULL ) ENGINE = MYISAM; */
mysql> CREATE TABLE t21(
-> id int primary key,
-> name VARCHAR(10) NOT NULL,
-> addr VARCHAR(100) NULL
-> ) ENGINE = MYISAM;
Query OK, 0 rows affected (0.04 sec)
-- 插入数据,当 name 列的值为 null 时导致插入失败
mysql> insert into t21 values(1,null,'河南省新乡市');
ERROR 1048 (23000): Column 'name' cannot be null
-- 空字符串可以插入,因此 null 和空字符串不相同
mysql> insert into t21 values(1,'','河南省新乡市');
Query OK, 1 row affected (0.05 sec)
mysql> select * from t21;
+----+------+--------------------+
| id | name | addr |
+----+------+--------------------+
| 1 | | 河南省新乡市 |
+----+------+--------------------+
1 row in set (0.02 sec)
如果一个列允许取 NULL 值会存在如下问题:
(1)表索引时不存储 NULL 值,如果索引字段可以为 NULL,索引的效率会下降。
(2)当数据和 NULL 值进行数学运算和比较运算时,结果仍然为 NULL。因此,要判断一个数据是否为 NULL 值,必须用 IS NULL 或 IS NOT NULL 来判断。
(3)建议在创建表时,所有列都设置为不允许取空值(NOT NULL)。
二、定义非空约束
创建表时,所有字段默认可以取空值,如果需要将某个字段定义为不允许取空值,可以使用非空约束(NOT NULL)。
语法格式如下:
create table 表名(
列名 类型 not null,
....
);
例如:创建表 t31,所有列都不允许取空值
/* create table t31( e_id int primary key, e_name char(20) not null, gender char(1) not null, jobtime datetime not null, salary int not null, phone char(20) not null, addr varchar(100) not null ); */
mysql> create table t31(
-> e_id int primary key,
-> e_name char(20) not null,
-> gender char(1) not null,
-> jobtime datetime not null,
-> salary int not null,
-> phone char(20) not null,
-> addr varchar(100) not null
-> );
Query OK, 0 rows affected (0.09 sec)
mysql> desc t31;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| e_id | int(11) | NO | PRI | NULL | |
| e_name | char(20) | NO | | NULL | |
| gender | char(1) | NO | | NULL | |
| jobtime | datetime | NO | | NULL | |
| salary | int(11) | NO | | NULL | |
| phone | char(20) | NO | | NULL | |
| addr | varchar(100) | NO | | NULL | |
+---------+--------------+------+-----+---------+-------+
7 rows in set (0.03 sec)
插入数据,所有列都必须指定数据。
mysql> insert into t31 values(1001,'王大伟','男','1998-7-1',10200,'13737325689','河南省新乡市');
Query OK, 1 row affected (0.06 sec)
mysql> select * from t31;
+------+-----------+--------+---------------------+--------+-------------+--------------------+
| e_id | e_name | gender | jobtime | salary | phone | addr |
+------+-----------+--------+---------------------+--------+-------------+--------------------+
| 1001 | 王大伟 | 男 | 1998-07-01 00:00:00 | 10200 | 13737325689 | 河南省新乡市 |
+------+-----------+--------+---------------------+--------+-------------+--------------------+
1 row in set (0.02 sec)
-- 任何的列不不能为 null,否则插入失败
mysql> insert into t31 values(1002,'刘涛','男','1996-6-11',12200,'13703732544',null);
ERROR 1048 (23000): Column 'addr' cannot be null
三、取消非空约束
要使某个列允许取空值,则修改该列的属性,去掉 not null 选项即可。
例如:使 t31 表中的 addr 列允许为空。
mysql> desc t31;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| e_id | int(11) | NO | PRI | NULL | |
| e_name | char(20) | NO | | NULL | |
| gender | char(1) | NO | | NULL | |
| jobtime | datetime | NO | | NULL | |
| salary | int(11) | NO | | NULL | |
| phone | char(20) | NO | | NULL | |
| addr | varchar(100) | NO | | NULL | |
+---------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> alter table t31 modify addr varchar(100);
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t31;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| e_id | int(11) | NO | PRI | NULL | |
| e_name | char(20) | NO | | NULL | |
| gender | char(1) | NO | | NULL | |
| jobtime | datetime | NO | | NULL | |
| salary | int(11) | NO | | NULL | |
| phone | char(20) | NO | | NULL | |
| addr | varchar(100) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
插入数据,使 addr 为 null:
mysql> insert into t31 values(1002,'刘涛','男','1996-6-11',12200,'13703732544',null);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t31;
+------+-----------+--------+---------------------+--------+-------------+--------------------+
| e_id | e_name | gender | jobtime | salary | phone | addr |
+------+-----------+--------+---------------------+--------+-------------+--------------------+
| 1001 | 王大伟 | 男 | 1998-07-01 00:00:00 | 10200 | 13737325689 | 河南省新乡市 |
| 1002 | 刘涛 | 男 | 1996-06-11 00:00:00 | 12200 | 13703732544 | NULL |
+------+-----------+--------+---------------------+--------+-------------+--------------------+
2 rows in set (0.00 sec)
边栏推荐
- acwing 871. 约数之和
- Hashtable
- Understand the MySQL architecture design in one article, and don't worry about the interviewer asking too much
- 【求解AX=b】
- Animation animation in unity is played backwards and forwards
- QT_嵌入式主界面添加与切换效果
- 软件测试如何快速入门
- EasyCode(逆向工程)插件使用
- Detailed explanation of TestNG automated testing framework
- 网络安全技术的新趋势探讨
猜你喜欢
VMware startup error: exception 0xc00000005 and windwos11 have no Hyper-V solution
Hashtable
The stock problem was wiped out
Baidu PaddlePaddle easydl helps manufacturing enterprises with intelligent transformation
2022/7/19
Blurred photos, second high-definition big picture, flying propeller ppde takes you to reproduce the image restoration model cmfnet
DNS domain name resolution
Unity shader shader learning (2)
My book Oracle database in memory architecture and practice was published
EasyCode(逆向工程)插件使用
随机推荐
JSON【代码演示详解,带你精通 JSON】
類和對象(上)
Applet: page scrolling to a certain position navigation bar fixed effect
tkinter各种控件库控件创建速度比较
在线沙箱集合
How to recover the lost database password?
acwing 868. 筛质数
Unity shader shader learning (I)
Question 124 of Li Kou: maximum path sum in binary tree
数据库系统原理与应用教程(026)—— MySQL 修改表中数据(二):删(delete from)
机械制造企业,如何借助ERP系统解决仓库管理难题?
PPDE第二季度迎新 | 欢迎22位AI开发者加入飞桨开发者技术专家计划!
Question 128 of Li Kou: longest continuous sequence
Solution to remote access failure of docker installation MySQL in virtual machine
How to transfer a single node of the warehouse database to a cluster
【Pygame小游戏】魂斗罗经典BOSS都回来了 准备好再次击败他们了吗?(附源码)
[731. My schedule II]
Unity Shader着色器学习(二)
Detailed explanation of TestNG automated testing framework
实习打怪之路:nodejs中的npm全局安装和局部安装的意思,npm中的全局安装和局部安装的区别