可以使用 Trigger,新增 A table 時就會去 update B table 了,以下用 Postgresql 資料庫為例
<pre class="c" name="code">
CREATE TABLE A (
id character(15) NOT NULL primary key,
quantity integer
);
CREATE TABLE B (
id character(15) NOT NULL primary key,
quantity integer
);
CREATE OR REPLACE FUNCTION addOtherTableQuantity() RETURNS trigger AS '
DECLARE
changed boolean;
BEGIN
UPDATE B SET quantity = (new.quantity + quantity) where id = new.id;
RETURN new;
END
' LANGUAGE plpgsql;
CREATE TRIGGER add_Other_Table_Quantity AFTER INSERT ON A
FOR EACH ROW EXECUTE PROCEDURE addOtherTableQuantity();
insert into B VALUES('1',5);
insert into A VALUES('1',5);
SELECT * FROM B;
當然也可用 PHP,把以下的 SQL 包在同一個 Transaction 即可
<pre class="c" name="code">
insert into A VALUES('1',5);
UPDATE B SET quantity = (5 + quantity) where id = 1;
<pre class="c" name="code">
<meta http-equiv="Content-Type" content="text/html; charset=uft8"/>
<form action=in2.php method=POST >
料號: <input type=text name=Number size=10 maxlength=25>
入庫量: <input type=text name=stock size=10 maxlength=25>
<p>
<center><input type=submit value="送出"> </center>
</form>
以上是我輸入資料的頁面
<pre class="c" name="code">
<meta http-equiv="Content-Type" content="text/html; charset=uft8"/>
<?php
mysql_connect ("localhost","root","Xxxx");
mysql_query("SET NAMES 'utf8'");
mysql_select_db (webdb2);
mysql_query ("INSERT INTO purchase ( Number,stock)
VALUES ('$Number','$stock')");
?>
這個是傳送的頁面
我應該把PHP程式碼放在?