I just checked and allowed to create a table with a column that is NULL by default, although it is also a UNIQUE KEY:
CREATE TABLE IF NOT EXISTS `u789` ( `column1` varchar(10) DEFAULT NULL, UNIQUE KEY (column1) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
As I understand it, it looks weird and doesn't make much sense. I expected the second insert
INSERT INTO u789 VALUE (NULL);
will fail.
But it inserts the first, second, third NULL value without any problems. Who can explain to me why it contains the second and third columns if NULL is already in the table?
This is a theoretical question (as I understand it, no one uses DEFAULT NULL + UNIQUE KEY for the same column in most situations), but I want to understand why it does not throw an error as soon as one NULL is already in the column. Am I doing something wrong with declaring a unique column?
Thanks.
source share