最近公司的多个项目,客户要求使用国产数据库,如:vastbase、达梦、人大金仓等;国产数据库大多基于MySQL、PostgreSQL等定制开发发展而来,也有借鉴Oracle思想构建;毕竟Oracle在传统数据库领域是老大,影响力大,兼容它是基本选择。
公司产品是基于MySQL5.7和8.0.18版本开发,产品的DAO层是基于Mybatis开发。
上周2个项目,一个是老客户做产品升级,原来产品使用的是Oracle11g数据库,要求新产品兼容;另一个是新项目,客户要求使用国产数据库vastbase,中文名称叫海量数据库,要求产品兼容。
这个任务便落在了我肩上,经过一周的全面改造,提交测试版本,总体比较稳定。
现总结分享,希望能帮到有类似需要的您。
本文主要介绍MySQL8.0转Oracle 11g的分析及注意事项。
一、数据结构转换
1. 数据类型差异
MySQL 中的一些数据类型在 Oracle 对应可能并不完全一致。
- MySQL 的 INT 在 Oracle 中应转换为 NUMBER(8) 。
- MySQL的 BigInt 在 Oracle 中应转换为 NUMBER(20), 不要转换为Long , Long在DBA做数据迁移时不方便。
- MySQL的 decimal(10,2) 应转换为 Number(12,2),这里有个精度计算的差异。
- 对于日期时间类型,MySQL 的 DATETIME 在 Oracle 中可能需要使用 TIMESTAMP ,多说一句,在 PostgreSQL 中则可以是 TIMESTAMP WITHOUT TIME ZONE 。
2. 自增主键处理
MySQL 通常使用 AUTO_INCREMENT 来实现自增主键。而 Oracle 中常见的是通过创建序列(SEQUENCE)和触发器来实现自增。在 PostgreSQL 中,可以使用 SERIAL 或 IDENTITY 来达到类似效果。
3. 字符串长度限制
Oracle有规定,所有的表名和字段名长度不能超过30位,这个要重点注意。
MySQL 对于 VARCHAR 类型的长度定义较为宽松,而 Oracle 和 PostgreSQL 对字符串长度的处理可能更为严格,需要根据实际情况调整字段长度的定义。
4.关键字处理
在MySQL中,level可以使用。
但在oracle中应该避免,关键字level等不能作为字段或别名出现在SQL语句中,否则,会提示语法错误!
如果需要特殊处理,关键字必须大写,且用双引号包围。如:
select t."LEVEL" from student t;
另外,像type、name、value虽然是oracle的关键字,但可以在SQL中使用,判别方法如下:
select t.* from v$reserved_words t;
v$reserved_words是Oracle的系统表,里面存储了Oracle所有的关键字,共6列;
reserved, res_type,res_attr,res_semi等表示是否为关键字,详见下表,如果有一个为Y,则代表不建议做为数据库表的列名。
KEYWORD | Name of the keyword |
LENGTH | Length of the keyword |
RESERVED | A value of Y means that the keyword cannot be used as an identifier. A value of N means that it is not reserved. |
RES_TYPE | A value of Y means that the keyword cannot be used as a type name. A value of N means that it is not reserved. |
RES_ATTR | A value of Y means that the keyword cannot be used as an attribute name. A value of N means that it is not reserved. |
RES_SEMI | A value of Y means that the keyword is not allowed as an identifier in certain situations, such as in DML. A value of N means that it is not reserved. |
DUPLICATE | A value of Y means that the keyword is a duplicate of another keyword. A value of N means that it is not a duplicate. |
二、语法差异
1. 函数
除标准函数外,MySQL特有的函数需要处理:
- group_concat()
在Oracle需要替换为wm_concat(), 替换的方式很多,这里推荐一种成本最低的;
使用MyBatis的DatabaseIdProvider.
首先,声明一个DatabaseIdProvider的Bean,便于MyBatis实现了VendorDatabaseIdProvider。在各厂商实现的数据库驱动中包含了数据库产品名,但大小写比较复杂难记,如:PostgreSQL,这里使用一个Properties全部转换为小写,这样在程序中方便编写。
@Bean
public DatabaseIdProvider getDatabaseIdProvider() {
DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
Properties properties = new Properties();
properties.setProperty("Oracle", "oracle");
properties.setProperty("MySQL", "mysql");
properties.setProperty("DB2", "db2");
properties.setProperty("Derby", "derby");
properties.setProperty("H2", "h2");
properties.setProperty("HSQL", "hsql");
properties.setProperty("Informix", "informix");
properties.setProperty("MS-SQL", "ms-sql");
properties.setProperty("PostgreSQL", "postgresql");
properties.setProperty("Sybase", "sybase");
properties.setProperty("Hana", "hana");
databaseIdProvider.setProperties(properties);
return databaseIdProvider;
}
其次,利用Mybatis的choose, when, otherwise 实现类似if-else的功能。如:
t1.affiliation_id,
t1.affiliation_code,
t1.affiliation_name,
t1.belong_to,
<choose>
<when test="_databaseId == 'oracle'.toString()">
WM_CONCAT(distinct t2.department_id) as departmentId,
WM_CONCAT(distinct t2.department_code) as departmentCode,
WM_CONCAT(distinct t2.department_name) as departmentName,
</when>
<otherwise>
GROUP_CONCAT( DISTINCT t2.department_id ) AS departmentId,
GROUP_CONCAT( DISTINCT t2.department_code ) AS departmentCode,
GROUP_CONCAT( DISTINCT t2.department_name ) AS departmentName,
</otherwise>
</choose>
t1.work_status,
t1.fault_time,
t1.fault_code,
这样就可以清晰的兼容多种数据库了。
_databaseId是MyBatis的内置变量,在DynamicContext类中,如下:
public class DynamicContext {
public static final String PARAMETER_OBJECT_KEY = "_parameter";
public static final String DATABASE_ID_KEY = "_databaseId";
static {
OgnlRuntime.setPropertyAccessor(ContextMap.class, new ContextAccessor());
}
private final ContextMap bindings;
private final StringJoiner sqlBuilder = new StringJoiner(" ");
private int uniqueNumber = 0;
public DynamicContext(Configuration configuration, Object parameterObject) {
if (parameterObject != null && !(parameterObject instanceof Map)) {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
bindings = new ContextMap(metaObject);
} else {
bindings = new ContextMap(null);
}
bindings.put(PARAMETER_OBJECT_KEY, parameterObject);
bindings.put(DATABASE_ID_KEY, configuration.getDatabaseId());
}
- concat()
在MySQL中,concat(...)中可以有多个参数,而Oracle中只能有2个参数;
有人用Oracle的写法兼容MySQL, 也是可以的,但是不推荐。如:
concat(x,concat(y,z))
如果是模糊查询,推荐使用MyBatis的<Bind/>标签,这样与数据库类型无关了。如:
<if test="dto.stationCode != null and dto.stationCode != ''">
<bind name="stationCodeLike" value="'%'+dto.stationCode+'%'" />
and t1.station_code like #{stationCodeLike}
</if>
2.group by
在MySQL中,group by 语法比较宽松,后面跟想要分组的字段即可;而Oracle必须是全量的Select字段,建议用Oracle语法兼容MySQL.
3. 常量
在MySQL中,Select后面常量可以用双引号或单引号,效果一样,如:
select "Y" as Flag , '1' as Num from dual;
但在Oracle中,Select后面常量必须用单引号包围。
这里兼容都用单引号。
4 日期比较
在MySQL中,日期比较是比较宽松的;字段类型是日期,比较时用字符串是可以的。
如:
Select * from student where created_date > '2024-06-30';
但在Oracle中,字段类型是日期,比较时用字符串是不可以的,需要转换。
Select t.* from student t where t.created_date > to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mm:ss') ;
三、总结
在产品开发时,做好代码走查,把握好各个数据库的特点;
这里还需要强调下Oracle、 PostgreSQL 和MySQL都有数据库,模式schema,数据表的概念,但是他们有较大的差异,在产品做数据库迁移时,需要考虑好这3者的对应关系。否则,在跨库查询时,会额外增添许多工作量。
个人感觉做Web应用开发,PostgreSQL越来越贴心了。
我是一个有20年开发经验的老鸟,欢迎关注,后面我们分享MySQL转Vastbase的经验。
也正在考虑写一个开源工具,目标是数据结构转换,每次转换总是困扰我,花费大量的时间,例如MySQL转Oracle, 索引没转过去,字段默认值没转过去,数据量大的时候,迁移特别慢等。但是又不想使用大数据工具。