ORACLE表生成MYSQL建表DDL

23 篇文章 0 订阅

ORACLE表生成MYSQL建表DDL

有同事问有没有ORACLE表转MYSQL语法的小工具,于是写了这个小函数,公开出来,也许有人用得上。

 

create or replace function fnc_table_to_mysql
( i_owner                       in string,
  i_table_name                  in string,
  i_number_default_type         in string := 'decimal',
  i_auto_incretment_column_name in string := '%ID'
)
/*
  功能:ORACLE表生成MYSQL建表DDL
  作者:叶正盛 2013-07-27
  新浪微博:@yzsind-叶正盛
  参数说明:
  i_owner:schema名
  i_table_name:表名
  i_number_default_type:NUMBER默认转换的类型,缺省是decimal
  i_auto_incretment_column_name:自增属性字段名称规则,默认是%ID
  
  已知问题:
  1.不支持分区
  2.不支持函数索引,位图索引等特殊索引定义
  3.不支持自定义数据类型,不支持ROWID,RAW等特殊数据类型
  4.不支持外键
  5.不支持自定义约束
  6.不支持与空间、事务相关属性
  7.DATE与TIMESTAMP转换成datetime,需注意精度
  8.超大NUMBER直接转换为bigint,需注意精度
  9.auto incretment 是根据字段名规则加一些判断,设置不一定准确,需检查
  */
 return clob is
  Result         clob;
  cnt            number;
  data_type      varchar2(128);
  column_str     varchar2(4000);
  pk_str         varchar2(4000);
  table_comments varchar2(4000);
  is_pk_column   number := 0;
begin
  select count(*)
    into cnt
    from all_tables
   where owner = i_owner
     and table_name = i_table_name;
  if (cnt = 0) then
    RAISE_APPLICATION_ERROR(-20000,'can''t found table,please check input!');
  else
    Result := 'CREATE TABLE `' || lower(i_table_name) || '`(';
    --column
    for c in (select a.column_name,
                     a.data_type,
                     a.data_length,
                     a.data_precision,
                     a.data_scale,
                     a.nullable,
                     a.data_default,
                     b.COMMENTS
                from all_tab_cols a, all_col_comments b
               where a.owner = i_owner
                 and a.table_name = i_table_name
                 and a.HIDDEN_COLUMN = 'NO'
                 and a.owner = b.OWNER
                 and a.TABLE_NAME = b.TABLE_NAME
                 and a.COLUMN_NAME = b.COLUMN_NAME
               order by a.column_id) loop
      if (c.data_type = 'VARCHAR2' or c.data_type = 'NVARCHAR2') then
        data_type := 'varchar(' || c.data_length || ')';
      elsif (c.data_type = 'CHAR' or c.data_type = 'NCHAR') then
        data_type := 'char(' || c.data_length || ')';
      elsif (c.data_type = 'NUMBER') then
        if (c.column_name like '%ID' and c.data_scale is null) then
          data_type := 'bigint';
        elsif (c.data_precision<3 and c.data_scale = 0) then
          data_type := 'tinyint';
        elsif (c.data_precision<5 and c.data_scale = 0) then
          data_type := 'smallint';
        elsif (c.data_precision<10 and c.data_scale = 0) then
          data_type := 'int';
        elsif (c.data_precision is not null and c.data_scale = 0) then
          data_type := 'bigint';
        elsif (c.data_precision is not null and c.data_scale is not null) then
          data_type := 'decimal(' || c.data_precision || ',' ||
                       c.data_scale || ')';
        else
          data_type := i_number_default_type;
        end if;
      elsif (c.data_type = 'DATE' or c.data_type like 'TIMESTAMP%') then
        data_type := 'datetime';
      elsif (c.data_type = 'CLOB' or c.data_type = 'NCLOB' or
            c.data_type = 'LONG') then
        data_type := 'text';
      elsif (c.data_type = 'BLOB' or c.data_type = 'LONG RAW') then
        data_type := 'blob';
      elsif (c.data_type = 'BINARY_FLOAT') then
        data_type := 'float';
      elsif (c.data_type = 'BINARY_DOUBLE') then
        data_type := 'double';
      else
        data_type := c.data_type;
      end if;
      column_str := '  `' || lower(c.column_name) || '` ' || data_type;
      if (c.column_name like i_auto_incretment_column_name and
         (c.data_scale is null or c.data_scale = 0)) then
        select count(*)
          into is_pk_column
          from all_constraints a, all_cons_columns b
         where a.owner = i_owner
           and a.table_name = i_table_name
           and a.constraint_type = 'P'
           and a.OWNER = b.OWNER
           and a.TABLE_NAME = b.TABLE_NAME
           and a.CONSTRAINT_NAME = b.CONSTRAINT_NAME
           and b.COLUMN_NAME = c.column_name;
        if is_pk_column > 0 then
          column_str := column_str || ' AUTO_INCREMENT';
        end if;
      end if;
      if c.nullable = 'NO' then
        column_str := column_str || ' NOT NULL';
      end if;
      if (trim(c.data_default) is not null) then
        column_str := column_str || ' DEFAULT ' ||
                      trim(replace(replace(c.data_default, chr(13), ''),
                                   chr(10),
                                   ''));
      end if;
      if c.comments is not null then
        column_str := column_str || ' COMMENT ''' || c.comments || '''';
      end if;
      Result := Result || chr(10) || column_str || ',';
    end loop;
    --pk
    for c in (select a.constraint_name, wm_concat(a.column_name) pk_columns
                from (select a.CONSTRAINT_NAME,
                             '`' || b.COLUMN_NAME || '`' column_name
                        from all_constraints a, all_cons_columns b
                       where a.owner = i_owner
                         and a.table_name = i_table_name
                         and a.constraint_type = 'P'
                         and a.OWNER = b.OWNER
                         and a.TABLE_NAME = b.TABLE_NAME
                         and a.CONSTRAINT_NAME = b.CONSTRAINT_NAME
                       order by b.POSITION) a
               group by a.constraint_name) loop
      Result := Result || chr(10) || '  PRIMARY KEY (' ||
                lower(c.pk_columns) || '),';
    end loop;
    --unique
    for c in (select a.constraint_name, wm_concat(a.column_name) uk_columns
                from (select a.CONSTRAINT_NAME,
                             '`' || b.COLUMN_NAME || '`' column_name
                        from all_constraints a, all_cons_columns b
                       where a.owner = i_owner
                         and a.table_name = i_table_name
                         and a.constraint_type = 'U'
                         and a.OWNER = b.OWNER
                         and a.TABLE_NAME = b.TABLE_NAME
                         and a.CONSTRAINT_NAME = b.CONSTRAINT_NAME
                       order by b.POSITION) a
               group by a.constraint_name) loop
      Result := Result || chr(10) || '  UNIQUE KEY `' ||
                lower(c.constraint_name) || '`(' || lower(c.uk_columns) || '),';
    end loop;
    -- index
    for c in (select a.index_name, wm_concat(a.column_name) ind_columns
                from (select a.index_name,
                             '`' || a.COLUMN_NAME || '`' column_name
                        from all_ind_columns a
                       where a.table_owner = i_owner
                         and a.TABLE_NAME = i_table_name
                         and not exists
                       (select index_name
                                from all_constraints b
                               where a.TABLE_OWNER = b.owner
                                 and a.TABLE_NAME = b.TABLE_NAME
                                 and a.INDEX_NAME = b.INDEX_NAME)
                       order by a.COLUMN_POSITION) a
               group by a.index_name) loop
      Result := Result || chr(10) || '  KEY `' || lower(c.index_name) || '`(' ||
                lower(c.ind_columns) || '),';
    end loop;
    Result := substr(Result, 1, length(result) - 1) || chr(10) || ')';
    --table comments
    select max(a.COMMENTS)
      into table_comments
      from all_tab_comments a
     where owner = i_owner
       and table_name = i_table_name;
    if (table_comments is not null) then
      Result := Result || 'COMMENT=''' || table_comments || '''';
    end if;
    Result := Result || ';';
  end if;
  return(Result);
end fnc_table_to_mysql;
/


源码新浪微盘下载地址:http://vdisk.weibo.com/s/Eco-DT0ZVUa

 





  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
生成 Oracle 建表 DDL 的步骤如下: 1. 创建数据源并连接到数据库。 2. 获取数据库元数据信息,包括名、列名、数据类型、长度、精度等。 3. 根据元数据信息,生成建表语句。 4. 将建表语句输出到文件或者直接执行。 下面是一个示例代码,可以根据该代码生成 Oracle 建表 DDL: ```java import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class OracleDDLGenerator { public static void main(String[] args) { String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String username = "username"; String password = "password"; String schema = "schema_name"; Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection(url, username, password); DatabaseMetaData meta = conn.getMetaData(); ResultSet tables = meta.getTables(null, schema, null, new String[] { "TABLE" }); while (tables.next()) { String tableName = tables.getString("TABLE_NAME"); System.out.println("Table: " + tableName); ResultSet columns = meta.getColumns(null, schema, tableName, null); StringBuilder sb = new StringBuilder(); sb.append("CREATE TABLE ").append(tableName).append(" ("); while (columns.next()) { String columnName = columns.getString("COLUMN_NAME"); String typeName = columns.getString("TYPE_NAME"); int columnSize = columns.getInt("COLUMN_SIZE"); int decimalDigits = columns.getInt("DECIMAL_DIGITS"); sb.append(columnName).append(" ").append(typeName).append("(").append(columnSize); if (decimalDigits > 0) { sb.append(",").append(decimalDigits); } sb.append("),"); } sb.deleteCharAt(sb.length() - 1); sb.append(");"); System.out.println(sb.toString()); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } ``` 其中,`url`、`username`、`password` 分别是数据库连接的 URL、用户名、密码,`schema` 是数据库的 schema 名称。可以根据实际情况修改这些参数。 该程序会获取数据库中所有的元数据信息,并输出对应的建表语句。可以将输出结果保存到文件中,或者直接执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值