2014-06-17 94 views
1

我有三個不同的表,例如tbl1tbl2tbl3。我需要從三個表中的每一箇中刪除行。唯一的問題是,每個表之間都存在外鍵約束。如果從tbl1刪除一行,刪除級聯並刪除tbl2tbl3中引用tbl1中已刪除行的所有行。我怎樣才能刪除特定的行?我需要使用外鍵刪除多個表中的多行

回答

0

這裏有一個辦法:

DELETE FROM tbl3 WHERE tbl3.tbl1_id = 'foo'; 
DELETE FROM tbl2 WHERE tbl2.tbl1_id = 'foo'; 
DELETE FROM tbl1 WHERE tbl1.tbl1_id = 'foo'; 
+0

感謝您的重播 –

0

有2種方式

  1. ,則應該設置外來約束ON DELETE CASCADE只有從第一個表中刪除
  2. 你可以做級聯manualy使用3刪除查詢
+0

是的,我使用了三個刪除查詢。謝謝 –

-1

如果($ _ REQUEST [ '行動'] == '刪除'){

 $id=$_REQUEST['site_id']; 
     $qry_arry=mysql_query("Select site_name from site where site_id=$id and client_id='".$client_id."'"); 
     $site_qry=mysql_fetch_array($qry_arry); 
     $site_nm=$site_qry['site_name']; 
     $del_server=mysql_query("DELETE FROM `server` where site_name='".$site_nm."' and client_id='".$client_id."'"); 
     $del_work=mysql_query("DELETE FROM `workstation` where site_name='".$site_nm."' and client_id='".$client_id."'"); 
     $qry=mysql_query("DELETE FROM `site` where site_id=$id and client_id='".$client_id."'"); 
     if(isset($del_server) && isset($del_work) && isset($qry)) 
     { 
     $msg='<div style="width:450px; -webkit-border-radius:5px; -moz-border-radius:5px; border-radius:5px; font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#000000; text-decoration:none; border:solid 1px #999999; margin:5px; padding:5px; background:#BFFFBF;">Deleted successfully </div>'; 
        echo "<script>window.location = 'https://portal.itimpulse.in/home-3' </script>"; 
      }  
        exit(0); 
    }