---
title: "关于级联删除个人总结"
author: "JYLike97"
date: "2007-07-25T15:24:53Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-07-25T15:24:53Z"
id: 105401
url: "https://jylike97.iteye.com/blog/105401"
---

级联删除在系统开发中是很常见的处理方法，应用系统中很多以业务逻辑建立起来的实体集合形成一个整体，但是他们分布在不同的表中，一个实体消失了（或这是被软删除了），那么跟他对应的（或者隶属于它）一切其他属性都应盖被删除（正常情况下 :( ）。
这里姑且举一个例子：
**删除酒店。**
酒店拥有：酒店法人（user），酒店房间（room），该酒店的房间类型（rtypes），
          酒店房间的照片（rphtoto），酒店照片（hphoto），床位（beds）。
他们的结构是；
1，酒店法人。
     :?: 
2，酒店本体。
     :?:         :?: 
3，酒店房间  ，  酒店照片。
     :?:         :?:          :?: 
4，房间类型  ，  房间照片   ，    床位。
因此，删除酒店应该，自底向上的删除，这样才能将酒店一切信息删除干净，不会出现断层而遗留报废数据。

```

procedure deletehotel(p_USERID NUMBER,
                      P_RESULT OUT NUMBER
                     )
AS
BEGIN
   P_RESULT:=-1;
  ---删除照片
  update photoes p
  set p.valid=1
  where exists(

               select /*+RULE*/ 1
                 from hotel h
                  where h.hotelid=p.hotelid
                  and   h.valid=0
                  and   h.userid=p_userid
               )
  and p.valid=0;
  ---删除房间类型
  update roomtype rt
    set rt.valid=1
  where exists(
                 select /*+RULE*/ 1
                 from hotel h
                 where h.hotelid=rt.hotelid
                 and  h.valid=0
                 and h.userid=p_userid
              )
  and rt.valid=0;

  ----删除床位
  delete from beds b
  where exists(
              select 1
              from hotel h,rooms r
              where r.hotelid=h.hotelid
              and h.valid=0
              and r.valid=0
              and b.roomid=r.roomid
              and h.userid=p_USERID
              );
  ----删除房间
  update rooms t
   set t.valid=1
  where  t.valid=0
  and exists(select  /*+RULE*/1
             from hotel h
             where h.hotelid=t.hotelid
             and h.valid=0
             and h.userid=p_userid
             );
  ---删除饭店
  update hotel h
  set h.valid=1
  where h.valid=0
   and  h.userid=p_userid;
  ---删除用户
  update emsuser u
  set u.valid=1
  where (u.valid=0 or u.valid=2)
  and u.userid=p_userid;
   p_result:=0;
END deletehotel;

```