2012-04-24 17 views
0

爲什麼這是重複密鑰?爲什麼這是一個重複的密鑰?

mysql> describe tagged_chemicals; 
+-------------+---------+------+-----+---------+-------+ 
| Field  | Type | Null | Key | Default | Extra | 
+-------------+---------+------+-----+---------+-------+ 
| bar_code | text | NO |  | NULL |  | 
| rfid_tag | text | NO | UNI | NULL |  | 
| checked_out | char(1) | NO |  | N  |  | 
+-------------+---------+------+-----+---------+-------+ 
3 rows in set (0.04 sec) 

mysql> select * from tagged_chemicals; 
+-------------+----------------------+-------------+ 
| bar_code | rfid_tag    | checked_out | 
+-------------+----------------------+-------------+ 
| 416444.0001 | 34443030304142453141 | N   | 
+-------------+----------------------+-------------+ 
1 row in set (0.00 sec) 

mysql> INSERT INTO tagged_chemicals (rfid_tag, bar_code) VALUES("34443030304144393935", "412577.0001B"); 
ERROR 1062 (23000): Duplicate entry '34443030304144393935' for key 'rfid_tag' 
+2

評論者/回答者:注意到現有rfid_tag ==的(不)插入rfid_tag,但它仍然拋出一個重複的輸入錯誤! – biziclop 2012-04-24 07:13:51

+1

@zerkms:問題在我看來,雖然rfid_tag應該是唯一的,但插入的新值當前不在表中? – Nanne 2012-04-24 07:14:13

+1

@Dr。 biziclop:這是因爲索引前綴而發生的。 http://dev.mysql.com/doc/refman/5.5/en/create-index.html - 「ctrl + f」代表「前綴」 – zerkms 2012-04-24 07:14:46

回答

4

這是因爲索引前綴。

前綴是實際放置到索引的字符數量。

如果前綴爲1,那麼您將無法插入行abaa,因爲它們的前綴值都是a,因此它會導致重複的輸入錯誤。

前綴用於減少存儲在索引中的數據量,因爲在大多數情況下,只有來自長字符串的幾個字符足以加快查詢速度。

更多細節在:http://dev.mysql.com/doc/refman/5.5/en/create-index.html

相關問題