Unix Technical Forum

foreign key problem

This is a discussion on foreign key problem within the pgsql Sql forums, part of the PostgreSQL category; --> Dear users, I'm trying to create constraints into a db they give me from access... I'm using pgadmin to ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 05:55 PM
ivan marchesini
 
Posts: n/a
Default 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




--
Ti prego di cercare di non inviarmi files .doc, .xls, .ppt, .dwg.
Preferisco formati liberi.
Please try to avoid to send me .doc, .xls, .ppt, .dwg files.
I prefer free formats.
http://it.wikipedia.org/wiki/Formato_aperto
http://en.wikipedia.org/wiki/Open_format

Ivan Marchesini
Department of Civil and Environmental Engineering
University of Perugia
Via G. Duranti 93/a
06125
Perugia (Italy)
e-mail: marchesini@unipg.it
ivan.marchesini@gmail.com
tel: +39(0)755853760
fax (university): +39(0)755853756
fax (home): +39(0)5782830887
jabber: geoivan73@jabber.org


---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 05:55 PM
Lew
 
Posts: n/a
Default Re: foreign key problem

ivan marchesini wrote:
> ____________________________
> 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"?


Yes. However, do be sure that the value in question is actually NULL, and
not, say, ''.

--
Lew
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 05:55 PM
Ashish Karalkar
 
Posts: n/a
Default Re: foreign key problem


----- Original Message -----
From: "ivan marchesini" <marchesini@unipg.it>
To: <pgsql-sql@postgresql.org>
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


---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:46 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com