Indexing InnoDB:如果不为NULL,则为唯一

Indexing InnoDB:如果不为NULL,则为唯一,indexing,innodb,unique,Indexing,Innodb,Unique,InnoDB是否支持“非空唯一”索引?即允许多个空值的唯一索引 (我想没有,因为我找不到任何关于它的东西-找到了很多相关的东西,但没有确定的答案)在InnoDB中,唯一的二级索引支持多个空值(主索引根本不支持空值)。例如: mysql> create table x (i int, j int, primary key (i), unique key (j)); mysql> insert into x (i,j) values (NULL,NULL); ERROR 1048 (23

InnoDB是否支持“非空唯一”索引?即允许多个空值的唯一索引


(我想没有,因为我找不到任何关于它的东西-找到了很多相关的东西,但没有确定的答案)

在InnoDB中,唯一的二级索引支持多个空值(主索引根本不支持空值)。例如:

mysql> create table x (i int, j int, primary key (i), unique key (j));
mysql> insert into x (i,j) values (NULL,NULL);
ERROR 1048 (23000): Column 'i' cannot be null
mysql> insert into x (i,j) values (1,NULL);
mysql> insert into x (i,j) values (2,NULL);
mysql> insert into x (i,j) values (3,3);
mysql> select * from x;
+---+------+
| i | j    |
+---+------+
| 1 | NULL |
| 2 | NULL |
| 3 |    3 |
+---+------+
3 rows in set (0.01 sec)

mysql> insert into x (i,j) values (4,3);
ERROR 1062 (23000): Duplicate entry '3' for key 'j'
哦,谢谢,我不知道!(很奇怪,我在谷歌上找不到它)