mysql 允许在唯一索引的字段中出现多个null值

线上问题:org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [update fl_table set id = ?, password = ?, email = ? where id = '3583954800']; Duplicate entry ' ' for key 'email'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry ' ' for key 'email

更新email字段为空时出现错误,查数据库发现表的email字段加了唯一索引,最多有一条为空或者其他值的数据

解决办法:唯一索引可在索引列插入多次null

email可以有多条email为null的记录,将冲突的字段设置为null,解决

在sql server中,唯一索引字段不能出现多个null值

在mysql 的innodb引擎中,是允许在唯一索引的字段中出现多个null值的。

根据NULL的定义,NULL表示的是未知,因此两个NULL比较的结果既不相等,也不不等,结果仍然是未知。根据这个定义,多个NULL值的存在应该不违反唯一约束,所以是合理的,在oracel也是如此。

猜你喜欢

转载自www.cnblogs.com/zhouj850/p/11093589.html