PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Unique IDs across two tables (newbie)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Unique IDs across two tables (newbie)

Réponse
 
LinkBack Outils de la discussion
Vieux 07/02/2008, 18h37   #1
philronan@blueyonder.co.uk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Unique IDs across two tables (newbie)

I'm working on importing a large XML file into a mySQL database.
Without going into too many irrelevant details, the XML schema
includes a cross-reference field that can refer to a unique entry in
either of two separate tables. As a trivial example:

:
<fruit>
<fruit_name>apple</fruit_name>
<xref>potato</xref> <!-- Cross-reference to "vegetable" element -->
</fruit>
<fruit>
<fruit_name>orange</fruit_name>
<xref>apple</xref> <!-- Cross-reference to "fruit" element -->
</fruit>
:
<vegetable>
<vegetable_name>potato</vegetable_name>
</vegetable>
:

Here, the content of the <xref> element can be the primary key of a
<fruit> element or a <vegetable> element. Instead of storing the text
value of these <xref> elements, I want to store a value that uniquely
identifies the element it refers to. Something like this perhaps:

:: Table `fruit`
+----------+-----------------+-----------+
| fruit_id | fruit_name | xref |
+----------+-----------------+-----------+
| 1 | apple | 3 |
+----------+-----------------+-----------+
| 2 | orange | 1 |
+----------+-----------------+-----------+

:: Table `vegetables`:
+----------+-----------------+-----------+
| veg_id | veg_name | xref |
+----------+-----------------+-----------+
| 3 | potato | NULL |
+----------+-----------------+-----------+

Ideally I'd like to somehow link the ID values in the `fruit` and
`vegetable` tables so that when one is incremented, the other is also
incremented. Is this possible?

Alternatively I guess I could use a separate xref column for each type
of target. But this doesn't seem to be very efficient because one of
these IDs will always be null (they can't both be set).

A third option would be to combine the "fruit" and "vegetable" data
into a single table, but in the XML file I'm working from, the "fruit"
and "vegetables" are very different and combining them would result in
a lot more redundancy.

I hope this all makes sense. Can anyone suggest the best way of fixing
this?
  Réponse avec citation
Vieux 08/02/2008, 05h35   #2
ThanksButNo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unique IDs across two tables (newbie)

On Feb 7, 10:37 am, philro...@blueyonder.co.uk wrote:
> I'm working on importing a large XML file into a mySQL database.
> Without going into too many irrelevant details, the XML schema
> includes a cross-reference field that can refer to a unique entry in
> either of two separate tables. As a trivial example:
>
> :
> <fruit>
> <fruit_name>apple</fruit_name>
> <xref>potato</xref> <!-- Cross-reference to "vegetable" element -->
> </fruit>
> <fruit>
> <fruit_name>orange</fruit_name>
> <xref>apple</xref> <!-- Cross-reference to "fruit" element -->
> </fruit>
> :
> <vegetable>
> <vegetable_name>potato</vegetable_name>
> </vegetable>
> :
>
> Here, the content of the <xref> element can be the primary key of a
> <fruit> element or a <vegetable> element. Instead of storing the text
> value of these <xref> elements, I want to store a value that uniquely
> identifies the element it refers to. Something like this perhaps:
>
> :: Table `fruit`
> +----------+-----------------+-----------+
> | fruit_id | fruit_name | xref |
> +----------+-----------------+-----------+
> | 1 | apple | 3 |
> +----------+-----------------+-----------+
> | 2 | orange | 1 |
> +----------+-----------------+-----------+
>
> :: Table `vegetables`:
> +----------+-----------------+-----------+
> | veg_id | veg_name | xref |
> +----------+-----------------+-----------+
> | 3 | potato | NULL |
> +----------+-----------------+-----------+
>
> Ideally I'd like to somehow link the ID values in the `fruit` and
> `vegetable` tables so that when one is incremented, the other is also
> incremented. Is this possible?
>
> Alternatively I guess I could use a separate xref column for each type
> of target. But this doesn't seem to be very efficient because one of
> these IDs will always be null (they can't both be set).
>
> A third option would be to combine the "fruit" and "vegetable" data
> into a single table, but in the XML file I'm working from, the "fruit"
> and "vegetables" are very different and combining them would result in
> a lot more redundancy.
>
> I hope this all makes sense. Can anyone suggest the best way of fixing
> this?


Add a new table -- call it, say, "edibles".

Columns: unique_seq, edible_type

As you add fruits, add (new unique_seq, 'fruit'), to this table, then
set 'fruit_id' to that new unique sequence for the fruit table also.

As you add vegetables, add (new unique_seq, 'vegetable') to this
table, same with sequence again for the 'veg_id' column.

And this is expandable: meats, deserts, poisons, regiftable fruitcake,
etc.

The relationship is:

fruits IS-A edibles
vegetables IS-A edibles
meats IS-A edibles
  Réponse avec citation
Vieux 08/02/2008, 14h18   #3
philronan@blueyonder.co.uk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unique IDs across two tables (newbie)

On 8 Feb, 05:35, ThanksButNo <no.no.tha...@gmail.com> wrote:
>
> Add a new table -- call it, say, "edibles".
>
> Columns: unique_seq, edible_type
>
> As you add fruits, add (new unique_seq, 'fruit'), to this table, then
> set 'fruit_id' to that new unique sequence for the fruit table also.
>
> As you add vegetables, add (new unique_seq, 'vegetable') to this
> table, same with sequence again for the 'veg_id' column.


Ah, of course. I had a feeling I was missing something obvious.
Sometimes I just can't see the wood for the trees.

Many thanks.

Have yourself a slice of regiftable fruitcake.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h00.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14036 seconds with 11 queries