2012-09-03 74 views
0

我有一個服務器上的如下表:重複錄入錯誤

CREATE TABLE routes (
    id   int(11) unsigned NOT NULL AUTO_INCREMENT, 
    from_lat double NOT NULL, 
    from_lng double NOT NULL, 
    to_lat  double NOT NULL, 
    to_lng  double NOT NULL, 
    distance int(11) unsigned NOT NULL, 
    drive_time int(11) unsigned NOT NULL, 

    PRIMARY KEY (id), 
    UNIQUE KEY route (from_lat,from_lng,to_lat,to_lng) 
) ENGINE=InnoDB; 

我們節省了從點A點的路由信息​​(from_lat,from_lng)到B點(to_lat,to_lng) 。座標上有唯一的索引。

不過,也有在數據庫中的兩個條目是讓我困惑:

+----+----------+----------+---------+---------+----------+------------+ 
| id | from_lat | from_lng | to_lat | to_lng | distance | drive_time | 
+----+----------+----------+---------+---------+----------+------------+ 
| 27 | 52.5333 | 13.1667 | 52.5833 | 13.2833 | 13647 | 1125  | 
| 28 | 52.5333 | 13.1667 | 52.5833 | 13.2833 | 13647 | 1125  | 
+----+----------+----------+---------+---------+----------+------------+ 

他們是完全一樣的。

當我不嘗試導出使用mysqldump,並試圖重新導入數據庫中,我得到一個錯誤:

ERROR 1062 (23000): Duplicate entry '52.5333-13.1667-52.5833-13.2833' for key 'route' 

這怎麼可能,這是在數據庫中,當有一個獨特的密鑰在他們? MySQL不應該拒絕它們嗎?

+0

也許當時沒有唯一鍵的行插入,以及(如何將取決於您的客戶端上。) *然後*密鑰是在添加後? –

+0

是的,可能是。但是,如果有重複條目,MySQL不應該阻止創建唯一索引? – iblue

+0

通常我會這麼認爲,但http://stackoverflow.com/questions/12099230/table-with-unique-constraint-has-duplicate-records-mysql-5-1-57 –

回答

2

是否有可能雙數值略有不同,但只有在第4位後?
如果您導出並導入它們,它們將是相同的,並且會給出唯一的約束違規。

this MySQL bug report報價:

When mysqldump dumps a DOUBLE value, it uses insufficient precision to distinguish between some close values (and, presumably, insufficient precision to recreate the exact values from the original database). If the DOUBLE value is a primary key or part of a unique index, restoring the database from this output fails with a duplicate key error.

嘗試使用逗號後面的多個數字顯示它們

+0

好主意。我正在使用mysql命令行客戶端。我該怎麼做呢? – iblue

+0

這個[bug報告](http://bugs.mysql.com/bug.php?id=36829)表明該問題在v6中得到了修復。不知道是否甚至有可能從MySQL中檢索完整的精度。 – Andomar