Re: Primary -- foreign keys

From: Pavol Obecajcik <Pavol.Obecajcik_at_onatix.com>
Date: 10 Aug 2001 13:07:24 -0700
Message-ID: <f19254f5.0108101207.6c3e4535_at_posting.google.com>


prk25_at_yahoo.com (keith) wrote in message news:<adbca63f.0108100550.b176fc5_at_posting.google.com>...
> Hi
>
> I am trying to understand why would anyone want to create the
> following:
>
> create table abc
> (
> x integer primary key,
> y char(20),
> foreign key (x) references abc(x)
> );
>
> What's being accomplished by creating a forein key which references a
> primary key of the same table??? Primary and foreign keys are on the
> same column of the same table????
>
> Thanks
>
> Keith

Hi,
  This is nonsense :-).

  Primary key is (must be) UNIQUE and NOT NULL.

  1. Foreign key must hold only existing values from referenced column (eventually NULL) => violated unique restriction.
  2. NOT NULL restriction => impossible to insert first row in table

try this:
 create table abc
 (

     x integer not null primary key,
     parent_abc integer, 
     y char(20)

 )
 ;
 alter table abc add foreign key (parent_abc) references abc(x)  ;

Palo Received on Fri Aug 10 2001 - 22:07:24 CEST

Original text of this message