Hi there I am looking to create a basic SQL trigger for my database.In essence my database is a fake banking system. 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. (The total amount of loan at each branch a2_bankbranch)
At the moment I have this code in a seperate file called 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?
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
.
Your Trigger has a WHERE
that has no value behind it.
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.
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":
Add the column 'Id' and set this one as a PRIMARY KEY
.
Table 'a2_loan':
Add a column 'BankId' where you need to fill in the 'a2_bankbranch.Id', also make in NOT NULL
.
This way you can use this in you Trigger to calculate what the total amount of 'a2_loan.Amount' per bank is.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.