This is a discussion on key on more table within the MySQL Database forums, part of the Database Forums category; Hi, do you know how can I declare a key on more tables? For example, if I have the table ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 -- ------- email: thinkmarco@yahoo.it ------- --- avast! Antivirus: In partenza messaggio pulito. Virus Database (VPS): 0546-1, 15/11/2005 Controllato il: 15/11/2005 14.06.45 avast! - copyright (c) 1988-2005 ALWIL Software. http://www.avast.com |
|
|||
|
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 > Macro, You can't. The purpose of a key is to uniquely identify a row in a single table. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|||
|
Jerry Stuckle wrote:
> 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 >> > > > Macro, > > You can't. The purpose of a key is to uniquely identify a row in a > single table. > s/uniquely// |
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|