check contraints incorrectly reject "null"
check contraints incorrectly reject "null"
От:
Don Baccus <dhogaza@pacifier.com>
Дата:
Given a table definition like: create table foo (i integer check (i > 0)); I noticed the following works in Oracle but fails in Postgres: insert into foo values(null); I was curious about what the standard might say, and had been meaning to buy Date's book for some time, so broke down and did so. According to Date, a check contraint should fail if the expression evaluates to false. It appears that Postgres only passes the check constraint if it evaluates to true. In three-valued logic, these statements aren't equivalent. He has a paragraph about nulls and check contraints in chapter 14, I believe, and his explanation makes it clear that Oracle is right, Postgres wrong. It's easy to fix by adding a check for null to the constraint, and afterwards the SQL still works with Oracle, but it's still a bug... - Don Baccus, Portland OR Nature photos, on-line guides, Pacific Northwest Rare Bird Alert Service and other goodies at http://donb.photo.net.
Re: [HACKERS] check contraints incorrectly reject "null"
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Don Baccus writes:
> According to Date, a check contraint should fail if the expression
> evaluates to false.
And SQL92 says:
A table check constraint is satisfied if and only if the specified is not false for any row of a table. ^^^^^^^^^
so they agree: a constraint that yields NULL should be considered
to pass. A tad nonintuitive, but who am I to argue...
I have fixed several bugs recently having to do with incorrect
evaluation of three-state boolean logic. I'll take care of this one.
regards, tom lane