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

相关推荐
苹果醋320 分钟前
SpringBoot快速入门
java·运维·spring boot·mysql·nginx
ROCKY_81724 分钟前
Mysql复习(一)
数据库·mysql
夜光小兔纸25 分钟前
oracle dblink 的创建及使用
数据库·oracle
Smile丶凉轩39 分钟前
MySQL库的操作
数据库·mysql·oracle
CodeChampion1 小时前
60.基于SSM的个人网站的设计与实现(项目 + 论文)
java·vue.js·mysql·spring·elementui·node.js·mybatis
我叫啥都行1 小时前
计算机基础复习12.22
java·jvm·redis·后端·mysql
明矾java2 小时前
Mysql-SQL执行流程解析
数据库·sql·mysql
p@nd@2 小时前
Oracle筑基篇-调度算法-LRU的引入
数据库·oracle·操作系统·lru
wy02_3 小时前
MySQL-MVCC(多版本并发控制)
数据库·mysql
中科院提名者5 小时前
Django连接mysql数据库报错ModuleNotFoundError: No module named ‘MySQLdb‘
数据库·mysql·django