I the following two tables/columns:
TableA
id | data
TableB
fkeyA1 | fkeyA2 | fkeyA3 | name
The first three columns in TableB are foreign keys all pointing to TableA. I would like to transform TableB into the following two tables:
TableC
id | name
TableD
fkeyC | fkeyA
so that one row in TableB becomes one row in TableC plus three rows in TableD. How should I write a SQL query for this?
EDIT:
In TableB, there is a unique index on (fkeyA1, fkeyA2, fkeyA3), but the name column does not necessarily have unique values.
You can use a cursor statement:
This may not be the best in performance, but will allow for non-unique values for the name column.