Pantek Library
Hosting Provided By
CybrHost
High Speed Hosting

increment contiguous unique values

From: Jack Bates <ms419(at)freezone.co.uk>
Date: Tue May 27 2008 - 15:15:03 EDT


I have contiguous sequential values in a UNIQUE KEY column:

mysql> SELECT foo FROM bar;

+-----+
| foo |

+-----+
| 1 |
| 2 |
| 3 |
| ... |

I want to insert a new row in the middle of this sequence, so I need to first shift all the rows which follow it.

So say I want to insert a new row as the second row in my example. I need to first increment the foo column for all rows where foo >= 2:

mysql> UPDATE bar SET foo = foo + 1 WHERE foo >= 2;

Duplicate entry '3' for key 1

I am confused by this error: If there are no duplicate values before my UPDATE statement, then there should be no duplicate values after it?

Do you need help?X

I can only guess that MySQL is incrementing the second row before the third row, resulting in a temporarily duplicate value 3 in the foo column in the second and third row, and reporting an error?

I do want MySQL to prevent duplicate values in the foo column, however my UPDATE statement should not result in duplicate values. Is there any way to defer this constraint until after the statement completes?

I am using InnoDB...

Thanks and best wishes - Jack

Received on Tue May 27 15:17:03 2008

This archive was generated by hypermail 2.1.8 : Fri Sep 26 2008 - 18:47:08 EDT


Contact Us  Legal Notices  Order Services Online 
Pantek Home  Privacy Policy  IT news  Site Map  Pantek Library