I have recently updated my table schema to include a new column, which has one or several possible values depending on the row (I can pull these possible values from another table). I wish to update my table and replace each current single row with one or several new rows, where the old column values all match the original row, and the new column value covers each possible value once.
As an example, suppose I had a table called Animal, with columns AnimalType, Size, Habitat, and Weight. I add a new column called Color – some animals only have one color, some can have one of several possible colors. Suppose I have a row for Bird that looks like this:
{ AnimalType = Bird, Size = Small, Habitat = Woods, Weight = under 10 pounds, Color = NULL }
Now suppose that, according to my other table (call it AnimalColors), Birds can come in three colors — red, blue and brown.
I wish to replace that single row with three new rows that look like the following:
{AnimalType = Bird, Size = Small, Habitat = Woods, Weight = under 10 pounds, Color = Red}
{AnimalType = Bird, Size = Small, Habitat = Woods, Weight = under 10 pounds, Color = Blue}
{AnimalType = Bird, Size = Small, Habitat = Woods, Weight = under 10 pounds, Color = Brown}
I have multiple rows in my Animals table and wish to perform this operation on each of them.
Is there a simple way to do this?
Thanks!
I would look at solving this problem by leaving the existing rows as is.
Create new rows with color populated based on the existing rows
Delete the NULL rows (after verifying it looks as expected)