View Single Post

  #4 (permalink)  
Old 11-15-2005
Dikkie Dik
 
Posts: n/a
Default Re: key on more table

myname@yahoo.it wrote:
> Hi,
> do you know how can I declare a key on more tables?
>
> For example, if I have the table TABLE1 and the table TABLE2, every one
> with a field called key, how can OI declare in mysql that the key is
> composed of TABLE1.key AND TABLE2.key?
>
> Thanks a lot
> Marco Righi
>

I don't know what you are trying to accomplish, but if you want a key
that appears as a field in more than one table and is still unique in
all those tables, you can introduce an identity table.

This identity table "generates" the keys with an auto-increment field
(primary key), and it is referrred to by foreign keys in the tables that
"share" the relation. Any time a foreign key field needs to be written,
a row is inserted in the identity table and its last genereated ID is used.
This is no hard check for uniqueness: the code that adds records must
generate an ID first. The uniqueness comes from the fact that all
generated IDs are different.

When I first encountered an ID-only table in my own database scheme, I
had given it a really hard thought. Somehow it didn't feel right. But
every other option would have given me a worse scheme and now I think
there can be situations where it is useful.

Best regards.
Reply With Quote