相册表h_album
a_id int
a_name varchar(50)照片表h_photo 
p_id int
a_id int 和h_album的外键
p_name varhcar(50)评论表h_comment
c_id int
c_relatedid int 关联的ID,此处关联h_photo的p_id,但不是外键
c_content
==================问题=====================
现在要删除h_album:delete from h_album where a_id=1----此处假设要删除的相册id为1
并删除a_id为1的照片表h_photo:delete from h_photo where a_id=1
删除照片表之前要先删除该相册a_id为1的所有照片的所有评论.
该怎么写呢?

解决方案 »

  1.   

    alter table     cascade
      

  2.   

    自己写出来了delete c from h_comment c inner join h_photo p on p.p_id=c.c_relatedid where c.type=3 and p.a_id=1
    想问下.为什么这里删除的是h_comment以及关联h_photo
    为什么删除掉的数据只有h_comment的,而不会影响h_photo呢?
      

  3.   

    喔 明白了.
    不过我不是要设置级联删除
    我想手动写sql语句来级联删除delete c from h_comment c inner join h_photo p on p.p_id=c.c_relatedid where c.type=3 and p.a_id=1这个语句,为什么只删除h_comment的数据?它不是关联h_photo的数据吗?怎么没有把h_photo的数据也删除掉呢?
      

  4.   

    谢谢叶子的回复
    上面那个问题解决了,自己写出来了delete c from h_comment c inner join h_photo p on p.p_id=c.c_relatedid where c.type=3 and p.a_id=1现在想问的是这个语句,为什么只删除h_comment的数据?它不是关联h_photo的数据吗?怎么没有把h_photo的数据也删除掉呢?
      

  5.   

    你可能没有理解sql的语法.
    delete删除的是from后面的那个表的数据,这里用inner join h_photo只是和表h_comment做一下连接查询出要删除的表h_comment的哪些数据.