Oracle 迁移 Mysql

-- Oracle->MySQL

-- 使用时改一下where条件的owner和table_name

-- 字段数据类型映射时会将Oracle中的浮点NUMBER转换为decimal(65,8)定点数

-- 可以识别主键约束、非空约束,但无法识别外键约束、唯一约束、自定义check

-- 对于Oracle字符串长度为4000的,MySQL中类型会使用text,否则会使用varchar

SELECT

concat(concat(t2.owner,'.'),t2.table_name) src_table_name,

CASE

WHEN t1.c=1 THEN concat(concat(concat(concat('create table ',lower(t2.owner)),'.'),lower(t2.table_name)),'(')

WHEN t1.c=2 THEN

concat(

concat(

concat(lower(t2.column_name),' '),

concat(

concat(

CASE

WHEN t2.data_type IN ('VARCHAR','VARCHAR2','CHAR','NCHAR','NVARCHAR2') THEN (case when t2.data_length=4000 then 'text' else concat(concat(concat('varchar','('),t2.data_length),')') end)

WHEN regexp_replace(t2.data_type,'[(].+[)]','') IN ('TIMESTAMP','DATE') THEN 'datetime'

WHEN t2.data_type IN ('CLOB','NCLOB','LONG') THEN 'text'

WHEN t2.data_type IN ('BLOB','LONG RAW') THEN 'blob'

WHEN t2.data_type IN ('BINARY_FLOAT') THEN 'float'

WHEN t2.data_type IN ('BINARY_DOUBLE') THEN 'double'

WHEN t2.data_type IN ('FLOAT') THEN concat(concat('float(',CASE WHEN t2.data_precision>53 THEN 53 ELSE t2.data_precision END),')')

WHEN t2.data_type IN ('NUMBER') THEN

CASE WHEN t2.data_precision IS NOT NULL AND t2.data_scale IS NOT NULL THEN concat(concat(concat(concat('decimal(',t2.data_precision),','),t2.data_scale),')')

WHEN t2.data_precision IS NULL AND t2.data_scale IS NULL THEN 'decimal(65,8)'

end

end

,' '),

(CASE WHEN t2.nullable='N' THEN 'not null ' ELSE '' END)

)

),

concat(concat(concat('comment ''',REPLACE(t2.column_comments,'''','''''')),''''),(CASE WHEN t2.column_id=t2.max_column_id AND t2.key_column_name IS null THEN '' ELSE ',' end))

)

WHEN t1.c=4 THEN concat(concat(') comment ''',REPLACE(t2.table_comments,'''','''''')),concat('''',chr(59)))

WHEN t1.c=3 THEN concat(concat('primary key(',lower(t2.key_column_name)),')')

END sql_line,

CASE WHEN t1.c=1 THEN 0 WHEN t1.c=2 THEN t2.column_id WHEN t1.c=3 THEN t2.column_id+1 WHEN t1.c=4 THEN t2.column_id+2 WHEN t1.c=5 THEN t2.column_id+3 end ord,

t2.owner,

t2.table_name,

t2.table_comments,

CASE WHEN t1.c=2 THEN t2.data_type END data_type,

CASE WHEN t1.c=2 THEN t2.data_length END data_length,

CASE WHEN t1.c=2 THEN t2.data_precision END data_precision,

CASE WHEN t1.c=2 THEN t2.data_scale END data_scale

FROM

(SELECT 1 c FROM dual

UNION ALL

SELECT 2 c FROM dual

UNION ALL

SELECT 3 c FROM dual

UNION ALL

SELECT 4 c FROM dual

UNION ALL

SELECT 5 c FROM dual

) t1

JOIN (

SELECT t1.owner,

t1.table_name,

t1.comments table_comments,

t2.COLUMN_name column_name,

t2.data_type,

t3.comments column_comments,

t2.data_length,

t2.data_PRECISION,

t2.data_scale,

t2.column_id,

max(t2.column_id) over(PARTITION BY t1.owner,t2.table_name) max_column_id,

t4.key_column_name,

t2.nullable

FROM all_tab_comments t1

JOIN ALL_TAB_COLS t2 ON t2.owner=t1.owner AND t2.table_name=t1.table_name

JOIN all_col_comments t3 ON t3.owner=t1.owner AND t3.table_name=t1.table_name AND t3.column_name=t2.column_name

LEFT JOIN (SELECT t1.owner,t1.constraint_name,t1.table_name,to_char(wmsys.wm_concat(t2.column_name)) key_column_name FROM ALL_CONSTRAINTS t1

JOIN ALL_CONS_COLUMNS t2 ON t2.owner=t1.owner AND t2.constraint_name=t1.constraint_name AND t2.table_name=t1.table_name AND t1.constraint_type='P'

GROUP BY t1.owner,t1.constraint_name,t1.table_name) t4 ON t4.owner=t1.owner AND t4.table_name=t1.table_name

WHERE t1.owner IN ('') AND t1.table_name IN ('') -- 在此处限制owner和table_name

) t2 ON t1.c=1 AND t2.column_id=1 OR t1.c=2 OR t1.c=3 AND t2.key_column_name IS NOT NULL AND t2.column_id=t2.max_column_id OR t1.c=4 AND t2.column_id=t2.max_column_id OR t1.c=5 AND t2.column_id=t2.max_column_id

ORDER BY src_table_name,ord

相关推荐
远歌已逝2 小时前
维护在线重做日志(二)
数据库·oracle
i道i6 小时前
MySQL win安装 和 pymysql使用示例
数据库·mysql
Oak Zhang8 小时前
sharding-jdbc自定义分片算法,表对应关系存储在mysql中,缓存到redis或者本地
redis·mysql·缓存
久醉不在酒9 小时前
MySQL数据库运维及集群搭建
运维·数据库·mysql
WindFutrue9 小时前
使用Mybatis向Mysql中的插入Point类型的数据全方位解析
数据库·mysql·mybatis
LKID体10 小时前
Python操作neo4j库py2neo使用(一)
python·oracle·neo4j
一只爱撸猫的程序猿10 小时前
一个简单的Linux 服务器性能优化案例
linux·mysql·nginx
计算机毕设源码qq-383653104110 小时前
(附项目源码)Java开发语言,215 springboot 大学生爱心互助代购网站,计算机毕设程序开发+文案(LW+PPT)
java·开发语言·spring boot·mysql·课程设计
袁庭新11 小时前
Cannal实现MySQL主从同步环境搭建
java·数据库·mysql·计算机·java程序员·袁庭新
爱学习的白杨树11 小时前
MySQL中有哪几种锁?
数据库·mysql