数据库迁移[英] Database Migration

本文是小编为大家收集整理的关于数据库迁移的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在Java中使用数据库迁移工具.该工具是将带有数据的数据库表复制到目标数据库.但是我希望它能在不同的数据库上工作.从MySQL复制并在Derby等中创建.使用JDBC,我们可以收集有关该表及其列的足够信息.但是我要问这个问题,我是否可以免费在Java上重新创建桌子.我的意思是不同的数据库具有不同的数据类型,有时它们在SQL语法上有所不同.那么JDBC或任何其他库(可以是开源)可以以简单而全局的方式进行此作业?

推荐答案

apache的 ddlutils 已经完成了我需要的事情.当我搜索CrossDB时,它发现了它,并且非常有用但功能强大.它可以通过参数从头开始生成数据库.或者它可以获取现有数据库表定义,也可以使用索引定义.如果需要,可以使用定界符(对于我来说,使用Apache Derby是致命的重要选择).您可以只打印这些定义或将它们直接应用于源数据库(我还没有尝试过第二个定义).它翻译了所选数据库的定义.但是一个大问题是,如何开始使用它没有好的教程.我浏览了包装,找到一个好的起点.这是我所取得的目标,是生成完整数据库表的示例代码创建SQL.

DerbyPlatform dp = new DerbyPlatform();
dp.setDelimitedIdentifierModeOn(true);
Database dbs = new Database();
DerbyModelReader dmr = new DerbyModelReader(dp);
Database test = dmr.getDatabase(conn, "MyDBTest");

DerbyBuilder db = new DerbyBuilder(dp);
String testSqlDerby = dp.getCreateTablesSql(test, true, true);
System.out.println(testSqlDerby);

System.out.println("\n\n\n\n");

MySql50Platform mp = new MySql50Platform();
mp.setDelimitedIdentifierModeOn(true);
MySqlBuilder mb = new MySqlBuilder(mp);
String testSqlMysql = mp.getCreateTablesSql(test, true, true);
System.out.println(testSqlMysql);

其他推荐答案

我不知道JDBC有一个通用的设施来做到这一点.您可能必须制作一个生成用于表创建的SQL的实用程序库.

从执行ANSI SQL并在您打算支持的尽可能多的平台上进行测试.请记住,Java仍然是写入的,到处调试,因此您需要在打算支持该系统的任何平台上进行测试.如果您必须为任何平台进行创建语句的辩证法变化,则子类.

其他推荐答案

尝试也是开源的Schemacrawler,并用Java编写.它提供对数据库元数据的程序化访问,还允许在JavaScript中脚本脚本. http://schemacrawler.sourceforge.net/

本文地址:https://www.itbaoku.cn/post/597683.html

问题描述

I am working on database migration tool in java. The tool is copying database tables with their data's to the destination database. But I want it to work on different databases. Copy from mysql and create in derby etc. With JDBC, we can gather enough information about the table and its columns. But I am going to ask this, if I can recreate tables on java with sql free. I mean different databases have different data types and some times they differs at sql syntax. So can JDBC or any other library (can be open source) do this job at an easy and global way?

推荐答案

Apache's DdlUtils is done what I need. When I am searching about crossdb found it, and it is very useful yet powerful. It can generate a database from scratch, just with the parameters. Or it can grab existing database table definitions, also with index definitions. You can use delimiter if you want (it is a deadly important option for me to use Apache Derby). You can just print out these definitions or apply them directly to source database (I haven't tried the second one yet). It translates definitions for the selected database. But one big problem is there is no good tutorial about how to start using it. I searched through the packages to find a good place to start. Here is what i have achieved, a sample code to generate full database table create sql.

DerbyPlatform dp = new DerbyPlatform();
dp.setDelimitedIdentifierModeOn(true);
Database dbs = new Database();
DerbyModelReader dmr = new DerbyModelReader(dp);
Database test = dmr.getDatabase(conn, "MyDBTest");

DerbyBuilder db = new DerbyBuilder(dp);
String testSqlDerby = dp.getCreateTablesSql(test, true, true);
System.out.println(testSqlDerby);

System.out.println("\n\n\n\n");

MySql50Platform mp = new MySql50Platform();
mp.setDelimitedIdentifierModeOn(true);
MySqlBuilder mb = new MySqlBuilder(mp);
String testSqlMysql = mp.getCreateTablesSql(test, true, true);
System.out.println(testSqlMysql);

其他推荐答案

I'm not aware of JDBC having a generic facility to do this. You probably have to make a utility library that generates the SQL for table creation.

Start with one that does ANSI SQL and test it on as many platforms as you intend to support. Remember, Java is still write-once, debug everywhere so you will need to test this on any platform you intend to support the system on. Subclass the generator if you have to make dialectic variations of the create statement for any of your platforms.

其他推荐答案

Try SchemaCrawler, which is also open source, and written in Java. It provides programmatic access to database metadata, and also allows scripting in JavaScript. http://schemacrawler.sourceforge.net/