Mawg
Mawg

Reputation: 40140

Why is this a duplicate key?

Why is this a duplicate key?

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'

Upvotes: 0

Views: 76

Answers (1)

zerkms
zerkms

Reputation: 254916

This happens because of index prefixes.

Prefix is amount of chars that actually placed to the index.

If prefix is 1 - then you wouldn't be able to insert rows ab and aa, because prefixed value for both of them is a thus it causes duplicate entry error.

Prefix is used to reduce amount of data stored in indexes, because in most cases just several characters from a long string is enough to speed up queries.

More details at: http://dev.mysql.com/doc/refman/5.5/en/create-index.html

Upvotes: 4

Related Questions