pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
unique constraints
3+ messages / 3 participants
[nested] [flat]

* unique constraints
@ 2016-12-16 15:30 Campbell, Lance <lance@illinois.edu>
  2016-12-16 15:37 ` Re: unique constraints Ron Watkins <rwatki@gmail.com>
  2016-12-16 15:37 ` Re: unique constraints Achilleas Mantzios <achill@matrix.gatewaynet.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Campbell, Lance @ 2016-12-16 15:30 UTC (permalink / raw)
  To: pgsql-admin

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

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: unique constraints
  2016-12-16 15:30 unique constraints Campbell, Lance <lance@illinois.edu>
@ 2016-12-16 15:37 ` Ron Watkins <rwatki@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Ron Watkins @ 2016-12-16 15:37 UTC (permalink / raw)
  To: 'Campbell, Lance' <lance@illinois.edu>; pgsql-admin

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

 

From: pgsql-admin-owner@postgresql.org
[mailto:pgsql-admin-owner@postgresql.org] On Behalf Of Campbell, Lance
Sent: Friday, December 16, 2016 8:30 AM
To: pgsql-admin@postgresql.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

 

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: unique constraints
  2016-12-16 15:30 unique constraints Campbell, Lance <lance@illinois.edu>
@ 2016-12-16 15:37 ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Achilleas Mantzios @ 2016-12-16 15:37 UTC (permalink / raw)
  To: pgsql-admin

On 16/12/2016 17:30, Campbell, Lance wrote:
>
> 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.
>
Because null is not a normal value you can compare against. Null basically is equivalent to "do not know". I haven't caught up wit the latest SQL flavors of pgsql, but old school you would do this by 
adding this :

CREATE UNIQUE INDEX test_id_uk ON test(id) WHERE (tags_id IS NULL);
>
> Thanks,
>
> Lance
>


-- 
Achilleas Mantzios
IT DEV Lead
IT DEPT
Dynacom Tankers Mgmt

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2016-12-16 15:37 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-12-16 15:30 unique constraints Campbell, Lance <lance@illinois.edu>
2016-12-16 15:37 ` Ron Watkins <rwatki@gmail.com>
2016-12-16 15:37 ` Achilleas Mantzios <achill@matrix.gatewaynet.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox