在表之间移动数据
Move data between tables
我目前在我的一个小项目中使用 Liquibase,效果很好。
但现在我面临一个问题。我的 ChangeLog 在我的 testenv 中按预期工作,但在我的 productiv 中失败。发生这种情况是因为我的 prod-tables 包含几行数据。
我知道 liquibase 中有一个更新命令,但我不确定如何正确使用它。
我想要存档的是将列从 table B 移动到 table A 而不会丢失其数据。 Table B 包含 table A 的外键。正常的 SQL-语句看起来像 update A set A.x = (select B.x from B where B.id = A.id)
如果有人能给我一个这样的更新变更集的例子就好了。
谢谢!
可能看起来像
<changeSet ...>
<update tableName="TABLE_A">
<column name="x" valueComputed="(select b.x from TABLE_B b where b.id=id)"/>
</update>
</changeset>
如果你想从多个 table 你应该有一个 where
子句,否则你最终会覆盖以前的更新操作为空值,
换句话说,对于在 yTable
中具有 no matching
键的 xTable
行,您将遇到 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>
在此代码段中,我想将 widget_id
列从 insert_widget
table 移动到 change
table,但已经更改 table有数据,所以我必须使用更新语句
感谢这里的回答 它帮助我弄清楚了查询
我目前在我的一个小项目中使用 Liquibase,效果很好。 但现在我面临一个问题。我的 ChangeLog 在我的 testenv 中按预期工作,但在我的 productiv 中失败。发生这种情况是因为我的 prod-tables 包含几行数据。
我知道 liquibase 中有一个更新命令,但我不确定如何正确使用它。
我想要存档的是将列从 table B 移动到 table A 而不会丢失其数据。 Table B 包含 table A 的外键。正常的 SQL-语句看起来像 update A set A.x = (select B.x from B where B.id = A.id)
如果有人能给我一个这样的更新变更集的例子就好了。
谢谢!
可能看起来像
<changeSet ...>
<update tableName="TABLE_A">
<column name="x" valueComputed="(select b.x from TABLE_B b where b.id=id)"/>
</update>
</changeset>
如果你想从多个 table 你应该有一个 where
子句,否则你最终会覆盖以前的更新操作为空值,
换句话说,对于在 yTable
中具有 no matching
键的 xTable
行,您将遇到 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>
在此代码段中,我想将 widget_id
列从 insert_widget
table 移动到 change
table,但已经更改 table有数据,所以我必须使用更新语句
感谢这里的回答 它帮助我弄清楚了查询