欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Oracle数据库表结构转MySQL表结构

程序员文章站 2022-03-15 19:38:20
...

此函数来自部门大佬杰,使用如下
需要将Oracle数据库中的表迁移到MySQL时,需要整理MySQL建表语句,手动整理比较麻烦,尤其是整理列备注语句。此时可以使用下面的函数自动生成MySQL版本语句。

参数:

1、需要转换的表名;
2、是否包含“已存在则删除语句”(DROP TABLE IF EXISTS),默认包含

使用方法:

`SELECT F_LIMS_GET_SQL_FOR_MYSQL('T_LIMS_EQUIPMENT') FROM DUAL;

SELECT F_LIMS_GET_SQL_FOR_MYSQL('T_LIMS_EQUIPMENT',0) FROM DUAL;`

将查询结果拷贝出,在MySQL库里执行即可。

函数如下:


```sql
CREATE OR REPLACE FUNCTION F_LIMS_GET_SQL_FOR_MYSQL(PI_TABLENAME IN VARCHAR2,
                                                    PI_ISDROP    IN INTEGER := 1)
  RETURN CLOB IS
  V_TABLENAME         VARCHAR(100);
  V_RET_SQL           VARCHAR2(4000);
  V_SQL_HEADER        VARCHAR2(1000);
  V_SQL_BODY          CLOB;
  V_SQL_INDEX         VARCHAR2(4000);
  V_SQL_TABLE_COMMENT VARCHAR(400);
  V_SQL_PK_COL_LIST   VARCHAR(1000);
  -- 根据当前数据库和表名,生成mysql建表语句
BEGIN
  -- 表名统一为大写
  SELECT UPPER(PI_TABLENAME) INTO V_TABLENAME FROM DUAL;

  SELECT MAX(T.COMMENTS)
    INTO V_SQL_TABLE_COMMENT
    FROM USER_TAB_COMMENTS T
   WHERE T.TABLE_NAME = V_TABLENAME;

  SELECT MAX(WM_CONCAT('`' || A.COLUMN_NAME || '` '))
    INTO V_SQL_PK_COL_LIST
    FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
   WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
     AND B.CONSTRAINT_TYPE = 'P'
     AND A.TABLE_NAME = V_TABLENAME
   GROUP BY A.TABLE_NAME;

  V_SQL_HEADER := '-- ' || V_SQL_TABLE_COMMENT || ' 建表语句' || CHR(13);

  IF PI_ISDROP = 1 THEN
    V_SQL_HEADER := V_SQL_HEADER || 'DROP TABLE IF EXISTS `' || V_TABLENAME || '`;' ||
                    CHR(13);
  END IF;
  V_SQL_HEADER := V_SQL_HEADER || 'CREATE TABLE `' || V_TABLENAME || '`  (';

  -- 表列信息
  SELECT WM_CONCAT(CHR(13) || '`' || T.COLUMN_NAME || '` ' ||
                   (CASE
                      WHEN T.DATA_TYPE = 'VARCHAR2' OR T.DATA_TYPE = 'NVARCHAR2' THEN
                       'VARCHAR(' || T.DATA_LENGTH || ')'
                      WHEN T.DATA_TYPE = 'DATE' THEN
                       'DATETIME'
                      WHEN T.DATA_TYPE = 'CLOB' THEN
                       'MEDIUMTEXT'
                      WHEN T.DATA_TYPE = 'NUMBER' THEN
                       'DECIMAL'
                      WHEN T.COLUMN_NAME = 'ID' THEN
                       'VARCHAR(32)'
                      ELSE
                       T.DATA_TYPE
                    END) || DECODE(T.NULLABLE, 'N', ' NOT NULL ', '') ||
                   ' COMMENT ''' || TC.COMMENTS || '''')
    INTO V_SQL_BODY
    FROM USER_TAB_COLUMNS T
    LEFT JOIN USER_COL_COMMENTS TC
      ON T.TABLE_NAME = TC.TABLE_NAME
     AND T.COLUMN_NAME = TC.COLUMN_NAME
   WHERE T.TABLE_NAME = V_TABLENAME
   ORDER BY T.COLUMN_ID;

  IF V_SQL_PK_COL_LIST IS NOT NULL THEN
    V_SQL_BODY := V_SQL_BODY || ' ,' || CHR(13) || ' PRIMARY KEY (' ||
                  V_SQL_PK_COL_LIST || ')  ';
  END IF;

  -- 表备注
  V_SQL_BODY := V_SQL_BODY || CHR(13) || ') COMMENT = ''' ||
                V_SQL_TABLE_COMMENT || ''';' || CHR(13);

  -- 唯一索引 
  SELECT (WM_CONCAT('@ALTER TABLE `' || A.TABLE_NAME ||
                    '` ADD UNIQUE INDEX (' ||
                    WM_CONCAT('`' || A.COLUMN_NAME || '`') || ');@'))
    INTO V_SQL_INDEX
    FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
   WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
     AND B.CONSTRAINT_TYPE = 'U'
     AND A.TABLE_NAME = V_TABLENAME
   GROUP BY A.TABLE_NAME, A.CONSTRAINT_NAME;

  IF V_SQL_INDEX IS NOT NULL THEN
    V_SQL_INDEX := REPLACE(REPLACE(V_SQL_INDEX, '@,@', CHR(13)), '@', '');
    V_SQL_INDEX := '-- 创建索引' || CHR(13) || V_SQL_INDEX;
  END IF;

  RETURN V_SQL_HEADER || V_SQL_BODY || V_SQL_INDEX;
END;
/