Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b8uHU-0001N2-4s for pgsql-sql@arkaria.postgresql.org; Fri, 03 Jun 2016 18:57:16 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b8uHT-0001ry-Ns for pgsql-sql@arkaria.postgresql.org; Fri, 03 Jun 2016 18:57:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b8uGW-0000oe-Cw for pgsql-sql@postgresql.org; Fri, 03 Jun 2016 18:56:16 +0000 Received: from know-smtprelay-omc-4.server.virginmedia.net ([80.0.253.68]) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b8uGP-0007jI-Cl for pgsql-sql@postgresql.org; Fri, 03 Jun 2016 18:56:15 +0000 Received: from karnak.local ([82.0.227.8]) by know-smtprelay-4-imp with bizsmtp id 2Jw71t0130BWF8201Jw7um; Fri, 03 Jun 2016 19:56:07 +0100 X-Originating-IP: [82.0.227.8] X-Spam: 0 X-Authority: v=2.1 cv=a9rNjhmF c=1 sm=1 tr=0 a=R+KqYUlj5m8sYyhcT1MgyA==:117 a=R+KqYUlj5m8sYyhcT1MgyA==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=pD_ry4oyNxEA:10 a=pGLkceISAAAA:8 a=mK_AVkanAAAA:8 a=Hpo1PF0Q8QIbg2-7S8wA:9 a=k0jzqO-mU1jmlblI:21 a=0KWInZKnZPTfhXFv:21 a=QEXdDO2ut3YA:10 a=6kGIvZw6iX1k4Y-7sg4_:22 a=3gWm3jAn84ENXaBijsEo:22 Received: from [127.0.0.1] (localhost [127.0.0.1]) by karnak.local (Postfix) with ESMTP id 0AD082025 for ; Fri, 3 Jun 2016 19:56:06 +0100 (BST) Subject: Re: NOT NULL CHECK (mycol !='') :good idea? bad idea? To: pgsql-sql@postgresql.org References: From: David W Noon Organization: Luton Operatic Society Message-ID: <5751D2C5.3080303@googlemail.com> Date: Fri, 3 Jun 2016 19:56:05 +0100 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 1.0 (+) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Fri, 3 Jun 2016 11:16:33 -0700, Michael Moore (michaeljmoore@gmail.com) wrote about "[SQL] NOT NULL CHECK (mycol !='') :good idea? bad idea?" (in ): > In Oracle, a NOT NULL constraint on a table column of VARCHAR in > essence says: "You need to put at least 1 character for a value". > There is no such thing as a zero-length string in Oracle, it's > either NULL or it has some characters. So Oracle is not compliant with ANSI standard SQL. > To make Postgres perform an equivalent column edit, I am > considering defining table columns like ... mycol VARCHAR(20) NOT > NULL CHECK (mycol !='') > > Is there any drawback to this? Is there a better way to do it? Any > thoughts? how about .... mycol VARCHAR(20) NOT NULL CHECK > (length(mycol) > 0) This looks like the best, as it checks the NULL status first (cheap check) and then the length, which is also determined quite quickly from the varlena descriptor. > or even mycol VARCHAR(20) CHECK (length(mycol) > > 0) I'm not sure what result LENGTH() returns if a NULL is supplied, but I would guess that it's NULL. This would make the comparison NULL > 0, which could be anything but probably FALSE. I would assert NOT NULL in the declaration to ensure that NULL values are eliminated before length checks. I assume that the problem domain does not require the ability to enter a zero-length string into that column, as this approach will replicate Oracle's NOT NULL semantics for that column. - -- Regards, Dave [RLU #314465] *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* david.w.noon@googlemail.com (David W Noon) *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iEYEARECAAYFAldR0sQACgkQogYgcI4W/5Qq4ACfRceTL7PRcG6F24A2nPzuxhui 0rYAn1PFHV0F2ivujaWk4mO6f3Gn7SMI =eGoG -----END PGP SIGNATURE----- -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql