Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 341BE9FA67F for ; Wed, 26 Sep 2007 10:26:11 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 15344-01-4 for ; Wed, 26 Sep 2007 10:26:08 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from pro33.abac.com (pro33.abac.com [66.226.64.34]) by postgresql.org (Postfix) with ESMTP id 91F269FA693 for ; Wed, 26 Sep 2007 10:26:07 -0300 (ADT) Received: from ashishk ([203.193.139.178]) (authenticated bits=0) by pro33.abac.com (8.14.1/8.14.1) with ESMTP id l8QDPgSo009956 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO); Wed, 26 Sep 2007 06:25:55 -0700 (PDT) (envelope-from ashish.karalkar@info-spectrum.com) Message-ID: <00c501c80040$cb2bee60$170211ac@LIONKING.COM> From: "Ashish Karalkar" To: , References: <1190637286.4804.72.camel@ivangeo> Subject: Re: foreign key problem Date: Wed, 26 Sep 2007 18:55:48 +0530 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=AWL=0.234, BAYES_40=-0.185, STOX_REPLY_TYPE=0.001 X-Spam-Level: X-Archive-Number: 200709/153 X-Sequence-Number: 29366 ----- Original Message ----- From: "ivan marchesini" To: Sent: Monday, September 24, 2007 6:04 PM Subject: [SQL] foreign key problem > Dear users, > I'm trying to create constraints into a db they give me from access... > I'm using pgadmin to create a new foreign key for the relationship > between two table.. > > I have a table called "generalita" wich contains a column called > "cod_carg": > ___________________ > select cod_carg from generalita group by cod_carg; > cod_carg > ---------- > > 2 > 0 > 1 > (4 righe) > ___________________ > > > I have another table, called "diz_carg" that contain, into a column > called "cod_carg" (that is a primary key), the domain values for the > "cod_carg" column of "generalita": > __________________ > select cod_carg from diz_carg group by cod_carg; > cod_carg > ---------- > 1 > 0 > 2 > (3 righe) > __________________ > > > > so when I try to create the foreign key I obtain: > ALTER TABLE generalita ADD CONSTRAINT fkey_diz_cod_carg FOREIGN KEY > (cod_carg) REFERENCES diz_carg (cod_carg) ON UPDATE NO ACTION ON DELETE > NO ACTION; > > ____________________________ > ERROR: insert or update on table "generalita" violates foreign key > constraint "fkey_diz_cod_carg" > DETAIL: Key (cod_carg)=( ) is not present in table "diz_carg". > ____________________________ > > > can I allow null values for "generalita.cod_carg" although it is a > foreign key for "diz_carg.cod_carg"? > > I apologize if it is a stupid question.... :-( > > many thanks to all > > Yes you can , check that column cod_carg of table generalita is not set to NOT NULL. With regards Ashish