Bug #55919 Duplicate rows on Unique key
Submitted: 11 Aug 2010 17:12 Modified: 11 Aug 2010 18:16
Reporter: Mark Grennan Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version: 5.1.37sp1-enterprise-gpl-advanced-log OS:Linux (RedHat 5.2)
Assigned to: CPU Architecture:Any
Tags: 5.1, duplicate, MySQL, unique key

[11 Aug 2010 17:12] Mark Grennan
Description:
I found lots of duplicate records in my database with a unique key defined.  The unique key I'm expecting to take care of this is 'report'.  

I think I have narrowed it down to the float (lat, lon) fields.  I'm guessing this is due to like values (41.85) not creating the same floating point representation.  I would like conformation. 

How to repeat:
Create this table and run the SQL INSERT statements (more then once if you would like).  The second INSERT should create an Duplicate error but it never does.

CREATE TABLE `test_storm_reports` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ref_id` int(11) DEFAULT NULL,
  `site` char(8) NOT NULL,
  `date_issued` datetime NOT NULL,
  `event_date` datetime NOT NULL,
  `event` enum('HAIL','RAIN','HEAVY RAIN','FREEZING RAIN','SNOW','HEAVY SNOW','BLIZZARD','TORNADO','WATER SPOUT','WIND','HIGH SUST WINDS','DOWNBURST','TSTM WND DMG','TSTM WND GST','NON-TSTM WND GST','NON-TSTM WND DMG','LIGHTNING','FLOOD','FLASH FLOOD','FUNNEL CLOUD','MARINE TSTM WIND') DEFAULT NULL,
  `location` varchar(40) DEFAULT NULL,
  `county` varchar(40) DEFAULT NULL,
  `state` char(2) DEFAULT NULL,
  `lat` float(6,2) NOT NULL,
  `lon` float(6,2) NOT NULL,
  `meas_est` enum('M','E') DEFAULT NULL,
  `mag` float(4,2) DEFAULT NULL,
  `units` enum('INCH','F','MPH') DEFAULT NULL,
  `source` enum('PUBLIC','AMATEUR RADIO','LAW ENFORCEMENT','TRAINED SPOTTER','FIRE DEPT/RESCUE','EMERGENCY MNGR','BROADCAST MEDIA','POST OFFICE','ASOS','NWS EMPLOYEE','OFFICIAL NWS OBS','MESONET','CO-OP OBSERVER','STORM CHASER','NWS STORM SURVEY','AWOS','UTILITY COMPANY','DEPT OF HIGHWAYS','NEWSPAPER','COUNTY OFFICIAL','OTHER FEDERAL','PARK/FOREST SRVC','911 CENTER','SKYWARN SPOTTER','C-MAN STATION','STATE POLICE','COCORAHS','BUOY','AIRPLANE PILOT','OK HIGHWAY PTRL','COAST GUARD','INSURANCE CO','UNKNOWN') DEFAULT NULL,
  `comments` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `report` (`site`,`event_date`,`event`,`lat`,`lon`,`source`,`comments`),
  KEY `event_date` (`event_date`)
) ENGINE=MyISAM AUTO_INCREMENT=24363 DEFAULT CHARSET=latin1

INSERT  INTO `test_storm_reports` (`ref_id`, `site`, `date_issued`, `event_date`, `event`, `location`, `county`, `state`, `lat`, `lon`, `meas_est`, `mag`, `units`, `source`, `comments`) VALUES (NULL,'KDMX','2010-08-11 06:15:00','2010-08-11 05:53:00','TSTM WND GST','2 W WOODWARD','DALLAS','IA',41.85,-93.96,'E',60,'MPH','EMERGENCY MNGR',NULL);
INSERT  INTO `test_storm_reports` (`ref_id`, `site`, `date_issued`, `event_date`, `event`, `location`, `county`, `state`, `lat`, `lon`, `meas_est`, `mag`, `units`, `source`, `comments`) VALUES (NULL,'KDMX','2010-08-11 10:05:00','2010-08-11 05:53:00','TSTM WND GST','2 W WOODWARD','DALLAS','IA',41.85,-93.96,'E',60,'MPH','EMERGENCY MNGR',NULL);

Suggested fix:

What is the best way to store latitude and Longitude?  DECIMAL(4,6)?
[11 Aug 2010 17:31] Mark Grennan
IF I change the 'comment' field to "test" and it will find the duplicate.

Is this issue due to NULL in the key?
[11 Aug 2010 17:49] Sveta Smirnova
Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://dev.mysql.com/doc/ and the instructions on
how to report a bug at http://bugs.mysql.com/how-to-report.php

You are absolutely right: this is issue with NULL in the key. Closing as not a bug.
[11 Aug 2010 18:16] Mark Grennan
Don't your love it when we find our own problems?

The issue was with NULL in the KEY.  IE use DEFAULT NULL is not a good idea.

Thanks