MySQL中的触发器问题

MySQL中的触发器问题,mysql,triggers,database-trigger,Mysql,Triggers,Database Trigger,我必须创建表和触发器来进行银行账户移动。我已经做了一些,但我还停留在平衡点上。我有这些桌子: 在这里,我只插入有关交易的信息,描述说明了我在做什么(存款50,取款20等),D表示存款,W表示取款 CREATE TABLE transactions (account VARCHAR (10) NOT NULL, date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP (), description varchar (30) NOT NULL, type

我必须创建表和触发器来进行银行账户移动。我已经做了一些,但我还停留在平衡点上。我有这些桌子:

在这里,我只插入有关交易的信息,描述说明了我在做什么(存款50,取款20等),
D
表示存款,
W
表示取款

CREATE TABLE transactions
(account VARCHAR (10) NOT NULL,
date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP (),
description varchar (30) NOT NULL,
type ENUM ( 'D', 'W') NOT NULL,
value DECIMAL (7,2) NOT NULL
); 
我已经做了一个触发器,显示哪些用户进行了交易和其他信息。后来我发现了一个问题

我有一个叫做余额的表,我需要有账号和当前余额

CREATE TABLE balance
(Account VARCHAR (10) PRIMARY KEY,
balance DECIMAL (7,2) default 0 NOT NULL 
);
所以我做了如下的触发,但它没有任何作用

DELIMITER //
CREATE TRIGGER BALANCE_AI after insert on transactions for each row 
begin 
IF NEW.type = 'D' THEN UPDATE balance SET balance = balance + new.value WHERE account = new.account  ;
ELSE UPDATE balance set balance = balance - new.value where account = new.account;
end if;
end // 
DELIMITER ; 
我不知道如何更新已确定账户的余额,或在余额表中插入并更新余额

我也试过这样做:

CREATE TRIGGER balance_AI after insert on transactions for each row 
begin 
 UPDATE balance set balance = balance + new.valor where account = new.account;
 insert into balance values (new.account,balance) ON DUPLICATE key update account = account ;
end // 
DELIMITER ;

您只能更新存在的行-测试余额是否存在,如果不存在,则创建余额

drop table if exists transactions,balance;

CREATE TABLE transactions
(account VARCHAR (10) NOT NULL,
date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP (),
description varchar (30) NOT NULL,
type ENUM ( 'D', 'W') NOT NULL,
value DECIMAL (7,2) NOT NULL
);

CREATE TABLE balance
(Account VARCHAR (10) PRIMARY KEY,
balance DECIMAL (7,2) NOT NULL
);

drop trigger if exists t;
DELIMITER //
CREATE TRIGGER t after insert on transactions for each row 
begin 
if not exists(select 1 from balance b where b.account = new.account) then
    insert into balance values (new.account,0);
end if;

IF NEW.type = 'D' THEN 
    UPDATE balance SET balance = balance + new.value WHERE account = new.account  ;
ELSE 
    UPDATE balance set balance = balance - new.value where account = new.account;
end if;
end // 
DELIMITER ; 

insert into transactions (account,description ,type ,value) values (1,'aaa','d',10);
insert into transactions (account,description ,type ,value) values (1,'aaa','d',20);
insert into transactions (account,description ,type ,value) values (1,'aaa','w',10);

select * from balance;

+---------+---------+
| Account | balance |
+---------+---------+
| 1       |   20.00 |
+---------+---------+
1 row in set (0.001 sec)

您只能更新存在的行-测试余额是否存在,如果不存在,则创建余额

drop table if exists transactions,balance;

CREATE TABLE transactions
(account VARCHAR (10) NOT NULL,
date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP (),
description varchar (30) NOT NULL,
type ENUM ( 'D', 'W') NOT NULL,
value DECIMAL (7,2) NOT NULL
);

CREATE TABLE balance
(Account VARCHAR (10) PRIMARY KEY,
balance DECIMAL (7,2) NOT NULL
);

drop trigger if exists t;
DELIMITER //
CREATE TRIGGER t after insert on transactions for each row 
begin 
if not exists(select 1 from balance b where b.account = new.account) then
    insert into balance values (new.account,0);
end if;

IF NEW.type = 'D' THEN 
    UPDATE balance SET balance = balance + new.value WHERE account = new.account  ;
ELSE 
    UPDATE balance set balance = balance - new.value where account = new.account;
end if;
end // 
DELIMITER ; 

insert into transactions (account,description ,type ,value) values (1,'aaa','d',10);
insert into transactions (account,description ,type ,value) values (1,'aaa','d',20);
insert into transactions (account,description ,type ,value) values (1,'aaa','w',10);

select * from balance;

+---------+---------+
| Account | balance |
+---------+---------+
| 1       |   20.00 |
+---------+---------+
1 row in set (0.001 sec)

您在什么时候为账户创建余额?是在账户创建时,还是在第一笔交易发生时需要创建余额。(此触发器期望余额存在)我尝试在余额表中将余额设置为默认值0,但也不起作用,因为它只是让余额始终为0。所以我真的不明白我做错了什么。我假设“在移动时插入后”是一个输入错误,因为您“创建表事务”-但可能不是?我插入后,因为我需要在任何存款或取款后更新余额,所以我认为它应该是在事务表中插入后,也许我错了。扳机在一张不存在的桌子上!请再次阅读我的最后一条评论。您是在什么时候为账户创建余额?是在账户创建时,还是需要在第一笔交易发生时创建余额。(此触发器期望余额存在)我尝试在余额表中将余额设置为默认值0,但也不起作用,因为它只是让余额始终为0。所以我真的不明白我做错了什么。我假设“在移动时插入后”是一个输入错误,因为您“创建表事务”-但可能不是?我插入后,因为我需要在任何存款或取款后更新余额,所以我认为它应该是在事务表中插入后,也许我错了。扳机在一张不存在的桌子上!请再次阅读我最后的评论。