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

相关推荐
运维开发那些事7 分钟前
Mysql MIC高可用集群搭建
mysql
GoingYoo1 小时前
MySQL原理:逻辑架构
数据库·sql·mysql
半点闲1 小时前
解决Oracle PL/SQL中“表或视图不存在“错误的完整指南
数据库·sql·oracle·pl/sql
手握风云-2 小时前
MySQL数据库精研之旅第五期:CRUD的趣味探索(中)
数据库·mysql
快来卷java10 小时前
MySQL篇(一):慢查询定位及索引、B树相关知识详解
java·数据结构·b树·mysql·adb
Pasregret11 小时前
04-深入解析 Spring 事务管理原理及源码
java·数据库·后端·spring·oracle
jnrjian11 小时前
归档重做日志archived log (明显) 比redo log重做日志文件小
数据库·oracle
TDengine (老段)12 小时前
TDengine 中的命名与边界
大数据·数据库·物联网·oracle·时序数据库·tdengine·iotdb
谁家有个大人12 小时前
MYSQL中对行与列的操作
数据库·mysql
dogplays14 小时前
dbeaver无法连接Oracle报错:ORA-17800、ORA-12514
数据库·oracle