Pular para o conteúdo

MySQL – Truncate e campo AUTO_INCREMENT

MySQL – Truncate e campo AUTO_INCREMENT

Fala galera!!

Vamos ver como se comporta o AUTO_INCREMENT ao executarrmos um TRUNCATE em uma tabela do MySQL.

mysql> create database teste;

Query OK, 1 row affected (0.00 sec)
mysql> use teste; 
Database changed  

mysql> create table teste_truncate ( id int primary key auto_increment, numero int); 
Query OK, 0 rows affected (0.05 sec)
  
CREATE PROCEDURE `p_teste_carga`() 
BEGIN     
DECLARE 
i INT;
set i = 1;      
while i <= 10000 do          
insert into teste_truncate(numero)          
values (i + 1);
          
set i = i + 1;
end while;
END  

mysql> call p_teste_carga();
Query OK, 1 row affected (41.18 sec)  
mysql> select max(id) from teste_truncate;
 +---------+ | max(id) | +---------+ |   10000 | +---------+ 1 row in set (0.01 sec)  
mysql> truncate table teste_truncate; 
Query OK, 0 rows affected (0.05 sec)  
mysql> insert into teste_truncate(numero) values(2);
Query OK, 1 row affected (0.00 sec)  
mysql> select max(id) from teste_truncate;
 +---------+ | max(id) | +---------+ |       1 | +---------+ 1 row in set (0.00 sec)

Vemos que o Truncate “limpa” o nosso campo incremental.

Quão útil foi este post ?

Clique em uma estrela para classificar o post

nota média 5 / 5. Contagem de votos: 13

Sem votos ! Seja o primeiro a classificar !

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

plugins premium WordPress