From: Ron Watkins <rwatki@gmail.com>
To: 'Campbell, Lance' <lance@illinois.edu>
To: pgsql-admin@postgresql.org
Subject: Re: unique constraints
Date: Fri, 16 Dec 2016 08:37:49 -0700
Message-ID: <1c6e01d257b2$5c201f90$14605eb0$@gmail.com> (raw)
In-Reply-To: <B75CD08C73BD3543B97E4EF3964B7D70479DACB2@CITESMBX1.ad.uillinois.edu>
References: <B75CD08C73BD3543B97E4EF3964B7D70479DACB2@CITESMBX1.ad.uillinois.edu>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-admin@postgresql.org
Cc: rwatki@gmail.com, lance@illinois.edu
Subject: Re: unique constraints
In-Reply-To: <1c6e01d257b2$5c201f90$14605eb0$@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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