简体   繁体   中英

Trigger that involves multiple tables Oracle, PL/SQL

I am new to SQL and I am trying to figure out triggers. I need to write a trigger that involves 3 tables, don't worry it is only triggered by one of the tables...

CREATE TABLE CUSTOMERS 
(CUSTID CHAR(8) constraint customers_pk primary key, 
CREDITSCORE NUMBER(5,2)

CREATE TABLE LOANDETAILS 
(LOANNO VARCHAR2(11) primary key, 
CUSTID CHAR(8),  
LOANSTATUSCODE NUMBER(3,0), 
RATE NUMBER(5,2), 

CREATE TABLE SCORECONVERSIONCHART 
(SCOREBAND VARCHAR2(1) constraint scorecc_pk primary key, 
MINSCORE NUMBER(3,0), 
MAXSCORE NUMBER(3,0), 
BASERATEADJUSTMENT NUMBER(4,2)
);

So when the credit score (creditscore) from the customer table is updated, i would like to look at the loan type (LOANTYPE) in the loan details table. If the loan type is a 1 or a 2 I want to update the loan rate in the loan details table. The loan rate is updated by seeing where the custom credit score falls (creditscore in customer table) in the score conversion chart table- if it falls between a given min and max score in the conversion chart table then the appropriate baserateadjustment is added to a base rate of 3%.

I am unclear as to how to get all of these tables to interact in the trigger,

If someone could help me in plain language or point me to a great resource I would appreciate it.

The following example may provide a starting place for you.
This trigger will conditionally update the LOANDETAILS , if the loan type is of an adjustable type, by the conversion in SCORECONVERSIONCHART , only when credit score is updated.

First, add some test data:

INSERT INTO CUSTOMERS VALUES ('00000000','Frodo','Baggins',null,null,null,'frodo@hobbiton.com',123,750);
INSERT INTO CUSTOMERS VALUES ('00000001','Chewbacca','?',null,null,null,'chewie@hoth.com',456,775);


INSERT INTO LOANDETAILS VALUES ('A',NULL,'00000000','1',NULL,NULL,7.2,NULL,NULL);
INSERT INTO LOANDETAILS VALUES ('B',NULL,'00000001','3',NULL,NULL,4.2,NULL,NULL);

INSERT INTO SCORECONVERSIONCHART VALUES ('X',500,599,22);
INSERT INTO SCORECONVERSIONCHART VALUES ('Y',600,699,3);
INSERT INTO SCORECONVERSIONCHART VALUES ('Z',700,799,1);
COMMIT;

Then create the TRIGGER Edit The original example presumed there was only one loan per customer. Updated to handle multiple loans per customer.

CREATE OR REPLACE TRIGGER Q2
AFTER UPDATE OF CREDITSCORE
  ON CUSTOMERS
FOR EACH ROW
  DECLARE
    V_LOAN_TYPE NUMBER;
  BEGIN
      UPDATE LOANDETAILS
      SET RATE = (SELECT SCORECONVERSIONCHART.BASERATEADJUSTMENT + 3
                  FROM SCORECONVERSIONCHART
                  WHERE :NEW.CREDITSCORE BETWEEN SCORECONVERSIONCHART.MINSCORE
                  AND SCORECONVERSIONCHART.MAXSCORE)
      WHERE LOANDETAILS.CUSTID = :NEW.CUSTID
      AND LOANDETAILS.LOANTYPE IN ('1','2');
END;
/

Then, test it:

SELECT CUSTID, LOANTYPE, RATE FROM LOANDETAILS;
CUSTID    LOANTYPE  RATE  
00000000  1         7.2   
00000001  3         4.2   

Then, update a non-credit-score attribute:

UPDATE CUSTOMERS SET STATE = 'WI';
SELECT CUSTID, LOANTYPE, RATE FROM LOANDETAILS;
CUSTID    LOANTYPE  RATE  
00000000  1         7.2   
00000001  3         4.2   

Then update the credit scores. Frodo's loan changes (his loan type is eligible) to 3% + the scoreconversionchart adjustment of 3% but Chewie's does not move at all.

UPDATE CUSTOMERS SET CREDITSCORE = 600;
SELECT CUSTID, LOANTYPE, RATE FROM LOANDETAILS;
CUSTID    LOANTYPE  RATE  
00000000  1         6     
00000001  3         4.2   

If the loan types are numbers, I'd suggest recording them as numbers.

You can do something like below.

    CREATE OR REPLACE TRIGGER trig
        AFTER UPDATE OF CREDITSCORE 
        ON CUSTOMERS
        FOR EACH ROW
        DECLARE
        temp_RATE LOANDETAILS.RATE%type
        temp_adj  SCORECONVERSIONCHART.BASERATEADJUSTMENT%type;
        BEGIN
        UPDATE LOANDETAILS
        SET RATE = RATE + 0.03* ( select s.BASERATEADJUSTMENT from customer c join loandetails l on c.custid=l.custid cross join SCORECONVERSIONCHART s where new.crediscore between s.minscore and s.maxscore and l.LOANTYPE in (1,2) and c.custid=new.custid)
WHERE CUSTID= ( select c.custid from customer c join loandetails l on c.custid=l.custid cross join SCORECONVERSIONCHART s where new.crediscore between s.minscore and s.maxscore and l.LOANTYPE in (1,2) and c.custid=new.custid)

        END IF;
        END;
        /

Now with that above subquery u will get baserate of that eligible customers. You can change that rate as per ur requirement.

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.

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