-- 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