Bug #8398 Primary Key with NULL values
Submitted: 9 Feb 2005 17:24 Modified: 27 May 2006 12:11
Reporter: Andrew Hanna Email Updates:
Status: Won't fix Impact on me:
None 
Category:MySQL Server Severity:S4 (Feature request)
Version: OS:
Assigned to: CPU Architecture:Any

[9 Feb 2005 17:24] Andrew Hanna
Description:
For InnoDB and MyISAM engines, would it be possible to make a PRIMARY KEY be able to have NULL columns, but then treat the NULL values as simply one other option, as is the case for BDB tables (or so I gather from the documentation, I do not have the BDB engine on my installation)?

How to repeat:
CREATE TABLE `test` (
`domain` VARCHAR( 50 ) NULL ,
`uri` VARCHAR( 250 ) NULL ,
`name` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `domain` , `uri` )
) TYPE = InnoDB; // table should be created properly (even with two NULL PRIMARY KEY columns)

INSERT INTO `test` VALUES ('example.com', NULL, 'welcome to example.com'),
(NULL, '/home', 'home page'),
(NULL, NULL, 'default index page');  // All of these columns should be added!

// this row however should NOT work after the above rows have been added
INSERT INTO `test` VALUES (NULL, '/home', 'home page 2'); // sorry, duplicate index for NULL-/home

// this one should work
INSERT INTO `test` VALUES (NULL, '/home2', 'home page 2'); // ok, this row is NULL-/home2, not NULL-/home

Suggested fix:
It would be nice to allow NULL to be another allowed value.  I understand that the current PRIMARY KEY is the way it is because a UNIQUE index can have multiple NULL values per column.

Just another suggestion, I don't know if this flies in the face of database design or structure, but I think it could be useful....
[27 May 2006 12:11] Valeriy Kravchuk
> For InnoDB and MyISAM engines, would it be possible to make a PRIMARY KEY > be able to have NULL columns?

Never. Having composite primary key with all NULL columns is against its definition, against all SQL standards. You may just add unique index, if you want this.