DBMS

[SQL]AUTO_INCREMENT 값 초기화

바켠주 2024. 7. 3. 17:41
728x90

AUTO_INCREMENT

  • 컬럼을 생성할 때 주는 옵션
  • 값이 자동으로 1씩 증가

student_tb의 student_id에 AUTO_INCREMENT 옵션 지정

delete하기전

delete from student_tb where student_id = 12;

student_id가 12인 행을 삭제하고 

insert into student_tb
	(student_id, student_name, phone, email, admission_date)
values 
	(0, "test4", "010-4444-4444", "aa@naver.com", now());

새로 추가하면 12가 아닌 13이 들어간다.

delete후 값을 새로 추가

값을 삭제하고 다시 추가해도  AUTO_INCREMENT는 계속 증가한다.

alter table student_tb AUTO_INCREMENT = 11;

AUTO_INCREMENT 값을 초기화하고 다시 insert하면

초기화하고 다시 insert

순서에 맞게 값을 변경할 수 있다.

AUTO_INCREMENT 값 초기화

alter table 테이블명 AUTO_INCREMENT = 시작할 값