1

I want to rebuild id's after delete raw....

e.x

1 Text1 Number
2 Text2 Number
3 Text3 Number
4 Text4 Number
5 Text5 Number

Whan after delete raw 4. i want table look like:

1 Text1 Number
2 Text2 Number
3 Text3 Number
4 Text5 Number

And whan i add new item add to id 5

I work on codeigniter and my controller is:

delete function:

$this->db->query("DELETE FROM bookmark WHERE id = '$id'");

add function:

 $this->db->query("INSERT INTO bookmark VALUES ('$id', '$text', '$number')");
MartinKalco
  • 62
  • 1
  • 9

1 Answers1

1

After delete run this mysql command

SET @num := 0;

UPDATE your_table SET id = @num := (@num+1);

ALTER TABLE tableName AUTO_INCREMENT = 1;

In codeigniter active-records use as :-

$this->db->query("SET @num := 0;");
$this->db->query("UPDATE your_table SET id = @num := (@num+1);");
$this->db->query("ALTER TABLE tableName AUTO_INCREMENT = 1;");

Find more info here link

Community
  • 1
  • 1
Praveen Kumar
  • 2,408
  • 1
  • 12
  • 20