Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 607D46332AF for ; Mon, 11 Jan 2010 16:14:26 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 39644-07 for ; Mon, 11 Jan 2010 20:14:15 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.157]) by mail.postgresql.org (Postfix) with ESMTP id A0312633246 for ; Mon, 11 Jan 2010 16:14:15 -0400 (AST) Received: by fg-out-1718.google.com with SMTP id 22so892569fge.1 for ; Mon, 11 Jan 2010 12:14:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=4DhibVzb/uRU8Xy98TFMvPWrffZ+nX/irpgc6TKXjdg=; b=Eu7cqV7gPvUabgjzZ/XnNng+m3LfpA1aIgGYLILOz2A1TVHUIbtfeiHfeS6EFuwspq F+O2YvpymK127HoamuIM2FdAie8RE0ivwUbz//ARwHTeOCYPnJw60Ul2qRacRKaQCx2N l1733J/SJHg23sieC0nZchymmhCrdgcpU6wfg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=glzew8A4sD9dYNmuGCRo3cR7W2LCvHzFWnpRkHxS2A72RFKi+7ifkPRJpvdPCI1xEC yznzH7mza314nlcdgGbKwPfyb4uFPuZbV9ETa3lu0LsiTAH7s0FDV8SASGjBqpAJu2// BJ6eGBi01lvXM8xJHjj6jJ/aoRmF/fuwk7wK4= MIME-Version: 1.0 Received: by 10.216.87.132 with SMTP id y4mr1543467wee.99.1263240853514; Mon, 11 Jan 2010 12:14:13 -0800 (PST) In-Reply-To: References: Date: Mon, 11 Jan 2010 13:14:12 -0700 Message-ID: Subject: Re: CHECK constraint removing brackets From: Scott Marlowe To: Andy Shellam Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.589 tagged_above=-10 required=5 tests=BAYES_00=-2.599, FH_DATE_PAST_20XX=3.188 X-Spam-Level: X-Archive-Number: 201001/19 X-Sequence-Number: 33555 On Mon, Jan 11, 2010 at 12:49 PM, Andy Shellam 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_state AND 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.