I am trying to insert an XML file of the following kind:
<thing>
<name>one</name>
<type>metal</type>
<type>round</type>
</thing>
<thing>
<name>two</name>
<type>round</type>
</thing>
<thing>
<name>three</name>
<type>metal</type>
<type>round</type>
</thing>
into an SQL database. There are lots of <thing>
elements and each has one or more <type>
elements. There are a lot of things, but only a few repeated different patterns of the <type>
element which each thing might have, so I have created a table of thing
, which has columns id
and pattern
, a table of pattern
, a table of type_pattern
which has columns type_id
and pattern_id
, and a table of type
which has a txt
column for the word, such as metal
or round
, and an id
column. As I parse the XML file, I wish to categorize each thing
into a particular type_pattern
, which is the pattern of types which it matches. For example, things one
and three
in the above match a pattern of having metal
and round
type, but thing two
has a different pattern of only round
type. So the database table for the above might look like
thing
id pattern_id name
1 1 one
2 2 two
3 1 three
type_pattern
pattern_id type_id
1 1
1 2
2 1
type
id txt
1 metal
2 round
The point is, I want to not have a table of thing and type, but a table of thing
, type_pattern
, and type
.
My question is, given a list of types, how should I write an SQL query to get the pattern id?
Or am I going about this the wrong way?