最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

MySQLinsertsduplicaterecordsdespiteuniqueindex|Lease_MySQL

来源:动视网 责编:小采 时间:2020-11-09 20:03:44
文档

MySQLinsertsduplicaterecordsdespiteuniqueindex|Lease_MySQL

MySQLinsertsduplicaterecordsdespiteuniqueindex|Lease_MySQL:Okay, so I thought I knew quite a bit about databases and so I added a unique index. For those who do not know: A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a ne
推荐度:
导读MySQLinsertsduplicaterecordsdespiteuniqueindex|Lease_MySQL:Okay, so I thought I knew quite a bit about databases and so I added a unique index. For those who do not know: A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a ne
 Okay, so I thought I knew quite a bit about databases and so I added a unique index. For those who do not know:

A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. – MySQL manual

I added the unique index to my table that made it look like this:

CREATE TABLE `table` (`a` int(11) DEFAULT NULL,`b` int(11) DEFAULT NULL,`c` int(11) DEFAULT NULL,UNIQUE KEY `a_b_c` (`a`,`b`,`c`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

I was trying to insert or increment values in it like this:

INSERT INTO `table` (a,b,c) VALUES (1,NULL,3),(1,NULL,3) ON DUPLICATE KEY UPDATE c=c+VALUES(c);

I expected to get the following result:

mysql> select * from `table`;+------+------+------+| a| b| c|+------+------+------+|1 | NULL |6 |+------+------+------+1 row in set (0.00 sec)

But it did not do that! I got this result instead:

mysql> select * from `table`;+------+------+------+| a| b| c|+------+------+------+|1 | NULL |3 ||1 | NULL |3 |+------+------+------+2 rows in set (0.00 sec)

I was clueless until I read the documentation of MySQL a little better:

This constraint does not apply to NULL values except for the BDB storage engine. For other engines, a UNIQUE index permits multiple NULL values for columns that can contain NULL. – MySQL manual

Especially the part where it says: “does not apply to NULL values” makes things much, much clearer. Note that Microsoft SQL Server behaves different (they way I expected). So keep this is mind when using a unique index in MySQL, because I certainly did not expect this behavior!

文档

MySQLinsertsduplicaterecordsdespiteuniqueindex|Lease_MySQL

MySQLinsertsduplicaterecordsdespiteuniqueindex|Lease_MySQL:Okay, so I thought I knew quite a bit about databases and so I added a unique index. For those who do not know: A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a ne
推荐度:
标签: 重复 mysql duplicate
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top