pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Wim Ceulemans <Wim.Ceulemans@nice.be>
To: Carolyn Lu Wong <carolyn@kss.net.au>
Cc: pgsql-sql@postgresql.org <pgsql-sql@postgresql.org>
Subject: Re: primary key question
Date: Thu, 20 Jul 2000 09:45:31 +0200
Message-ID: <3976AE1B.C8BD2CEE@nice.be> (raw)
References: <3976A9A6.55E6BE2E@kss.net.au>

Carolyn Lu Wong wrote:
> 
> create table aaa(
>         field1  .... not null,
>         field2  ....,
>         ....,
>         primary key (field1, field2)
> );
> 
> Based on the above table definition, field2 allows null values. But
> after the table created based on the above script, field2 becomes not
> null. The only conclusion I come up with is setting the field as part of
> the primary key, PostgreSQL automatically sets the field to not null. Or
> is it something else?
> 
> Is this a feature or bug?

This is in sync with the SQL-92 spec as the following explains:

Quote from Tom Lane on pgsql-general yesterday:
> Two nulls are never considered equal, therefore the unique constraint
> does not trigger.
> 
> This is correct behavior according to SQL92 4.10.2:
> 
>          A unique constraint is satisfied if and only if no two rows in
>          a table have the same non-null values in the unique columns. In
>                                ^^^^^^^^
>          addition, if the unique constraint was defined with PRIMARY KEY,
>          then it requires that none of the values in the specified column or
>          columns be the null value.
> 
> (The second sentence just says that PRIMARY KEY implies NOT NULL as well
> as UNIQUE.)
> 
> Another way to look at it is that the comparison to see whether the two
> NULLs are equal would yield NULL, and a NULL result for a constraint
> condition is not considered to violate the constraint.
> 
> Another way to look at it is that NULL means "I don't know what the
> value is", so if you don't know what the values in two rows really are,
> you don't know whether they're equal either.  I suppose you could make
> a case for either accepting or rejecting the UNIQUE constraint in that
> situation --- but SQL92 chose the "accept" decision, and I think that
> for the majority of practical applications they made the right choice.
> 
> If you don't like that behavior, possibly your column should be defined
> as NOT NULL.
> 

Regards
Wim



view thread (4+ messages)  latest in thread

Message-ID: <3976AE1B.C8BD2CEE@nice.be>
Permalink:  ../3976AE1B.C8BD2CEE@nice.be/
Also on:    postgresql.org/message-id/3976AE1B.C8BD2CEE@nice.be

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: Wim.Ceulemans@nice.be, carolyn@kss.net.au
  Subject: Re: primary key question
  In-Reply-To: <3976AE1B.C8BD2CEE@nice.be>
  References: <3976A9A6.55E6BE2E@kss.net.au> <3976AE1B.C8BD2CEE@nice.be>

* 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