Re: unique constraints

From: "Ron Watkins" <rwatki(at)gmail(dot)com>
To: "'Campbell, Lance'" <lance(at)illinois(dot)edu>, <pgsql-admin(at)postgresql(dot)org>
Subject: Re: unique constraints
Date: 2016-12-16 15:37:49
Message-ID: 1c6e01d257b2$5c201f90$14605eb0$@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Because NULL != NULL, therefore you can't have a unique value for NULL.

From: pgsql-admin-owner(at)postgresql(dot)org
[mailto:pgsql-admin-owner(at)postgresql(dot)org] On Behalf Of Campbell, Lance
Sent: Friday, December 16, 2016 8:30 AM
To: pgsql-admin(at)postgresql(dot)org
Subject: [ADMIN] unique constraints

PostgreSQL 9.5

I have a table:

CREATE TABLE test

(

id integer NOT NULL,

tag_id integer,

CONSTRAINT test_id_tag_id_key UNIQUE (id, tag_id)

)

WITH (

OIDS=FALSE

);

I noticed I can insert this into the database:

Insert into test (id, tag_id) values(1,null);

Insert into test (id, tag_id) values(1,null);

But this does not work:

Insert into test (id, tag_id) values(2,1);

Insert into test (id, tag_id) values(2,1); (error occurs)

Select * from test;

1, null

1, null

2, 1

I understand why in the second set of inserts I get an error when trying to
insert the same set of values. But why does the first example work? I
would have thought that it would have only allowed the first insert to work.

Thanks,

Lance

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Achilleas Mantzios 2016-12-16 15:37:57 Re: unique constraints
Previous Message Campbell, Lance 2016-12-16 15:30:14 unique constraints