Afficher un message
Vieux 07/02/2008, 19h37   #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
 
Page generated in 0,06322 seconds with 9 queries