试图在另一个表的字段中显示一个表的总和

时间:2014-11-18 21:20:01

标签: mysql transactions

我试图建立一个有两个表 - 帐户和交易的基本财务数据库。我希望事务表中给定帐户的事务在帐户表中汇总和填充,而不是作为查询,而是自动填充和填充。因此,换句话说,我希望在“交易”中使用给定帐户的值。汇总并显示帐户' account_balance'。寻找某种alter table或modify语句来实现这一点。

到目前为止我所拥有的一切

CREATE TABLE account
(
account_number NUMERIC (20),
account_name VARCHAR (50),
account_type ENUM ('checking', 'savings', 'other'),
account_balance DECIMAL (6,2),
PRIMARY KEY (account_name)
);

CREATE TABLE transaction
(
transaction_id int NOT NULL AUTO_INCREMENT,
transaction_type ENUM ('deposit', 'withdrawal', 'interest', 'transfer', 'other'),
transaction_amount DECIMAL (6,2),
transaction_description VARCHAR (50),
transaction_date DATE,
account_name VARCHAR (50),
PRIMARY KEY (transaction_id),
CONSTRAINT account_name FOREIGN KEY (account_name) REFERENCES account (account_name)
);

0 个答案:

没有答案