설문조사
PostgreSQL/PPAS 관련 듣고 싶은 교육은


총 게시물 94건, 최근 0 건
   

pg15의 뉴피쳐 UNIQUE NULLS NOT DISTINCT 구문

글쓴이 : PostgresDBA 날짜 : 2022-11-22 (화) 10:40 조회 : 1795
drop table if exists x;
drop table if exists y;

CREATE TABLE x
(
    x TEXT ,
    CONSTRAINT uq_x        UNIQUE (x)
);

-- unique 로 선언되었지만 null 값은 예외입니다. 중복으로 insert 가 가능합니다. 

insert into x values(null);
insert into x values(null);;


select * From x;
   x    
--------
 <NULL>
 <NULL>

------------------------------------------------------------------------------------
pg15 에서는 새로운 구문이 생겨서 null 값 중복을 막을수 있습니다.

CREATE TABLE y
(
    y TEXT ,
    CONSTRAINT uq_y        UNIQUE NULLS NOT DISTINCT (y)
);

insert into y values(null);  -- success

insert into y values(null);   -- failure
ERROR:  duplicate key value violates unique constraint "uq_y"
DETAIL:  Key (y)=(null) already exists.


   

postgresdba.com