简体   繁体   English

简单的SQL oracle触发器

[英]Simple SQL oracle trigger

Hi there I am looking to create a basic SQL trigger for my database.In essence my database is a fake banking system. 嗨,我正在寻找为数据库创建基本的SQL触发器的方法,实际上我的数据库是一个伪造的银行系统。 The load file is shown below 加载文件如下所示

-- prefix a2 is to

DROP TABLE a2_loanr;
DROP TABLE a2_accr;
DROP TABLE a2_customer;
DROP TABLE a2_account;
DROP TABLE a2_loan;
DROP TABLE a2_bankbranch;
DROP TABLE a2_bank;

CREATE TABLE a2_bank (
routingcode   VARCHAR(200)  PRIMARY KEY,
name          VARCHAR(200)  NOT NULL,
address       VARCHAR(200)  NOT NULL
);

INSERT INTO a2_bank VALUES
( '123456','ASB', '3 gladstone rd');
INSERT INTO a2_bank VALUES
( '123556','BNZ', '5 gladstone rd');
INSERT INTO a2_bank VALUES
( '12456','KIWIBANK', '3 gladstone rd');


CREATE TABLE a2_bankbranch (
name           VARCHAR(200)  NOT NULL,
branch_num     VARCHAR(200)  NOT NULL,
address        VARCHAR(200)  NOT NULL,
routing_code   VARCHAR(200)  NOT NULL,
total_loan     VARCHAR(200)  NOT NULL,
FOREIGN KEY(routing_code) REFERENCES a2_bank(routingcode),
PRIMARY KEY(branch_num, routing_code)
);
INSERT INTO a2_bankbranch VALUES
( 'ASB', '5', '3 gladstone rd', '123456');
INSERT INTO a2_bankbranch VALUES
( 'ASB', '4', '28 stevee rd', '123456');

CREATE TABLE a2_loan (
loan_num       CHAR(10)  PRIMARY KEY,
type           VARCHAR(200)  NOT NULL,
amount         VARCHAR(200)  NOT NULL,
contract_date  DATE          NOT NULL
);

INSERT INTO a2_loan VALUES
( '323', 'Mortgage', '$2000000', TO_DATE('11-03-1994', 'DD-MM-YYYY') );
INSERT INTO a2_loan VALUES
( '33', 'Car', '$2000', TO_DATE('12-08-1994', 'DD-MM-YYYY') );
INSERT INTO a2_loan VALUES
( '3243', 'Pesonal', '$875', TO_DATE('14-06-1994', 'DD-MM-YYYY') );
INSERT INTO a2_loan VALUES
( '6', 'Mortgage', '$400500', TO_DATE('11-06-1994', 'DD-MM-YYYY') );

CREATE TABLE a2_account (
acc_num       CHAR(10)  PRIMARY KEY,
type           VARCHAR(20)  NOT NULL,
balance         VARCHAR(10)  NOT NULL
);
INSERT INTO a2_account VALUES
( '2539267332', 'Savings', '20');
INSERT INTO a2_account VALUES
( '8237893378', 'Cash', '300');
INSERT INTO a2_account VALUES
( '2378723936', 'Cheque', '75');
INSERT INTO a2_account VALUES
( '2378723937', 'Savings', '175');


CREATE TABLE a2_customer (
ird_num         CHAR(8)  PRIMARY KEY,
name            VARCHAR(200)  NOT NULL,
address         VARCHAR(200)  NOT NULL,
phone           VARCHAR(20)
);
INSERT INTO a2_customer VALUES
( '25362672',  'Stan Yel', '5 Wanna way', '02010201');
INSERT INTO a2_customer VALUES
( '83783783', 'Cam Birch', '34 Trada st', '02302020202');
INSERT INTO a2_customer VALUES
( '23723367', 'Jeff King', '5 Queens st', '38982383');
INSERT INTO a2_customer VALUES
( '54637822',  'John Smith', '24 Queen st', '38922383');


CREATE TABLE a2_accr (
ird_num                CHAR(8)  NOT NULL ,
account_num            CHAR(10)  NOT NULL,
FOREIGN KEY(ird_num) REFERENCES a2_customer(ird_num),
FOREIGN KEY(account_num) REFERENCES a2_account(acc_num)
);
INSERT INTO a2_accr VALUES
( '25362672', '2539267332');
INSERT INTO a2_accr VALUES
( '83783783', '8237893378');
INSERT INTO a2_accr VALUES
( '83783783', '2378723937');

CREATE TABLE a2_loanr (
ird_num                CHAR(8)  NOT NULL ,
loan_num            CHAR(10)  NOT NULL,
FOREIGN KEY(ird_num) REFERENCES a2_customer(ird_num),
FOREIGN KEY(loan_num) REFERENCES a2_loan(loan_num)
);
INSERT INTO a2_loanr VALUES
( '54637822', '323');
INSERT INTO a2_loanr VALUES
( '23723367', '33');

COMMIT;

With this database I am looking to create the derived attribute "total_loan" that is simply the total amount of loan that each bank branch has at any given moment in time. 通过该数据库,我希望创建派生属性“ total_loan”,该属性就是每个银行分支机构在任何给定时刻的总贷款额。 (The total amount of loan at each branch a2_bankbranch) (每个分支机构a2_bankbranch的贷款总额)

At the moment I have this code in a seperate file called trig.sql: 目前,我在一个名为trig.sql的单独文件中拥有以下代码:

-- Create a trigger that will update the total loan amount
--that each bank brach may have

CREATE OR REPLACE TRIGGER ttl
AFTER INSERT OR UPDATE OR DELETE OF amount ON a2-loan
FOR EACH ROW
BEGIN
IF INSERTING THEN
UPDATE a2_bankbranch
SET total_loan =
WHERE
ELSIF UPDATING THEN
UPDATE a2_bankbranch
SET total_loan =
WHERE
ELSE --deleting
UPDATE a2_bankbranch
SET total_loan =
WHERE
END;

At the moment I am struggling to get the trigger working correctly. 目前,我正在努力使触发器正常工作。 And also how do I run my trigger through my load.sql file? 还有如何通过load.sql文件运行触发器?

A trigger responds to inserts, updates and deletes on a table. 触发器响应表上的插入,更新和删除。 So you must make sure that the trigger is in place before you insert any data. 因此,在插入任何数据之前,必须确保触发器到位。

In this case however, it doesn't make any sense to use a trigger since you can just calculate the sum on the fly. 但是,在这种情况下,使用触发器没有任何意义,因为您可以随时计算总和。

A Trigger responds to a INSERT , UPDATE and/or DELETE . 触发器响应INSERTUPDATE和/或DELETE
Your Trigger has a WHERE that has no value behind it. 你的触发器有一个WHERE是背后有没有价值。

I think the setup of your Trigger is good, but that you need to look carefully if the query is possible in a certain situation. 我认为您的触发器的设置很好,但是如果在特定情况下可以进行查询,则需要仔细查看。 You can not UPDATE where there is no line to update. 您无法在没有行要更新的地方进行UPDATE

Have a look here as well, here they explain how to setup a Trigger: enter link description here 也可以在这里看看,在这里他们解释了如何设置触发器: 在此处输入链接描述

Edit: 编辑:

I would add something to your tables as well: 我也会在您的表中添加一些内容:

Table "a2_bankbranch": 表“ a2_bankbranch”:
Add the column 'Id' and set this one as a PRIMARY KEY . 添加列“ Id”并将其设置为PRIMARY KEY
Table 'a2_loan': 表'a2_loan':
Add a column 'BankId' where you need to fill in the 'a2_bankbranch.Id', also make in NOT NULL . 在需要填写“ a2_bankbranch.Id”的位置添加一列“ BankId”,同时输入NOT NULL

This way you can use this in you Trigger to calculate what the total amount of 'a2_loan.Amount' per bank is. 这样,您可以在触发器中使用它来计算每个银行的“ a2_loan.Amount”总额。

So I realise that this is not a real system, but ... 所以我意识到这不是一个真正的系统,但是...

You cannot successfully maintain a summary record using triggers, because if two rows are modified at the same time then each execution of a trigger will not see the changes that have been made in the other session. 您无法使用触发器成功维护摘要记录,因为如果同时修改两行,则每次执行触发器都将看不到在另一个会话中所做的更改。 The only solution to that is to provide a locking mechanism to serialise access to the branch table. 唯一的解决方案是提供一种锁定机制,以序列化对分支表的访问。

You could also update the branch record asynchronously, by just having a scheduled job that runs every hour/day to perform the update. 您还可以通过仅安排一个每小时/每天运行一次的计划作业来执行更新来异步更新分支记录。

Or, you could use a fast refresh materialised view to aggregate the loan data by branch, which would be an efficient way of being able to also report on loans per day/month, total amounts etc. 或者,您可以使用快速刷新的物化视图按分支汇总贷款数据,这将是一种有效的方式,它还能够报告每天/每月的贷款,总金额等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM