본문 바로가기
DB/MariaDB

[MariaDB] 13.auto_increment

by 델버 2022. 12. 20.
  • id나 순번 같은 고유한 값을 자동으로 넣어주는 기능이 auto_increment이다.

설정

  • 기존 조건을 지키면 된다.

조건

  • primary key만 가능

  • column이 int만 가능하다.

  • 이미 설정된 column에 설정
    alter table table명 modify column **int** auto_increment primary key ;

  • table을 create할 때 설정
    `create table table명 (

    column명 int auto_increment primary key

    );`


column을 id int로 만들고 auto_increment로 설정할 경우 따로 id 값을 넣지 않아도 자동으로 +1하여 들어간다.

그런데 만약 id를 3까지 만들고 2번을 지운다고 하면 auto_increment는 2번을 만들지 않는다. 즉, 삭제한다고 없는 번호를 기준으로 설정되지 않고 그대로 쭉쭉 만드는 것이다. 데이터를 모두 삭제한다고 해도 마지막에 만든 숫자 +1을 계속 한다.

그래서 초기화를 해주어야 한다.

초기화

  • 지정해주고 싶은 int를 설정하여 여기서 부터 시작하게 한다.
    alter table table명 auto_increment = 지정해줄int;

제거

  • alter table table명 change column명 자료형 null;
  • alter table table명 change column명 column명 자료형;

'DB > MariaDB' 카테고리의 다른 글

[MariaDB] 15. foreign key  (0) 2022.12.20
[MariaDB] 14.primary key  (0) 2022.12.20
[MariaDB] 12.union  (0) 2022.12.20
[MariaDB] 11. join  (0) 2022.12.20
[MariaDB] 10.논리 함수  (0) 2022.12.20

댓글