Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquibase: Copy column data into a new column in the same table

I have a table with a column A. I am creating a new column B. B will have the same data as column A. How do I replicate the column in Liquibase? Is there some expression I can write to do the replication?

like image 783
sindhunk Avatar asked Mar 31 '16 00:03

sindhunk


People also ask

How do you copy values from one column to another?

Select the row or column that you want to move or copy. In the cell, click where you want to paste the characters, or double-click another cell to move or copy the data. or press Ctrl+V. Press ENTER.

How do I copy data from one column to another column in SQL Server?

UPDATE table SET columnB = columnA; This will update every row. This will also work if you want to transfer old value to other column and update the first one: UPDATE table SET columnA = 'new value', columnB = columnA . Like other answer says - don't forget the WHERE clause to update only what's needed.

How can I UPDATE one column value to another column in the same table in SQL Server?

In such a case, you can use the following UPDATE statement syntax to update column from one table, based on value of another table. UPDATE first_table, second_table SET first_table. column1 = second_table. column2 WHERE first_table.id = second_table.


Video Answer


2 Answers

Create a new changeset where you add a new column, and then update column B using the <sql> tag:

<changeSet author="yourName" id="example">
    <addColumn catalogName="db"
               schemaName="public"
               tableName="yourTable">
        <!-- replace varchar(255) with the actual type of column A -->
        <column name="B" type="varchar(255)"/>
    </addColumn>

    <sql>UPDATE yourTable SET B = A</sql>
</changeSet>
like image 177
Tim Biegeleisen Avatar answered Sep 19 '22 08:09

Tim Biegeleisen


this is possible too:

<changeSet id="1" author="your_name">
    <addColumn tableName="your_table">
        <column name="b" type="varchar(255)"/>
    </addColumn>
</changeSet>

<changeSet id="2" author="your_name">
    <update tableName="your_table">
        <column name="b" valueComputed="a"/>
    </update>
</changeSet>
like image 31
Vasily Konyaev Avatar answered Sep 22 '22 08:09

Vasily Konyaev