Bug #70718 Unique index allows duplicates with null values

Submitted: 24 Oct 2013 3:58 Modified: 24 Oct 2013 12:16
Reporter: Dimitriy A Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:MySQL 5.0 and up OS:Any
Assigned to: CPU Architecture:Any
Tags: duplicate, null, unique index

[24 Oct 2013 3:58] Dimitriy A
Description:
Revisit Bug #8173 (http://bugs.mysql.com/bug.php?id=8173).
I am not sure how comments are treated for bugs, which have been closed, so I am opening a new bug report. There were multiple reasons given as to why this is a bug, but lately there were no responses from MySQL developers. Could we have the SQL Standard reviewed again and provide a fix to the problem.

Description of the bug:
unique index allows duplicates if at least one of the columns is null.

How to repeat:
mysql> create table tb
    -> (
    ->  id      int not null auto_increment,
    ->  a       int,
    ->  b       int,
    ->  primary key (id),
    ->  index (a),
    ->  index (b),
    ->  unique index (a,b)
    -> ) type = innodb; /* occurs in myISAM tables as well */
Query OK, 0 rows affected, 1 warning (0.42 sec)

mysql> insert into tb (a,b) values (1,2);
Query OK, 1 row affected (0.00 sec)

mysql> insert into tb (a,b) values (1,2);       /* fails (correct) */
ERROR 1062 (23000): Duplicate entry '1-2' for key 2

mysql> insert into tb (a,b) values (1,null);
Query OK, 1 row affected (0.00 sec)

mysql> insert into tb (a,b) values (1,null);    /* succeeds (incorrect?) */
Query OK, 1 row affected (0.02 sec)

mysql> select version();
+----------------+
| version()      |
+----------------+
| 5.0.0-alpha-nt |
+----------------+
1 row in set (0.00 sec)

Suggested fix:
Return "duplicate key" error.
[24 Oct 2013 12:16] MySQL Verification Team
Hello Dimitriy,

Thank you for the bug report, duplicate of http://bugs.mysql.com/bug.php?id=17825

Thanks,
Umesh

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.042 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.