How to link foreign keys & primary keys using python?

Steve Holden steve at holdenweb.com
Mon Jun 12 02:51:21 EDT 2006


sonal.patankar at 3i-infotech.com wrote:
> Hi Mr. George,
> 
> Let me try it again...
> 
>  I am not using any relational database to store the required tables
> with primary keys & foreign keys....
> 
None the less, you are using relational database terminology. If you 
want people to understand you then you should use it correctly or 
explain yourself in other ways ...

> When I say PRIMARY KEY =>
> 1. It means an index is created on the specified fields
>     (Out of various fields given in the comma separated txt file)
> FileFormat: CODE, FIRST_NAME, last_name, area_of_expertise, country
> Eg:  A1,Harry,George, python, XYZCOUNTRY--------(1st record)
> 
> 2. The index can be formed on a single field or on multiple fields
>     Eg: a. 'CODE' (single field ) {pk_code}
>           b. 'CODE' & 'NAME' (multiple fields ) {pk_code_fname}
> 
> Now when I say FOREIGN KEY =>
> 1. If the foreign Key is formed on the field 'CODE' in another text
> file
>  Format: subsriber_code,CODE,first_name, no_of_posts,active(Y/N)
>  Eg:   SUB_001, A1, Harry, 50, Y
> 
>    This means the CODE (A1) given here is checked in the index formed
> above
>    with primary key: pk_code...
> 
> 2. If the foreign Key is formed on the fields 'CODE' & 'FIRST_NAME'
>     Format: subsriber_code,CODE,FIRST_NAME, no_of_posts,active(Y/N)
>         Eg:   SUB_001, A1, Harry, 50, Y
> 
>    This means the CODE (A1) & FIRST_NAME (Harry) given here
>    are checked in the index formed above with primary key:
> pk_code_fname...
> 
> I am done till here.....
> 
> The problem starts if I have defined an index on multiple fields
> (composite PK)
> say: CODE & FIRST_NAME (pk_code_fname)
> and if I need to define a FK on a single field out of these
> say: CODE
> 
> I am unable to do that...
> Mr. George, I thought i must explain the code i am dealin with,
> for better understanding.., but i am sorry i confused you all the more
> (incase, u want to view the code please refer to the code snippets in
> my first query posted)
> 
> I hope you atleast get an idea of what i am hunting for.... :(
> 
You say you have to use existing code, which is a pity because it seems 
that code is organised in such a way as to make your problem difficult.

The only ways to locate information by a *portion* of the primary key, 
given that you appear to be using tuples as primary key values, is 
either to maintain a separate index on the partial key (in other words 
have a lookup table that holds the partial key values).

Note, also, that you run a risk here: there is no guarantee that only 
one record will have any given partial key value.

The whole point of foreign keys in databases is that they are *complete* 
primary key values, and can therefore refer at at most one occurrence of 
the referenced entity. It is not correct to refer to a reference to a 
partial primary key as a "foreign key", precisely because all guarantees 
of uniqueness are lost.

It seems that you are not a native English speaker, so I am sorry if 
this is leading to further problems.

The more simply you can explain yourself the easier it will be to help!

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Love me, love my blog  http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list