개발/DB

[MariaDB] auto_increment 값 초기화

leebera_ 2022. 5. 27. 09:10
alter table tableName auto_increment=1;

 

만약 auto_increment가 설정된 컬럼에 설정 값보다 이미 높은 값이 있다면 auto_increment를 원하는 값으로 설정하더라도 이미 있는 최댓값의 다음값으로 설정된다.

 

예시

id number
4 0
5 0
6 0

위와 같은 테이블이 있을 때 auto_increment를 1로 설정하고 행을 추가해도 auto_increment값은 최댓값인 6의 다음인 7로 설정된다.

 

insert를 하나 더 하면 아래와 같이 7이 추가된다.

id number
4 0
5 0
6 0
7 0

 


 

참고

 

How to set initial value and auto increment in MySQL?

How do I set the initial value for an "id" column in a MySQL table that start from 1001? I want to do an insert "INSERT INTO users (name, email) VALUES ('{$name}', '{$email}')"; Without specifyin...

stackoverflow.com