转换一个 Oracle 表(数据)以填充另一个略有不同的 Oracle 表的最佳方法是什么?

发布于 2024-10-04 07:06:58 字数 285 浏览 5 评论 0原文

我有两张 Oracle 表,一张旧一张,一张新的。 旧表的设计很糟糕(请注意,比我的更糟糕),但是有很多当前数据需要迁移到我创建的新表中。

新表有新列,不同的列。

我想到只编写一个 PHP 脚本或带有一大堆字符串替换的东西......显然这是一种愚蠢的方法。

我真的很希望能够在此过程中清理一下数据。有些它是用标记存储的(例如:“
名字
”),大量空格等,所以我真的很想在将其放入新表之前修复所有这些问题。

有没有人有做这样的事情的经验?我应该怎么办?

谢谢 :)

I have two Oracle tables, an old one and a new one.
The old one was poorly designed (more so than mine, mind you) but there is a lot of current data that needs to be migrated into the new table that I created.

The new table has new columns, different columns.

I thought of just writing a PHP script or something with a whole bunch of string replacement... clearly that's a stupid way to do it though.

I would really like to be able to clean up the data a bit along the way as well. Some it was stored with markup in it (ex: "
First Name
"), lots of blank space, etc, so I would really like to fix all that before putting it into the new table.

Does anyone have any experience doing something like this? What should I do?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花开雨落又逢春i 2024-10-11 07:06:58

我经常这样做 - 您可以使用简单的选择状态进行迁移:

create table newtable as select 
 field1,
 trim(oldfield2) as field3,
 cast(field3 as number(6)) as field4,
 (select pk from lookuptable where value = field5) as field5,
 etc,
from
 oldtable

在清理和转换数据时,使用像 php 等中间语言可以做的事情实际上很少是您在本机 SQL 中做不到的。

对于更复杂的清理,您始终可以创建一个 sql 函数来完成繁重的工作,但我已经清理了一些非常糟糕的数据,而没有诉诸于此。不要忘记在oracle中你有decode、case语句等。

I do this quite a bit - you can migrate with simple select statememt:

create table newtable as select 
 field1,
 trim(oldfield2) as field3,
 cast(field3 as number(6)) as field4,
 (select pk from lookuptable where value = field5) as field5,
 etc,
from
 oldtable

There's really very little you could do with an intermediate language like php, etc that you can't do in native SQL when it comes to cleaning and transforming data.

For more complex cleanup, you can always create a sql function that does the heavy lifting, but I have cleaned up some pretty horrible data without resorting to that. Don't forget in oracle you have decode, case statements, etc.

伴梦长久 2024-10-11 07:06:58

我会检查一个 ETL 工具,例如 Pentaho Kettle。您将能够从旧表中查询数据,转换和清理数据,然后将其重新插入到新表中,所有这些都可以使用一个很好的所见即所得工具完成。

这是我之前回答的关于使用 Kettle 进行数据迁移和操作的问题。
使用 Pentaho Kettle,如何从单个表加载多个表,同时保持引用完整性?

I'd checkout an ETL tool like Pentaho Kettle. You'll be able to query the data from the old table, transform and clean it up, and re-insert it into the new table, all with a nice WYSIWYG tool.

Here's a previous question i answered regarding data migration and manipulation with Kettle.
Using Pentaho Kettle, how do I load multiple tables from a single table while keeping referential integrity?

风启觞 2024-10-11 07:06:58

如果数据量不大,并且您只打算执行一次,那么将很难击败自行启动的程序。特别是如果您有一些需要实现的自定义逻辑。
下载、学习和使用所花费的时间使用工具(例如 pentaho 等)可能不值得您花时间。

编码 select *,更新内存中的列 &使用 PHP 或任何其他编程语言可以快速完成插入操作。

话虽这么说,如果您发现自己经常这样做,那么 ETL 工具可能值得学习。

If the data volumes aren't massive and if you are only going to do this once, then it will be hard to beat a roll-it-yourself program. Especially if you have some custom logic you need implemented.
The time taken to download, learn & use a tool (such as pentaho etc.) will probably not worth your while.

Coding a select *, updating columns in memory & doing an insert into will be quickly done in PHP or any other programming language.

That being said, if you find yourself doing this often, then an ETL tool might be worth learning.

空‖城人不在 2024-10-11 07:06:58

我自己正在从事一个类似的项目 - 将数据从包含几十个表的一个模型迁移到具有相似数量表的稍微不同的模型。

我采取了为每个目标表创建 MERGE 语句的方法。源查询获取所需的所有数据,根据需要对其进行格式化,然后如果该行已存在则进行合并,并根据需要更新/插入。这样,我可以在开发解决方案时多次运行该语句。

I'm working on a similar project myself - migrating data from one model containing a couple of dozen tables to a somewhat different model of similar number of tables.

I've taken the approach of creating a MERGE statement for each target table. The source query gets all the data it needs, formats it as required, then the merge works out if the row already exists and updates/inserts as required. This way, I can run the statement multiple times as I develop the solution.

云仙小弟 2024-10-11 07:06:58

取决于转换过程的复杂程度。如果可以很容易地用单个 SQL 语句来表达,那么您就已经准备好了;只需创建 SELECT 语句,然后执行 CREATE TABLE / INSERT 语句。但是,如果您需要执行一些复杂的转换或(令人震惊)拆分或合并任何行以正确转换它们,则应该使用管道表函数。但听起来情况并非如此。尝试坚持上面另一个克里斯建议的单一陈述。您绝对不想将数据从数据库中取出来进行转换,因为传入和传出 Oracle 的速度始终比将数据全部保留在数据库中要慢。

还有一些提示:

  • 如果表已存在并且您正在执行 INSERT...SELECT 语句,请在插入时使用 /*+ APPEND */ 提示,以便执行批量操作。请注意,CREATE TABLE 默认情况下会执行此操作(只要可能即可;在某些条件下无法执行批量操作,例如,如果新表是索引组织表、具有触发器等。
  • 如果您使用的是 10.2 或更高版本,则还应该考虑使用 LOG ERRORS INTO 子句将拒绝的记录记录到这样,如果一条记录出现意外错误,您就不会丢失整个操作。

Depends on how complex the conversion process is. If it is easy enough to express in a single SQL statement, you're all set; just create the SELECT statement and then do the CREATE TABLE / INSERT statement. However, if you need to perform some complex transformation or (shudder) split or merge any of the rows to convert them properly, you should use a pipelined table function. It doesn't sound like that is the case, though; try to stick to the single statement as the other Chris suggested above. You definitely do not want to pull the data out of the database to do the transform as the transfer in and out of Oracle will always be slower than keeping it all in the database.

A couple more tips:

  • If the table already exists and you are doing an INSERT...SELECT statement, use the /*+ APPEND */ hint on the insert so that you are doing a bulk operation. Note that CREATE TABLE does this by default (as long as it's possible; you cannot perform bulk ops under certain conditions, e.g. if the new table is an index-organized table, has triggers, etc.
  • If you are on 10.2 or later, you should also consider using the LOG ERRORS INTO clause to log rejected records to an error table. That way, you won't lose the whole operation if one record has an error you didn't expect.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文