본문 바로가기

DB/PostgreSQL

Postgresql 시퀀스

시퀀스 적용 테이블 : board 

적용할 시퀀스 : board_sequence

 

CASE 1

[시퀀스 생성]

CREATE SEQUENCE board_sequence;

[시퀀스 적용]

ALTER TABLE board ALTER COLUMN board_no SET DEFAULT nextval('board_sequence');

 

CASE 2

[테이블 생성 및 적용]

CREATE TABLE board (

  board_no integer NOT NULL DEFAULT nextval('board_sequence')

);

 

CASE 3

[테이블 생성 및 적용 (기본값)]

CREATE TABLE board (

  board_no serial NOT NULL

);

 

[시퀀스 삭제]

DROP SEQUENCE board_sequence

(시퀀스 삭제전에 테이블 먼저 삭제해야 함)

 

[시퀀스 조회]

select n.nspname as sequence_schema, 
          c.relname as sequence_name,
          u.usename as owner
from pg_class c 
     join pg_namespace n on n.oid = c.relnamespace
     join pg_user u on u.usesysid = c.relowner
where c.relkind = 'S'
     and u.usename = current_user;

 

 

postgresql.kr/docs/11/sql-altersequence.html