Hướng dẫn reset id mysql

I googled and found this question, but the answer I am really looking for fulfils two criteria:

  1. using purely MySQL queries
  2. reset an existing table auto-increment to max[id] + 1

Since I couldn't find exactly what I want here, I have cobbled the answer from various answers and sharing it here.

Few things to note:

  1. the table in question is InnoDB
  2. the table uses the field id with type as int as primary key
  3. the only way to do this purely in MySQL is to use stored procedure
  4. my images below are using SequelPro as the GUI. You should be able to adapt it based on your preferred MySQL editor
  5. I have tested this on MySQL Ver 14.14 Distrib 5.5.61, for debian-linux-gnu

Step 1: Create Stored Procedure

create a stored procedure like this:

DELIMITER //
CREATE PROCEDURE reset_autoincrement[IN tablename varchar[200]]
BEGIN

      SET @get_next_inc = CONCAT['SELECT @next_inc := max[id] + 1 FROM ',tablename,';'];
      PREPARE stmt FROM @get_next_inc;
      EXECUTE stmt;
      SELECT @next_inc AS result;
      DEALLOCATE PREPARE stmt;

      set @alter_statement = concat['ALTER TABLE ', tablename, ' AUTO_INCREMENT = ', @next_inc, ';'];
      PREPARE stmt FROM @alter_statement;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;
END //
DELIMITER ;

Then run it.

Before run, it looks like this when you look under Stored Procedures in your database.

When I run, I simply select the stored procedure and press Run Selection

Note: the delimiters part are crucial. Hence if you copy and paste from the top selected answers in this question, they tend not to work for this reason.

After I run, I should see the stored procedure

If you need to change the stored procedure, you need to delete the stored procedure, then select to run again.

Step 2: Call the stored procedure

This time you can simply use normal MySQL queries.

call reset_autoincrement['products'];

Originally from my own SQL queries notes in //simkimsia.com/reset-mysql-autoincrement-to-max-id-plus-1/ and adapted for Stack Overflow.

Hướng dẫn Thiết lập, Reset giá trị, column tự tăng trong MySQL

Giá trị tự tăng ‘AUTO_INCREMENT’:

  • Thiết lập giá trị tự tăng cho column khi được insert
  • Thường được dùng với các column là primary key [cũng có thể dùng với các column khác]
  • Khi insert một bản ghi mới ta không cần truyền giá trị vào column này mà nó sẽ tự động được tăng lên

Để thiết lập giá trị tự tăng, ta chọn ô AI khi tạo table trong MySQL.

Ví dụ dưới đây mình tạo table có column id tự tăng:

CREATE TABLE `customer` [
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR[45] NULL,
PRIMARY KEY [`id`]];

Bây giờ thực hiện insert bản ghi mới vào table ‘customer’  –> ta chỉ cần truyền giá trị vào column name, còn giá trị ở column id sẽ được tự động tăng.

INSERT INTO customer [`name`] VALUES ['kai'];

Để thiết lập giá trị tự tăng ban đầu ta dùng lệnh:

ALTER TABLE table_name AUTO_INCREMENT = value;

Ví dụ mình thiết lập giá trị tự tăng bắt đầu từ số 10 sau đấy thực hiện insert:

* Lưu ý, giá trị tự tăng không tính theo giá trị lớn nhất hiện tại.

Ví dụ trong table customer, bạn xóa hết các record đi –> insert lại thì id sẽ bắt đầu từ 11 chứ không phải là 10 hay 1.

Nếu bạn muốn bắt đầu từ số 1 ta lại dùng lệnh:

ALTER TABLE customer AUTO_INCREMENT = 1;

Nếu bạn không muốn sử dụng câu lệnh thì có thể vào mục sửa table và chọn tab options và nhập giá trị AUTO_INCREMENT

Thiết lập, Reset giá trị, column tự tăng trong MySQL

References:

//dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html

Chủ Đề