반응형
/********************************************************************************************
-- Title : [9.2] Serial, Bigserial 데이터 타입
-- Reference : dbrang.tistory.com
-- Key word : serial bigserial identity sequence
********************************************************************************************/
-- 초기화
drop table ttt;
drop sequence ttt_id_seq;

-- serial & bigserial
create table ttt
( id serial not null primary key
, a varchar(100)
);

NOTICE:  CREATE TABLE will create implicit sequence "ttt_id_seq" for serial column "ttt.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "ttt_pkey" for table "ttt"

-- 시퀀스 확인 
SELECT relname, relkind FROM pg_class
where relkind = 'S'; 
/*
relanem       relkind
-----------   -----------
"ttt2_a_seq"  "S"
"ttt_id_seq"  "S"
*/

-- serial 제거
-- serial -> int로, bigserial -> bigint로 
alter table ttt
alter column id set default null;

-- 시퀀스 삭제 확인
-- 시퀀스 owner로 ttt.id가 지정되지 않아 삭제되지 않음.
SELECT relname, relkind FROM pg_class
where relkind = 'S';

-- 내부적으로 시퀀스로 처리됨.
-- 참조 : http://dbrang.tistory.com/784

--  SQL Server의 identity와 유사

반응형

+ Recent posts