代码之家  ›  专栏  ›  技术社区  ›  Mike

用于查找缺失字段关系的智能Oracle工具

  •  1
  • Mike  · 技术社区  · 16 年前

    3 回复  |  直到 16 年前
        1
  •  2
  •   Jeffrey Kemp    16 年前

    我已经做过几次了。我发现这是一种非常人性化的智能—通过在数据字典中运行大量查询(例如,EvilTeach的查询)、从列中查询示例数据、检查应用程序如何创建数据以及了解业务需求和用户流程来提供帮助。

    例如,在许多遗留应用程序中,我发现约束(包括引用完整性约束)是在前端应用程序中检查和实现的,这意味着数据遵循约束(几乎100%:),但实际上在数据库级别并没有约束。很多有趣的结果。

        2
  •  1
  •   EvilTeach    16 年前

    这也许是个好的开始

    select column_name, table_name, data_type
    from user_tab_cols
    order by column_name, table_name
    
        3
  •  1
  •   Kirill Leontev    16 年前

    您可以使用如下查询:

    select c1.TABLE_NAME, c1.COLUMN_NAME, c2.TABLE_NAME, c2.COLUMN_NAME
      from user_tab_columns c1,
           user_tables      at1,
           user_tab_columns c2,
           user_tables      at2
     where c1.COLUMN_NAME = c2.COLUMN_NAME
       and c1.DATA_TYPE = c2.DATA_TYPE
       and c1.TABLE_NAME = at1.TABLE_NAME
       and c2.TABLE_NAME = at2.TABLE_NAME
       and c1.TABLE_NAME != c2.TABLE_NAME
       /*and c1.TABLE_NAME = 'TABLE' --check this for one table
         and c1.COLUMN_NAME = 'TABLE_PK'*/
       and not exists (select 1
              from user_cons_columns ucc,
                   user_constraints  uc,
                   user_constraints  uc2,
                   user_cons_columns ucc2
             where ucc.CONSTRAINT_NAME = uc.CONSTRAINT_NAME
               and uc.TABLE_NAME = ucc.TABLE_NAME
               and ucc.table_name = c1.TABLE_NAME
               and ucc.column_name = c1.COLUMN_NAME
               and uc.CONSTRAINT_TYPE = 'P'
               and uc2.table_name = c2.TABLE_NAME
               and ucc2.column_name = c2.COLUMN_NAME
               and uc2.table_name = ucc2.table_name
               and uc2.r_constraint_name = uc.constraint_name
               and uc2.constraint_type = 'R')
    

    但是,在这里我同意杰弗里的观点,这是一种非常人性化的智慧,没有工具可以肯定地做到这一点。无论如何,你得手工做。