mysql觸發器資料庫同步資料方法

來源:互聯網
上載者:User

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  qianshuei@163.com*
* 建立時間*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;");
   
     }
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.