萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> mysql觸發器數據庫同步數據方法

mysql觸發器數據庫同步數據方法

mysql教程觸發器數據庫教程同步數據方法

<script language="網頁特效">

function res(){
document.getElementById("tab2").value = "pre_"+document.getElementById("tab1").value;
}
</script>
</head>

<body>
<form action="#" method="post" name="form1" target="_blank" id="form1">
  數據庫名
    <input name="dbTbaleName" type="text" id="dbTbaleName" value="dbname" />
    <br>
  驅動表
  <input name="tab1" type="text" id="tab1" /> <br>
  被觸發的表
  <input name="tab2" type="text" id="tab2"  /><br>
  <input type="submit" name="Submit" value="提交" /><br>
</form>
<?php教程
/**
* -------------------文檔注釋------------------------------------------
* 作者*達達*QQ 85089479*email  [email protected]*
* 創建時間*2006-012-*
* 功能描述*數據同步觸發器生成工具*
* 最後修改時間*2006-12-*
* -------------------------------------------------------------------
*/
$dbName = "localhost"; //主機名
$dbUserName = "root"; // 用戶名
$dbpassword = "admin"; //密碼
$dbTbaleName = $_POST['dbTbaleName']; //數據庫名
$tab1 = $_POST['tab1']; //執行的表
$tab2 = $_POST['tab2']; //被觸發的表
$conn = mysql_connect($dbName, $dbUserName, $dbpassword) or die("請檢查你的主機名數據庫用戶名和密碼");
mysql_select_db($dbTbaleName, $conn) or die("數據庫還沒有連接");
mysql_select_db("$dbTbaleName");
$result = mysql_query("select * from $tab1")//得到表查詢的結果
or die("Query failed: " . mysql_error());
echo("<br>");
$i = 1;//這裡是為了替換最後一個列名的,符號所以=1 <>0
echo "create trigger " . $tab1 . "_insert BEFORE INSERT on $tab1<br>";
echo "for each row BEGIN<br>";
echo "INSERT INTO $tab2 SET<br>";
while ($i < mysql_num_fields($result)){//列的數量的循環
   
     $meta = mysql_fetch_field($result);//得到一個關於列的信息的對角
     if (!$meta){
         echo "No information available<br />n";
         }
   //下面輸出的是觸發器程序列信息
     echo "<pre>
  `$meta->name`=NEW.$meta->name,
</pre>";
     $i++;
     }
$meta = mysql_fetch_field($result);
echo "<pre>
  `$meta->name`=NEW.$meta->name;
</pre>";
mysql_free_result($result);
echo("END;");

// -----------------------------------------------------------------
$result = mysql_query("select * from $tab1")
or die("Query failed: " . mysql_error());
$i = 1;
if ($i < mysql_num_fields($result)){
   
     $meta = mysql_fetch_field($result);
   
     // -----------------------------------------------------------------
    /**
     * create trigger `tri_del` BEFORE DELETE on `aaa`
     * for each row BEGIN
     * DELETE FROM bbb WHERE id =OLD.id;
     */
    echo("<br>");
   
   
     echo "create trigger " . $tab1 . "_del BEFORE DELETE on $tab1<br>";
     echo "for each row BEGIN<br>";
   
     echo "DELETE FROM $tab2 WHERE $meta->name =OLD.$meta->name;<br>";
     echo("END;");
   
     }
// -----------------------------------------------------------------
echo("<br>");
/**
* create trigger `up_date` BEFORE UPDATE on `aaa`
* for each row BEGIN
* UPDATE bbb set a1 = new.a1 , a2=new.a2 where id = new.id;
* END;
*/
$result = mysql_query("select * from $tab1")
or die("Query failed: " . mysql_error());
/**
* get column metadata
*/
$i = 1;
echo "create trigger " . $tab1 . "_update BEFORE UPDATE on $tab1<br>";
echo "for each row BEGIN<br>";
echo "UPDATE $tab2 SET<br>";
while ($i < mysql_num_fields($result)){
   
     $meta = mysql_fetch_field($result);
     if (!$meta){
         echo "No information available<br />n";
         }
     echo "<pre>
  `$meta->name`=NEW.$meta->name,
</pre>";
     $i++;
     }
$meta = mysql_fetch_field($result);
echo "<pre>
  `$meta->name`=NEW.$meta->name
</pre>";
mysql_free_result($result);
// -----------------------------
$result = mysql_query("select * from $tab1")
or die("Query failed: " . mysql_error());
$i = 1;
if ($i < mysql_num_fields($result)){
   
     $meta = mysql_fetch_field($result);
     echo("where $meta->name = new.$meta->name;<br>");
   
   
     echo("END;");
   
     }
?>

copyright © 萬盛學電腦網 all rights reserved