1

I am new-bie to MySQL. I want a user-defined function for the below functionality. I have table that has rowid column which has UNIQUE, AUTO-INCREMENT, NOT NULL values. When the table has 100 rows,next id value is set to 101 by default.But when I delete 50 rows, then add a new row, it's again 101 but not 51.How to create & call function that updates value according to current no of rows.Plz give complete function code & how to call it when some rows are deleted in table.

1 Answer 1

0

You could try below:

SET @count = 0;
UPDATE `tblName` SET `id` = @count:= @count + 1;

However, i do think it is NOT advisable to reuse the deleted rows auto increment id as auto increment field is not designed for this purpose. This is especially important as the id field might be used as foreign key in relation with other tables.

1
  • Ok, but I'm not using the rowid as foreign key but only want a sequential count of rows in table to have correct row count, be easy to delete rows just with the unique id.
    – Honey459
    Jul 27, 2012 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.