Re: CHECK constraint removing brackets - Mailing list pgsql-sql

From Scott Marlowe
Subject Re: CHECK constraint removing brackets
Date
Msg-id dcc563d11001111214p3cd20de4rf65d8d591b3e4cca@mail.gmail.com
Whole thread Raw
In response to CHECK constraint removing brackets  (Andy Shellam <andy-lists@networkmail.eu>)
List pgsql-sql
On Mon, Jan 11, 2010 at 12:49 PM, Andy Shellam
<andy-lists@networkmail.eu> wrote:
> With the above in mind, I decided on the following check to enforce this:
>
> (state = 'Unconfirmed'::client.order_state AND invoice_id = NULL) OR (state != 'Unconfirmed'::client.order_state AND
invoice_id!= NULL)
 

Nothing can = null.  and invoice_id IS NULL is the proper
nomenclature.  Also, something <> NULL makes no sense, because we
don't know what NULL is, so that becomes something IS NOT NULL

Also != is not proper SQL, although many dbs understand it, <> is the
proper way to write NOT EQUAL TO.

> However PostgreSQL (8.4.2) converts this to the following:
>
> state = 'Unconfirmed'::client.order_state AND invoice_id = NULL::integer OR state <>
'Unconfirmed'::client.order_stateAND invoice_id <> NULL::integer
 

ANDs have priority of ORs so the removal of the parenthesis makes no
great change here.  also, SQL standard is <> not !=.

I'm guessing the real problems here are your NULL handling.  See if
changing it to IS NULL / IS NOT NULL gets you what you want.


pgsql-sql by date:

Previous
From: Andy Shellam
Date:
Subject: CHECK constraint removing brackets
Next
From: Tom Lane
Date:
Subject: Re: CHECK constraint removing brackets