【问题标题】:Move data between tables在表之间移动数据
【发布时间】:2016-02-22 13:59:30
【问题描述】:

我目前在我的一个小项目中使用 Liquibase,效果很好。 但现在我面临一个问题。我的 ChangeLog 在我的 testenv 中按预期工作,但在我的生产环境中失败。发生这种情况是因为我的产品表包含几行数据。

我知道 liquibase 中有一个 UPDATE-Command,但我不确定如何正确使用它。

我要存档的是将列从表 B 移动到表 A 而不会丢失其数据。表 B 包含表 A 的外键。正常的 SQL 语句看起来像 update A set A.x = (select B.x from B where B.id = A.id)

如果有人能给我一个这样的更新变更集的例子,那就太好了。

谢谢!

【问题讨论】:

  • 最简单的做法是使用<sql> 标签并将update 语句写入该标签。我发现<update> 标签 使用起来非常麻烦。
  • 谢谢!这工作正常。看看它如何与<update> 标签一起工作对我来说仍然很有趣

标签: liquibase


【解决方案1】:

可能看起来像

<changeSet ...>
    <update tableName="TABLE_A">
            <column name="x" valueComputed="(select b.x from TABLE_B b where b.id=id)"/>
    </update>
</changeset>

【讨论】:

    【解决方案2】:

    如果你想从多个表中获取,你应该有一个where 子句,否则你最终会将之前的更新操作覆盖为空值,

    换句话说,如果xTable 的行在no matching 中键入yTable,您将遇到xTable.newColumn 设置为null 的问题

    不用担心 where 子句会解决这个问题,只需添加这个 where 子句:

    xTable.itsId =(the same select statement in valueComputed but select Id instead)
    

    这是一个真实的例子

    <changeSet id="0.0.6.1" author="bmomani">
     .....
        <update tableName="change">
            <column name="WIDGET_ID" valueComputed="(SELECT insert_widget.WIDGET_ID FROM insert_widget WHERE change.ID = insert_widget.ID)"/>
            <where>change.id = (SELECT insert_widget.ID FROM insert_widget WHERE change.ID = insert_widget.ID)</where>
        </update>
    
        <update tableName="change">
            <column name="WIDGET_ID" valueComputed="(SELECT remove_widget.WIDGET_ID FROM remove_widget WHERE change.ID = remove_widget.ID)">
            </column>
            <where>change.id = (SELECT remove_widget.ID FROM remove_widget WHERE change.ID = remove_widget.ID)</where>
        </update>
        <comment>note that we can do this in one update statement if we used union</comment>
    
        <comment> optional to drop column</comment>
        <!--<dropColumn tableName="insert_widget" columnName="widget_id"/>-->
        <!--<dropColumn tableName="remove_widget" columnName="widget_id"/>-->
    </changeSet>
    

    在这个sn-p中,我想将widget_id列从insert_widget表移动到change表,但是更改表已经有数据,所以我必须使用更新语句

    感谢https://stackoverflow.com/a/224807/4251431这里的这个答案,它帮助我找出了查询

    【讨论】:

      猜你喜欢
      • 2011-04-23
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2016-11-29
      • 2018-09-10
      相关资源
      最近更新 更多