Re: [SQL] Why wont this update work?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Robert Chalmers" <robert(at)chalmers(dot)com(dot)au>
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] Why wont this update work?
Date: 1999-05-19 13:53:20
Message-ID: 29713.927122000@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Robert Chalmers" <robert(at)chalmers(dot)com(dot)au> writes:
> UPDATE catalog
> SET language = 'NoD'
> WHERE language = 'NULL'; ........ or NULL and so on.
> [ doesn't work ]

WHERE language IS NULL is the SQL-standard-approved way of testing
for NULL fields. (There's also IS NOT NULL if you need that.)

If you have a sufficiently recent version of Postgres, it will also
accept WHERE language = NULL (no quotes), but that's a nonstandard
extension. (We only put it in because we had to for reasons of
compatibility with Microsoft applications.)

> UPDATE catalog
> SET language = 'NoD'
> WHERE NOT language = 'E';

This approach cannot work for finding nulls, because in expressions
null essentially means "don't know" ... and that means it propagates.
Is null equal to 'E'? The answer is "I don't know" --- another null.
Feed that null to the NOT operator, and out comes another null. The
buck stops at the WHERE clause, which is looking for a 'true' result
... but null isn't true either, so the tuple doesn't get selected.

In short, no expression involving ordinary comparison and logic
operators can produce anything but null when fed a null input.
And that means you can't select tuples containing nulls with such
an expression.

This is why the standard decrees a special syntax for testing for
null --- it's a very special operation. Microsoft's database code
is in fact violating the standard by accepting field = NULL as meaning
field IS NULL, because by rights field = NULL should always produce
a null result (whether the field contains null or not!).

regards, tom lane

Browse pgsql-sql by date

  From Date Subject
Next Message José Soares 1999-05-19 14:19:13 Re: [SQL] Why wont this update work?
Previous Message Herouth Maoz 1999-05-19 13:52:54 Re: [SQL] Oddities with NULL and GROUP BY