【问题标题】:PHP, script for moving db2 data from one database to anotherPHP,用于将 db2 数据从一个数据库移动到另一个数据库的脚本
【发布时间】:2020-02-11 14:33:49
【问题描述】:

我需要将开发数据库上的表中的一些数据移植到生产数据库上的相同表中,但生产已经有与开发数据库匹配的主键记录,因此我无法使用主键bundleRenderer 转储数据。渲染到流

在这种情况下,item_id 是父记录中的主键,用于关联子记录。插入父记录将创建一个新的主键,因此我需要子插入也具有新创建的主键,以便在生产数据库bundleRenderer.renderToStream 上维护关系

到目前为止我的脚本:

<?php
$DB2connPROD = odbc_connect("schema","user", "pass");
$DB2connDEV = odbc_connect("schema","user", "pass");


//Get itemt records from dev

$getDevitems = "
    select item_id,item_typet_id,item_identifier,expiration_timestamp 
    from development.itemt where item_typet_id in (2,3)
";

//$getDevitems will get records that have a primary key item_id which is used to get the records in the following select queries

foreach($getDevitems as $items){

    //Get all comments

    $getComments = "
    select tc.item_id, tc.comment, tc.comment_type_id from development.item_commentt tc
    inner join development.itemt t on tc.item_id = t.item_id
    where t.item_id = {item_id_from_getDevitems}
    ";

    $insertitem = "INSERT into production (item_identifier,expiration_timestamp)
    values (item_identifier,expiration_timestamp)";

    $insertComment = "INSERT into productionComment (item_id, comment, comment_type_id)
    values (item_id, comment, comment_type_id)";

}

?>

所以如果 $getDevitems 返回

item_id  |  item_typet_id  |  item_identifier  |  expiration_timestamp
----------------------------------------------------------------------------
123             1                   544                 '2020-03-01 12:00:00'

我希望它现在在 where 子句中以 123 作为 ID 运行评论选择:

select tc.item_id, tc.comment, tc.comment_type_id from development.item_commentt tc
inner join development.itemt t on tc.item_id = t.item_id
where t.item_id = 123

现在,对于我的旧父记录,我拥有所有父数据和所有关系子数据。所以我想将新的父记录插入数据库,创建新的 ID,并使用新创建的主键/ID 插入子记录。所以对于新的父记录,我会这样做:

$insertitem = "INSERT into production (item_identifier,expiration_timestamp)
values (544,'2020-03-01 12:00:00')";

假设创建了 item_id = 43409 的新记录。我希望我的评论插入为:

$insertComment = "INSERT into productionComment (item_id, comment, comment_type_id)
values (43409, comment, comment_type_id)";

底线:我需要从开发数据库中获取关系数据(全部基于 item_id),并将它们插入到一个新数据库中,该数据库创建一个新的主键,但我需要保持关系。

我怎样才能正确地完成这个来做我需要的事情并确保我为每个最初选择的项目保持完整的关系?

【问题讨论】:

  • 是从 AS-400 db2 到 MySQL / PostgreSQL / etc 吗?伟大的项目。但问题含糊不清 - 请改进问题,以便我们提供帮助。
  • 从开发db2 for as 400到生产db2 for as400,所以源和目标在系统和结构上是相同的。除此之外,最好的改进是什么?我没有很多细节,只是一般需要保持记录之间的关系

标签: php sql db2-400


【解决方案1】:

鉴于您的插入是:

$insertitem = "INSERT into production (item_identifier,expiration_timestamp)
values (item_identifier,expiration_timestamp)";

$insertComment = "INSERT into productionComment (item_id, comment, comment_type_id)
values (item_id, comment, comment_type_id)";

您似乎正在为 item_id 使用标识列。您可以使用 IDENTITY_VAL_LOCAL() 函数检索最近生成的标识值,因此第二个插入应该是:

$insertComment = "INSERT into productionComment (item_id, comment, comment_type_id)
values (IDENTITY_VAL_LOCAL(), comment, comment_type_id)";

【讨论】:

    【解决方案2】:

    我无法为 PHP 提供帮助,但对于 DB2 for IBMi,您有不同的解决方案:

    如果我理解正确,item_id 是 GENERATED ALWAYS as IDENTITY 列

    您可以使用获取新创建的 item_id

    select item_id from final table (
      INSERT into production (item_identifier,expiration_timestamp)
      values (544,'2020-03-01 12:00:00')
    )
    

    或者您可以使用 dev 值或您自己的增量来强制 item_id 的值

      INSERT into production (idtem_id, item_identifier,expiration_timestamp)
      values (<your value>, 544,'2020-03-01 12:00:00')
      OVERRIDING SYSTEM VALUE
    

    在这种情况下,您必须通过发出设置 item_id 的下一个值

    alter table production alter column item_id restart with <restart value>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      • 2016-06-10
      • 2011-05-19
      相关资源
      最近更新 更多