简体   繁体   English

PHP,用于将 db2 数据从一个数据库移动到另一个数据库的脚本

[英]PHP, script for moving db2 data from one database to another

I need to port some data from tables on a development database into identical tables on the production database, but the production already has records with primary keys that match the dev database so I can't dump the data in with primary keysbundleRenderer.renderToStream我需要将一些数据从开发数据库的表移植到生产数据库的相同表中,但是生产已经有主键与开发数据库匹配的记录,所以我不能用主键 bundleRenderer.renderToStream 转储数据

In this case, item_id is the primary key in the parent record, which is used to relate child records to it.在这种情况下, item_id是父记录中的主键,用于将子记录关联到它。 Doing the insert of parent records will create a new primary key, so I need child inserts to also have the newly created primary key so that the relationship is maintained on the production databasebundleRenderer.renderToStream插入父记录将创建一个新的主键,因此我需要子插入也具有新创建的主键,以便在生产数据库 bundleRenderer.renderToStream 上维护关系

my script so far:到目前为止我的脚本:

<?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)";

}

?>

So if $getDevitems returns所以如果 $getDevitems 返回

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

I would want it to now run the comment select with 123 as the ID in the where clause:我希望它现在运行评论 select,其中 123 作为 where 子句中的 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

Now for my legacy parent record I have all of the parent data and all of the relational child data.现在,对于我的遗留父记录,我拥有所有父数据和所有关系子数据。 so I want to insert the new parent record into the database, creating the new ID, and inserting the child record with the newly created primary key/ID.所以我想将新的父记录插入数据库,创建新的 ID,并使用新创建的主键/ID 插入子记录。 So for the new parent record I would do:因此,对于新的父记录,我会这样做:

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

Let's say that creates the new record with item_id = 43409. I want my comment insert to be:假设创建了 item_id = 43409 的新记录。我希望我的评论插入是:

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

Bottom LIne: I need to take relational data (all based on item_id) from a development database, and insert these into a new database which creates a new primary key but I need to keep the relationship.底线:我需要从开发数据库中获取关系数据(全部基于 item_id),并将这些数据插入到新数据库中,这会创建一个新的主键,但我需要保持这种关系。

How can I properly finish this to do what I need and make sure I maintain the full relationship for each originally selected item?我怎样才能正确地完成它来做我需要的事情并确保我保持每个最初选择的项目的完整关系?

I cannot help with PHP but with DB2 for IBMi you have different solutions:我无法帮助 PHP 但 DB2 对于 IBMi 你有不同的解决方案:

If i understand it correctly item_id is a GENERATED ALWAYS as IDENTITY column如果我理解正确,item_id 是一个 GENERATED ALWAYS as IDENTITY 列

You can get newly created item_id using您可以使用获取新创建的 item_id

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

Or you can force value of item_id with dev value or your own increment或者您可以使用开发值或您自己的增量强制 item_id 的值

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

In this case you will have to set next value for item_id by issueing在这种情况下,您必须通过发出以下命令为 item_id 设置下一个值

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

Given that your inserts are:鉴于您的插入是:

$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)";

It looks like you are using an identity column for item_id.看起来您正在为 item_id 使用标识列。 You can retrieve the most recent generated identity value using the IDENTITY_VAL_LOCAL() function so the second insert should be:您可以使用IDENTITY_VAL_LOCAL() function 检索最近生成的标识值,因此第二个插入应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM