X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 11DE8D1DF23; Thu, 11 Dec 2003 15:20:18 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 56160-09; Thu, 11 Dec 2003 11:19:50 -0400 (AST) Received: from noon.pghoster.com (noon.pghoster.com [64.246.0.64]) by svr1.postgresql.org (Postfix) with ESMTP id B8E64D1DF16; Thu, 11 Dec 2003 11:19:45 -0400 (AST) Received: from [61.5.0.73] (helo=[192.168.0.4]) by noon.pghoster.com with asmtp (Exim 4.24) id 1AUSbM-00034G-RP; Thu, 11 Dec 2003 09:19:45 -0600 Subject: Re: Should we consider empty fields as NULL values when From: Chris Travers To: Nagib Abi Fadel Cc: generalpost , developPost In-Reply-To: <001401c3bff4$49abd7f0$f664a8c0@nagib> References: <001401c3bff4$49abd7f0$f664a8c0@nagib> Content-Type: text/plain Organization: Message-Id: <1071152091.2212.682.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 (1.2.2-4) Date: 11 Dec 2003 22:15:57 +0800 Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - noon.pghoster.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - travelamericas.com X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200312/631 X-Sequence-Number: 54025 On Thu, 2003-12-11 at 22:36, Nagib Abi Fadel wrote: > HI, > let's say we have the following table : > > # CREATE TABLE tempo (col1 varchar(3) not null); > CREATE TABLE > > # insert INTO tempo VALUES (''); > INSERT 11420541 1 > > the insert command works. > > The issue is that since the column col1 is defined as character with > not null attribute, > shouldn't we deny such inserts (i mean inserting empty fields)??? > (PS: i am using postresql 7.3.2) NULL has a special meaning and CAST(NULL, string) != '' Remember that NULL is a special value and does not equate to any other value. For example you may KNOW that the value of a string is '', but if you don't know what the value is, then NULL is the value which represents that unknown. For this reason, NULL || 'Mystring' IS NULL '' || 'MyString' = 'MyString' In the first case, we are appending 'Mystring' to an unknown string (hence the result is unknown), and in the second, we append 'MyString' to an empty string. Hence the value is the same. > > When using script languages (like PHP) if by mistake the variable is > not defined such insert is possible (let's say we have a variable > $col1_value and after a long day of work we make a mistake and write > it $col_value). The only way to handle this is to write your own routines to check the values and substitute as appropriate. That is not the answer you were looking for, but... For example (PHP): function db_quote($db_var){ if ($db_var === NULL){ return 'NULL'; } else { return "'$db_var'"; } } This will enclose your variable with single-quotes unless it is not set in which case it will return a string, "NULL" which can be used in your database queries. Best Wishes, Chris Travers > > This "problem" is solved by adding the constraint: > ALTER TABLE tempo add constraint col1_check check(col1!=''); >