• Oracle——判断对象是否存在(未完工)


    一、系统表:

    1、User_Tables:存储用户下的所有表的信息;

    2、dba_tables:存储管理员权限下的所有表的信息;

    3、all_tables:存储所有表的信息。

    4、all_Tab_Columns:存储所有表的所有字段的信息

    二、判断对象是否存在

    1、判断表

    我们只能通过使用select count(*) 的方式判断当前表是否存在,返回1则代表存在,0则代表不存在,例如:
    SELECT COUNT(*) FROM User_Tables WHERE table_name = 'CODE_BMDM';(在SQL中使用这种方法亦可)
    需要注意的是:表名(或者其他对象名)必须全部大写,有特殊字符的除外(表名之间有空格等特殊字符),否则查询不到。其中的 User_Tables(用户下的所有表) 也可以换成dba_tables(管理员权限下的所有表) 或者all_tables(所有表)

     2、判断字段

    declare cl integer:=0;
    begin
      select count(*) into cl from all_tab_columns where table_name='表名' and column_name='字段名';
      if cl<1 then /* 不存在字段 */
        dbms_output.put_line('bu存在字段');
        execute immediate'alter table 表名 add (字段名 varchar2(36))';
      end if;
    end;

    • 添加字段的语法:alter table tablename add (column datatype [default value][null/not null],….);
    • 修改字段的语法:alter table tablename modify (column datatype [default value][null/not null],….);
    • 删除字段的语法:alter table tablename drop (column);

    3、查看表中字段类型

    select * From all_tab_columns where table_name=upper('表名');

     

     

     

     

  • 相关阅读:
    核心编程(第七章)
    核心编程答案(第六章)
    spring aop配置切点执行了两次的原因
    spring AOP使用 xml配置
    有关于时间戳的pgsql操作
    sql 中 limit 与 limit,offset连用
    学习大数据笔记day1
    Java实现各种排序
    关于java洗牌发牌小程序
    flex.css
  • 原文地址:https://www.cnblogs.com/SunBlog/p/4015477.html
Copyright © 2020-2023  润新知