zoukankan      html  css  js  c++  java
  • oracle存储过程删除树状结构的表数据

    今天在删除一个车辆品牌表的时候,遇到了一个问题,是在java的代码中做逻辑删除还是直接在Oracle中一次删除完成呢

    思来想去觉得还是在sql里直接删除比较合适,

    为什么呢?

    第一,涉及数据库的读写操作一定要遵从ACID原则,

    第二,java中操作单个删除太麻烦,而且占用内存,多次打开数据库链接,造成资源浪费

    第三,好久没写储存过程了,太咸

    下面是这个表的结构

    create table T_BRAND_CLASS 
    (
       ID                   VARCHAR2(32)         not null,
       BRAND_NAME           VARCHAR2(100),
       BRAND_IMG            VARCHAR2(32),
       BRAND_INITLAL        VARCHAR2(10),
       BRAND_ISHOT          VARCHAR2(10),
       BRAND_ISSTICK        VARCHAR2(10),
       constraint PK_T_BRAND_CLASS primary key (ID)
             USING INDEX  ENABLE
    );
    
    comment on table T_BRAND_CLASS is
    '品牌分类';
    create table T_BRAND_TYPE 
    (
       ID                   VARCHAR2(32)         not null,
       BRAND_CLASS_ID       VARCHAR2(32),
       TYPE_NAME            VARCHAR2(100),
       constraint PK_T_BRAND_TYPE primary key (ID)
             USING INDEX  ENABLE
    );
    
    comment on table T_BRAND_TYPE is
    '品牌型号';
    create table T_BRAND_SPECIFICATION 
    (
       ID                   VARCHAR2(32)         not null,
       BRAND_ID             VARCHAR2(32),
       BRAND_CC             VARCHAR2(32),
       CC_TYPE              NUMBER,
       constraint PK_T_BRAND_SPECIFICATION primary key (ID)
             USING INDEX  ENABLE
    );
    
    comment on table T_BRAND_SPECIFICATION is
    '品牌规格';
    create table T_BRAND_SERIES 
    (
       ID                   VARCHAR2(32)         not null,
       SERIES_NAME          VARCHAR2(100),
       SPECIFCATION_ID      VARCHAR2(32),
       SERIES_YEAR          VARCHAR2(10),
       constraint PK_T_BRAND_SERIES primary key (ID)
             USING INDEX  ENABLE
    );
    
    comment on table T_BRAND_SERIES is
    '品牌车系';

    大概的可以看出来这个一个树状结构的表,彼此的关联就是一个对方的id,温习了下储存过程常用的关键字

    cursor :游标 紧跟后面的sql查询的结果会自动给定义的变量赋值,数据结构类似lua里的table,
    BEGIN:BEGIN和end之间的代码会动态的执行;
    for .. in .. loop:循环
    v_sql VARCHAR2(1000);:定义数据类型
    v_sql:= :赋值方式
    execute immediate .aa.USING bb :aa是一个要执行的sql语句,bb是一个aa中的输出占位符填充数据

    看完上面大概后一个简单的储存过程就可以写出来了,如下传入一个表名的id 就可以删除对应表数据下面的所有关联数据


    create or replace PROCEDURE DEL_brand_class(istable_id  in VARCHAR2) 
    is  
    v_sql VARCHAR2(1000);
    typeid VARCHAR2(1000);
    speid VARCHAR2(1000);
    serid VARCHAR2(1000);
    cursor cur_tableid is
     select a.id as id,
      b.id as typeid,
      c.id as speid,
      d.id as serid
      from t_brand_class a
      left join T_BRAND_TYPE  b on a.id = b.BRAND_CLASS_ID
      left join T_BRAND_SPECIFICATION   c on b.id = c.BRAND_ID
      left join T_BRAND_SERIES d on c.ID = d.SPECIFCATION_ID
      where a.id = istable_id;
    BEGIN
     for rec_id in cur_tableid loop
    
      v_sql:='delete from t_brand_class where id =:1';
      typeid:='delete from T_BRAND_TYPE where id = :1';
      speid:='delete from T_BRAND_SPECIFICATION where id = :1';
      serid:='delete from T_BRAND_SERIES where id = :1';
      execute immediate v_sql USING rec_id.id;
        execute immediate typeid USING rec_id.typeid;
          execute immediate speid USING rec_id.speid;
            execute immediate serid USING rec_id.serid;
      end loop;
    commit;
    END DEL_brand_class;
  • 相关阅读:
    springmvc log4j 配置
    intellij idea maven springmvc 环境搭建
    spring,property not found on type
    intellij idea maven 工程生成可执行的jar
    device eth0 does not seem to be present, delaying initialization
    macos ssh host配置及免密登陆
    centos7 搭建 docker 环境
    通过rest接口获取自增id (twitter snowflake算法)
    微信小程序开发体验
    gitbook 制作 beego 参考手册
  • 原文地址:https://www.cnblogs.com/dmeck/p/9391521.html
Copyright © 2011-2022 走看看