[oracle] oracle 테이블에 따라 mysql 테이블 구성 문장을 저장하는 과정

6748 단어 oraclemysql
oracle 테이블에서 mysql 테이블 구성 문장을 만드는 과정은 다음과 같습니다.
일부 부분은 맞춤형 수요이기 때문에 실제 사용 장면에 따라 미세하게 조정해야 한다.
create or replace procedure p_wjf_ora_to_mysql(i_tablename in varchar2,
                                               i_tab_owner in varchar2) as
  --declare 
  ----datatype cursor 
  cursor c_v1 is
    select a.COLUMN_NAME,
           a.DATA_TYPE,
           a.DATA_LENGTH,
           a.NULLABLE,
           a.DATA_DEFAULT,
           a.DATA_PRECISION,
           a.DATA_SCALE,
           a.COLUMN_ID
      from dba_tab_columns a
     where table_name = upper(i_tablename)
       and owner = upper(i_tab_owner)
     order by COLUMN_ID;
  v_row c_v1%rowtype;

  -----index cursor
  cursor c_v2 is
    select a.INDEX_NAME, b.CONSTRAINT_TYPE, a.owner
      from dba_indexes a, DBA_CONSTRAINTS b
     where a.TABLE_OWNER = upper(i_tab_owner)
       and a.TABLE_NAME = upper(i_tablename)
       and a.index_name = b.INDEX_NAME(+)
       and a.owner = b.INDEX_OWNER(+);
  v_row_ind c_v2%rowtype;

  v_tab_owner varchar2(30);
  v_tablename varchar2(30);
  ----  comment  
  v_col_comm varchar2(4000);
  ----       
  v_col_null varchar2(4000);
  -----      
  v_col_def varchar2(4000);
  -----     
  v_my_col varchar2(4000);
  -----      
  v_my_key varchar2(4000);
  -----        
  v_pri_count number;
  ------     
  v_pri_col varchar2(30);
  -------      
  v_part_info varchar2(30);
  -------     
  v_ind_col varchar2(300);
  ------       
  v_is_tab_exist number;
  -----
  v_tab_comm varchar2(4000);
  ----
  v_ind_con number;
  ----      
  i number;

begin
  ------       
  select upper(i_tablename), upper(i_tab_owner)
    into v_tablename, v_tab_owner
    from dual;

  -----       
  select count(*)
    into v_is_tab_exist
    from dba_tables
   where table_name = v_tablename
     and owner = v_tab_owner;
  if v_is_tab_exist <> 1 then
    dbms_output.put_line('    ,   ,     ' || v_is_tab_exist);
    return;
  end if;
  ------primary
  select nvl(count(b.COLUMN_NAME), 0)
    into v_pri_count
    from DBA_CONSTRAINTS a, DBA_CONS_COLUMNS b
   where a.TABLE_NAME = v_tablename
     and a.OWNER = v_tab_owner
     and CONSTRAINT_TYPE = 'P'
     and a.CONSTRAINT_NAME = b.CONSTRAINT_NAME(+)
     and a.owner = b.owner(+);
  if v_pri_count = 1 then
    select b.COLUMN_NAME
      into v_pri_col
      from DBA_CONSTRAINTS a, DBA_CONS_COLUMNS b
     where a.TABLE_NAME = v_tablename
       and a.OWNER = v_tab_owner
       and CONSTRAINT_TYPE = 'P'
       and a.CONSTRAINT_NAME = b.CONSTRAINT_NAME(+)
       and a.owner = b.owner(+);
  else
    dbms_output.put_line('err:    ' || v_pri_count);
  end if;

  -----part
  select PARTITIONED
    into v_part_info
    from dba_tables
   where TABLE_NAME = v_tablename
     and OWNER = v_tab_owner;
  if v_part_info = 'YES' then
    dbms_output.put_line('err:     ');
  end if;

  open c_v1;
  dbms_output.put_line('create table ' || v_tablename || '(');
  loop
    fetch c_v1
      into v_row;
    exit when c_v1%notfound;
    select ' COMMENT ''' || comments || ''''
      into v_col_comm
      from DBA_COL_COMMENTS
     where table_name = v_tablename
       and owner = v_tab_owner
       and column_name = v_row.COLUMN_NAME;
    ---not null
    if v_row.NULLABLE = 'N' then
      v_col_null := ' not null ';
    else
      v_col_null := '';
    end if;
  
    ----default
    if v_row.DATA_DEFAULT is null then
      v_col_def := '';
    elsif upper(v_row.DATA_DEFAULT) = 'SYSDATE' then
      v_col_def := ' default CURRENT_TIMESTAMP ';
    else
      v_col_def := ' default ' || v_row.DATA_DEFAULT;
    end if;
    ----datatype
    if v_row.data_type = 'VARCHAR' or v_row.data_type = 'VARCHAR2' then
      v_my_col := v_row.COLUMN_NAME || ' VARCHAR(' || v_row.DATA_LENGTH || ')' ||
                  v_col_def || v_col_null || v_col_comm || ',';
    elsif v_row.data_type = 'DATE' then
      v_my_col := v_row.COLUMN_NAME || ' DATETIME' || v_col_def ||
                  v_col_null || v_col_comm || ',';
    elsif v_row.data_type = 'NUMBER' and
          (v_row.DATA_PRECISION is not null or v_row.DATA_SCALE is not null) then
      v_my_col := v_row.COLUMN_NAME || ' DECIMAL(' || v_row.DATA_PRECISION || ',' ||
                  v_row.DATA_SCALE || ')' || v_col_def || v_col_null ||
                  v_col_comm || ',';
    elsif v_row.data_type = 'NUMBER' and v_row.DATA_PRECISION is null and
          v_row.DATA_SCALE is null then
      v_my_col := v_row.COLUMN_NAME || ' BIGINT(20)' || v_col_def ||
                  v_col_null || v_col_comm || ',';
    else
      v_my_col := v_row.data_type || ',' || v_row.DATA_PRECISION || ',' ||
                  v_row.DATA_SCALE || v_col_def || v_col_null || v_col_comm || ',';
    end if;
    dbms_output.put_line(v_my_col);
  end loop;
  close c_v1;
  -----index
  select count(*)
    into v_ind_con
    from dba_indexes
   where table_name = v_tablename
     and owner = v_tab_owner;
  i := 1;
  open c_v2;
  loop
    fetch c_v2
      into v_row_ind;
    exit when c_v2%notfound;
    select max(idx_col)
      into v_ind_col
      from (select dbms_lob.substr(wm_concat(COLUMN_NAME)
                                   over(partition by index_name order by
                                        COLUMN_POSITION),
                                   1000) idx_col
              from DBA_IND_COLUMNS
             where index_owner = v_row_ind.owner
               and index_name = v_row_ind.index_name);
  
    if v_row_ind.CONSTRAINT_TYPE = 'P' then
      v_my_key := ' primary key ' || v_row_ind.INDEX_NAME || ' (' ||
                  v_ind_col || ')';
    elsif v_row_ind.CONSTRAINT_TYPE = 'U' then
      v_my_key := ' unique key ' || v_row_ind.INDEX_NAME || ' (' ||
                  v_ind_col || ')';
    else
      v_my_key := ' key ' || v_row_ind.INDEX_NAME || ' (' || v_ind_col || ')';
    end if;
    if i < v_ind_con then
      v_my_key := v_my_key || ',';
    end if;
    dbms_output.put_line(v_my_key);
    i := i + 1;
  end loop;
  close c_v2;
  -----tab comment
  select COMMENTS
    into v_tab_comm
    from DBA_TAB_COMMENTS
   where TABLE_NAME = v_tablename
     and owner = v_tab_owner;
  dbms_output.put_line(') engine=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC comment=''' ||
                       v_tab_comm || ''';');
end;

좋은 웹페이지 즐겨찾기