抓出Oralce当前账户下所有表建表语句,迁移到MySQL

有时候需要导出当前Oracle账户下所有的表结构信息,在其他Oracle测试库重建,或者迁移到MySQL数据库中
虽然可以用工具,但是本人还是习惯自己动手

本脚本会将分区表处理为非分区表,如需添加分区信息,自己改脚本
本脚本只支持number,char,varchar2,date,timestamp数据类型
如需支持更多数据类型,自己修改脚本
如需将Oracle数据类型转换为MySQL数据类型,请看后面

set pages 10000
set feedback off
col table_ddl_in_current_user format a150
with tab_column as
 (select table_name,
         column_name,
         column_id,
         data_type,
         data_length,
         data_precision,
         data_scale,
         nullable
    from user_tab_columns),
tab as
 (select table_name from user_tables)
select case
         when column_id = 0 then
          'create table ' || lower(table_name)
       end || case
         when column_id = 1 and max_colid = 1 then
          '(' || trim(column_name)
         when column_id = 1 and max_colid <> 1 then
          '(' || trim(column_name) || ','
         when column_id > 1 and column_id < max_colid then
          column_name || ','
         when column_id = max_colid then
          column_name
       end || case
         when column_id = 999 then
          ');'
       end as table_ddl_in_current_user
  from (select table_name,
               column_id,
               max(column_id) over(partition by table_name) max_colid,
               case
                 when data_type in ('DATE', 'TIMESTAMP(6)') then
                  ' ' || lower(column_name) || ' ' || lower(data_type)
                 when data_type in ('CHAR', 'VARCHAR2') then
                  ' ' || lower(column_name) || ' ' || lower(data_type) || '(' ||
                  data_length || ')'
                 when data_type = 'NUMBER' and data_precision is not null and
                      data_scale = 0 then
                  ' ' || lower(column_name) || ' ' || lower(data_type) || '(' ||
                  data_precision || ')'
                 when data_type = 'NUMBER' and data_precision is not null and
                      data_scale > 0 then
                  ' ' || lower(column_name) || ' ' || lower(data_type) || '(' ||
                  data_precision || ',' || data_scale || ')'
                 when data_type = 'NUMBER' and data_precision is null and
                      data_scale is null then
                  ' ' || lower(column_name) || ' ' || lower(data_type)
               end || case
                 when nullable = 'N' then
                  ' not null'
               end as column_name
          from tab_column
         where table_name in (select table_name from tab)
        union all
        select table_name,
               -1         as column_id,
               null       as max_colid,
               null       as column_name
          from tab
        union all
        select table_name,
               0          as column_id,
               null       as max_colid,
               null       as column_name
          from tab
        union all
        select table_name,
               999        as column_id,
               null       as max_colid,
               null       as column_name
          from tab
         order by table_name, column_id);

抓出当前账户下主键,外键,索引,视图,check约束,触发器,函数,存储过程等我就不贴了
想精通SQL优化?精通SQL? 精通系统优化?精通表设计?精通分库分表? 加我微信 692162374 报个名学习吧

将Oracle建表语句转为MySQL建表语句

with tab_column
 as
 (select table_name,
         column_name,
         column_id,
         data_type,
         data_length,
         data_precision,
         data_scale,
         nullable
    from user_tab_columns ),
tab as
 (select table_name from user_tables)
select case
         when column_id = 0 then
          'create table ' || lower(table_name)
       end || case
         when column_id = 1 and max_colid = 1 then
          '(' || trim(column_name)
         when column_id = 1 and max_colid <> 1 then
          '(' || trim(column_name) || ','
         when column_id > 1 and column_id < max_colid then
          column_name || ','
         when column_id = max_colid then
          column_name
       end || case
         when column_id = 999 then
          ');'
       end as table_ddl_in_current_user
  from (select table_name,
               column_id,
               max(column_id) over(partition by table_name) max_colid,
               case
                 when data_type in ('DATE', 'TIMESTAMP(6)') then
                  ' ' || lower(column_name) || ' ' || 'datetime'
                 when data_type in ('VARCHAR2') then
                  ' ' || lower(column_name) || ' ' || 'varchar' || '(' ||
                  data_length || ')'
                  when data_type in ('CHAR') then
                  ' ' || lower(column_name) || ' ' || 'varchar' || '(' ||
                  data_length || ')' 
                 when data_type = 'NUMBER' and data_precision is not null and
                      data_scale = 0 then
                  ' ' || lower(column_name) || ' ' || 'int' || '(' ||
                  data_precision || ')'
                 when data_type = 'NUMBER' and data_precision is not null and
                      data_scale > 0 then
                  ' ' || lower(column_name) || ' ' || 'decimal' || '(' ||
                  data_precision || ',' || data_scale || ')'
                 when data_type = 'NUMBER' and data_precision is null and
                      data_scale is null then
                  ' ' || lower(column_name) || ' ' || 'int'
               end || case
                 when nullable = 'N' then
                  ' not null'
               end as column_name
          from tab_column
         where table_name in (select table_name from tab)
        union all
        select table_name,
               -1         as column_id,
               null       as max_colid,
               null       as column_name
          from tab
        union all
        select table_name,
               0          as column_id,
               null       as max_colid,
               null       as column_name
          from tab
        union all
        select table_name,
               999        as column_id,
               null       as max_colid,
               null       as column_name
          from tab
         order by table_name, column_id);

猜你喜欢

转载自blog.csdn.net/robinson1988/article/details/105827801