Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8745809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:05:44+00:00 2026-06-13T12:05:44+00:00

I have recently updated my table schema to include a new column, which has

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:05:45+00:00Added an answer on June 13, 2026 at 12:05 pm

    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)

    -- Add rows with color populated
    WITH AnimalColors (AnimalType, Color) AS
    (
        SELECT 'bird', 'red'
        UNION ALL SELECT 'bird', 'blue'
        UNION ALL SELECT 'bird', 'brown'
        UNION ALL SELECT 'lizard', 'green'
    )
    , ExistingRows(AnimalType, Size, Habitat, Weight, Color) AS
    (
        SELECT 'bird', 'small', 'woods', 'under 10 pounds', NULL
        UNION ALL SELECT 'lizard', 'small', 'rocks', 'under 10 pounds', NULL
    )
    INSERT INTO
        Animal
    (
        AnimalType
    ,   Size
    ,   Habitat
    ,   Weight
    ,   Color
    )
    SELECT
        ER.AnimalType
    ,   ER.Size
    ,   ER.Habitat
    ,   ER.Weight
    ,   AC.Color
    FROM
        AnimalColors AC
        INNER JOIN
            ExistingRows ER
            ON ER.AnimalType = AC.AnimalType;
    
    -- Manually review values of Animal table
    SELECT A.* FROM Animal A ORDER BY A.AnimalType, A.Color;
    
    -- Remove the uncolored rows
    DELETE A FROM Animal A WHERE A.Color IS NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently added a new column (age) to a database table (user). I
I have a rails environment which I set-up with macports. I recently updated macports
looks like today is going to be another rubbish one. we have recently updated
I have recently updated my ADT and it is now ADT_V20. I have also
I have recently updated my model, added a BooleanField to it however when I
I have recently updated the Cucumber gems ('cucumber' and 'cucumber-rails') on my machine and
I recently updated from MVC4 beta to the RC and have encountered a small
I recently updated a rails app from 3.0.4 to 3.2.4 and I have a
I have recently installed iOS 6 beta on my device and updated to the
Recently I updated my computer to Ubuntu 11.10 64-bits. I have a problem when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.