View Full Version : How do I create a table that contains all records from two others


Wayward
07-11-2005, 03:32 AM
I'm trying to create a table that automatically contains records for every permutation of the two foreign keys it imports. Let me explain with an example:

I have a Characters table:
CharacterID (Primary Key)
CharacterName
CharacterClass
[...]

And, I have a Skills table:
SkillID (Primary Key)
SkillName
SkillDescription
[...]

I want a third table named CharacterSkills to combine those two tables. It might look like this:
CharacterID (Primary Key)
SkillID (Primary Key)
CharacterSkillSlot
[...]

That looks a lot like a junction table for a many-to-many relationship. The difference is I want this table automatically filled with every combination of Characters and Skills. For example, if I have three characters and five skills this table would contain fifteen records. If I add a fourth character I'd expect this table to grow by another five records (as one record per skill is automatically added).

Here are those same tables with some example data.

Characters table:
CHR01, King Loo, [...]
CHR02, Humble Pie, [...]
CHR03, Jayne Bo, [...]

Skills table:
SK01, Amity, [...]
SK02, Diversion, [...]
SK03, Banish, [...]
SK04, Guilt, [...]
SK05, Shame, [...]

CharacterSkills table:
CHR01, SK01, [...]
CHR01, SK02, [...]
CHR01, SK03, [...]
CHR01, SK04, [...]
CHR01, SK05, [...]

CHR02, SK01, [...]
CHR02, SK02, [...]
CHR02, SK03, [...]
CHR02, SK04, [...]
CHR02, SK05, [...]

CHR03, SK01, [...]
CHR03, SK02, [...]
CHR03, SK03, [...]
CHR03, SK04, [...]
CHR03, SK05, [...]

Any pointers on how to create this magical third table, or whether it' possible at all, will be greatly appreciated.

Mile-O
07-11-2005, 03:58 AM
Have a look at the attachment - it creates a Cartesian in a query to get all possible variations and the query is also an append query set to append all the returned records into your proper table.

Wayward
07-11-2005, 04:21 AM
Have a look at the attachment - it creates a Cartesian in a query to get all possible variations and the query is also an append query set to append all the returned records into your proper table.
Thank you very much for your time and effort. You've solved my problem for me. It's also useful to know that this kind of table has a name so I can find out a little bit more about them.

Mile-O
07-11-2005, 08:47 AM
It's also useful to know that this kind of table has a name so I can find out a little bit more about them.

If you mean Cartesian (http://en.wikipedia.org/wiki/Cartesian_product) then that's not a type of table; it's a mathematical product.

Wayward
07-11-2005, 09:50 AM
If you mean Cartesian (http://en.wikipedia.org/wiki/Cartesian_product) then that's not a type of table; it's a mathematical product.
Nethertheless, 'cartesian' and 'database table' did give me a starting point with the search engines where I found these definitions in database glossaries:
Cartesian product

A join with no join condition results in a Cartesian product, or a cross product. A Cartesian product is the set of all possible combinations of rows drawn one from each table. In other words, for a join of two tables, each row in one table is matched in turn with every row in the other. A Cartesian product for more than two tables is the result of pairing each row of one table with every row of the Cartesian product of the remaining tables. All other kinds of joins are subsets of Cartesian products effectively created by deriving the Cartesian product and then excluding rows that fail the join condition.

cross product

Table formed by joining all possible combinations of the rows of two other tables. This is the basis for most joins (default or inner join). Same as Cartesian product.
The above definitions were informative to me because I'd been trying to create the cartesian product of two tables by tampering with the joins.