树的问题

有一个数据库表有两个字段
ID,PID.
id记录的是对象的ID,这里我使用的是uuid的字符串。pid是在id中的pid表示是id的父对象,通过这个表来表示一个树型结构的一组对象。现在问题是我想删除其中一条记录,连带把她所有的子对象也删除掉,怎么来用SQL语句解决。id和pid的值都是没有规律的,唯一连接的方式就是pid在id中。
[185 byte] By [wyzegg-蛋] at [2007-12-16]
# 1
写个存储过程可以解决.
bzszp-SongZip at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 2
有点难度,想想。。。
jlandzpa-jlandzpa at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 3
不能用存储过程和触发器,只用SQL语句可以吗
wyzegg-蛋 at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 4
用connect by start with可以实现,具体的我说不清楚,你查资料吧
snowy_howe-天下有雪 at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 5
--大致思路
delete from tablename where rowid in
(
SELECT rowid FROM emp
START WITH job = 'PRESIDENT'
CONNECT BY PRIOR empno = mgr);

jlandzpa-jlandzpa at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 6
找到ID='XXX'的所有子记录:
select * from your_table start with id='xxxx'
connect by fid=prior id

删除:
delete from your_table where id in
(select id from your_table start with id='xxxx'
connect by fid=prior id)

如果表是自引用的,这样还删除不掉,提示“已找到子记录”。
用过程肯定可以。

from c_id in (select id from --对记录排序,从底层记录开始删除
(
select rn,id from
(
(select rownum rn,id from your_table start with id='xxx'
connect by fid=prior id)
)order by rn desc
)
loop

delete from your_table where id=c_id.id;
end loop;

CHENGXB-阿困 at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 7
up
wyzegg-蛋 at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 8
我的数据库是mysql有没有别的办法
wyzegg-蛋 at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 9
1。可以先把子对象找出来,删除,如(id='001'纪录):
delete from tabname where id in
(select b.id from tabname a,tabname b where
a.id=b.pid and a.id='001')

2。再删除
delete from tabname where id='001'

zhangshunshi-宇轩 at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 10
zhangshunshi(宇轩) :
这样只把第一层的字节点删除了,树形结构的话不一定只有两层,子节点下面的那些节点也要删除吧
bzszp-SongZip at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 11
这有个笨办法:
第一层字节点:
select id from your_table where fid='001'
第二层字节点:
select id from your_table where fid in
(select id from your_table where fid='001')
第三层自节点:
select id from your_table where fid in
(select id from your_table where fid in
(select id from your_table where fid='001'))
以此类推可以求出每一层的ID,将上述结果UNION,可得到ID='001'的所有子节点。如果最大层数已知,可用这种形式取得任一节点的所有子节点,然后
DELETE FROM your_table WHERE ID IN (前面取子节点的SQL)

不知这样合不合你的意,哈哈!!

CHENGXB-阿困 at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 12
不知道MYSQL中是否可以定义级联删除,下面是ORACLE下的例子:

SQL> create table a (id varchar2(10),pid varchar2(10));

Table created.

SQL> alter table a add constraint pk_a primary key (id);

Table altered.

SQL> alter table a add constraint fk_a
2 foreign key (pid) references a(id)
3 on delete cascade;

Table altered.

SQL> select * from a;

no rows selected

SQL> insert into a values ('1',null);

1 row created.

SQL> insert into a values ('2','1');

1 row created.

SQL> insert into a values ('3','1');

1 row created.

SQL> insert into a values ('4',null);

1 row created.

SQL> insert into a values ('5','3');

1 row created.

SQL> select * from a;

ID PID
---------- ----------
1
2 1
3 1
4
5 3

SQL> delete from a where id=1;

1 row deleted.

SQL> select * from a;

ID PID
---------- ----------
4
KingSunSha-弱水三千 at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...
# 13
up good ,好问题,好方法(三千)

: jlandzpa(欧高黎嘉陈) 的思路也很好
fxbsmile-流水无弦 at 2007-10-23 > top of Msdn China Tech,Oracle,基础和管理...