Monday, 22 September 2014

Update two columns of two tables using trigger

Create three tables: [customer,purchase,payment]

create table  customer(customerid number not null,customername VARCHAR2(20),
                                   CONSTRAINT customerid_pk PRIMARY KEY(customerid));
                     
create table purchase(purchaseid number not null,customerid number not null,
                                  CONSTRAINT purchaseid_pk PRIMARY KEY(purchaseid),
                                  CONSTRAINT customerid_fk foreign key(customerid) REFERENCES
                                  customer(customerid));
                     
create table payment(paymentid number not null,customerid number not null,
                                 CONSTRAINT paymentid_pk PRIMARY KEY(paymentid),
                                 CONSTRAINT customerid1_fk foreign key(customerid) REFERENCES
                                 customer(customerid));

customer                                 












purchase

payment









Creating trigger

create or replace TRIGGER customertrigger
BEFORE UPDATE of customerid on customer
FOR EACH ROW
BEGIN

UPDATE PAYMENT
set customerid= :new.customerid
where PAYMENT.customerid= :old.customerid;

update purchase 
set customerid = :new.customerid
where purchase.customerid= :old.customerid;

end;

Update a query

update CUSTOMER
set CUSTOMERID=117 WHERE CUSTOMERNAME='aria';

OUTPUT












No comments:

Post a Comment