본문 바로가기

DS

자동 증가 컬럼생성 방법

반응형


mysql, postgresql 을 예로 설명 하도록 한다.

mysql 에서는 테이블 생성시 auto_increment 라는 키워드를 붙혀주면 해당 컬럼이 자동 증가 된다.


하지만 postgresql  에서는 생성 방법이 조금 다르다.

먼저 sequence 를 생성후  해당 시퀀스를 사용하여 컬럼의 값을 자동 증가 되도록 하게 한다.


============== mysql =====================
create table Member(
id bigint auto_increment primary key,
name varchar(100) not null,
age int null

);

============= postgresql =============
#시퀀스 생성


create sequence member_id;

# 테이블 생성시 생성된 시퀀스 member_id 를 사용하여 id 컬럼을 자동 증가되도록 해준다.


create table Member(
id bigint not null default nextval('member_id') primary key,
name varchar(100) not null,
age int not null

);


반응형