创建医生信息表 doctor_info1 、doctor_info2 ,区别是 doctor_info1 中doctor_name 加上not null 约束
CREATE TABLE `doctor_info1` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `doctor_name` varchar(32) NOT NULL, `doctor_code` varchar(32) DEFAULT NULL, `dept_code` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COMMENT='医生信息表'; CREATE TABLE `doctor_info2` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `doctor_name` varchar(32) DEFAULT NULL, `doctor_code` varchar(32) DEFAULT NULL, `dept_code` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='医生信息表';


不使用Null 的好处
SELECT * FROM doctor_info2 WHERE doctor_name != '王平'

2、单列索引不存null值,复合索引不存全为null的值,如果列允许为null,可能会得到 不符合预期的结果集
3、如果两个字段进行拼接:比如 doctor_code + dept_code,首先要各字段进行非null判断,否则只要有任意一个字段为空都会导致拼接的结果为null。比如使用 concat 函数拼接时同理。


4、如果有 Null column 存在的情况下,count(Null column)需要格外注意,null 值不会参与统计.
注意 Null 字段的判断方式, = null 将会得到错误的结果(用is null 、is not null 来判断)。
5、Null 列需要更多的存储空间:需要一个额外字节作为判断是否为 NULL 的标志位。
Mysql 中设置 not null 和 DEFAULT 约束 对象入参为null的情况:
1. 在代码中给对象设置默认值
2. 手写sql insert 语句,对象中为null 不需要入库的字段 不在sql中插入
INSERT INTO fms_order_statement (statement_batch_number, batch_type, batch_type_name, store_name, store_type_name, area_name, statement_date) VALUES
(#{item.statementBatchNumber}, #{item.batchType},#{item.batchTypeName}, #{item.storeName},#{item.storeTypeName},#{item.areaName},#item.statementDate})
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/225140.html原文链接:https://javaforall.net
