You are on page 1of 3

SQL> create table itemmaster(item_id varchar2(5) primary key not null, 2 description varchar2(10),stock number(9,2), price number(9,2)); Table

created. SQL> desc itemmaster; Name Null? ----------------------------------------- -------ITEM_ID NOT NULL DESCRIPTION STOCK PRICE Type ---------------------------VARCHAR2(5) VARCHAR2(10) NUMBER(9,2) NUMBER(9,2)

SQL> create table customermaster(cust_id varchar2(5) primary key not null, 2 cust_name varchar2(15), deposit number(9,2)); Table created. SQL> desc customermaster; Name Null? ----------------------------------------- -------CUST_ID NOT NULL CUST_NAME DEPOSIT Type ---------------------------VARCHAR2(5) VARCHAR2(15) NUMBER(9,2)

SQL> create table itemtransaction(item_id varchar2(5), cust_id varchar2(5), 2 qty number(6), transdate date); Table created. SQL> desc itemtransaction; Name Null? ----------------------------------------- -------ITEM_ID CUST_ID QTY TRANSDATE create or replace trigger transaction before insert on itemtransaction for each row declare no_stock exception; no_deposit exception; qnty itemmaster.stock%type; x itemmaster.stock%type; cost itemmaster.price%type; depo itemmaster.price%type; y itemmaster.price%type; sale itemmaster.price%type; begin select stock into qnty from itemmaster where item_id=:new.item_id; if qnty <:new.qty then dbms_output.put_line('Stock is less'); raise no_stock; else Type ---------------------------VARCHAR2(5) VARCHAR2(5) NUMBER(6) DATE

select price into cost from itemmaster where item_id=:new.item_id; sale:=cost*:new.qty; select deposit into depo from customermaster where cust_id=:new.cust_id; if sale>depo then dbms_output.put_line('Deposit is less'); raise no_deposit; else dbms_output.put_line('Transaction possible,tables updated'); x:=qnty-:new.qty; update itemmaster set stock=x where item_id=:new.item_id; y:=depo-sale; update customermaster set deposit=y where cust_id=:new.cust_id; end if; end if; exception when no_stock then raise_application_error(-20001,'Price Set Zero'); when no_deposit then raise_application_error(-20002,'Deposit Low'); end; SQL> insert into itemmaster values('&item_id','&description',&stock,&price); Enter value for item_id: I1 Enter value for description: GOOD Enter value for stock: 450 Enter value for price: 600 old 1: insert into itemmaster values('&item_id','&description',&stock,&price) new 1: insert into itemmaster values('I1','GOOD',450,600) 1 row created. SQL> / Enter value for Enter value for Enter value for Enter value for old 1: insert new 1: insert 1 row created. SQL> / Enter value for Enter value for Enter value for Enter value for old 1: insert new 1: insert 1 row created. SQL> / Enter value for Enter value for Enter value for Enter value for old 1: insert new 1: insert item_id: I4 description: BAD stock: 5990 price: 3000 into itemmaster values('&item_id','&description',&stock,&price) into itemmaster values('I4','BAD',5990,3000) item_id: I3 description: GOOD stock: 4000 price: 5000 into itemmaster values('&item_id','&description',&stock,&price) into itemmaster values('I3','GOOD',4000,5000) item_id: I2 description: BAD stock: 300 price: 5000 into itemmaster values('&item_id','&description',&stock,&price) into itemmaster values('I2','BAD',300,5000)

1 row created. SQL> insert into customermaster values('&cust_id','&cust_name',&deposit);

Enter Enter Enter old new

value for value for value for 1: insert 1: insert

cust_id: C1 cust_name: AIDA deposit: 4000 into customermaster values('&cust_id','&cust_name',&deposit) into customermaster values('C1','AIDA',4000)

1 row created. SQL> / Enter value for Enter value for Enter value for old 1: insert new 1: insert 1 row created. SQL> / Enter value for Enter value for Enter value for old 1: insert new 1: insert 1 row created. SQL> / Enter value for Enter value for Enter value for old 1: insert new 1: insert 1 row created. SQL> select * from itemmaster; ITEM_ ----I1 I2 I3 I4 DESCRIPTIO STOCK PRICE ---------- ---------- ---------GOOD 450 600 BAD 300 5000 GOOD 4000 5000 BAD 5990 3000 cust_id: C4 cust_name: EMILO deposit: 3400 into customermaster values('&cust_id','&cust_name',&deposit) into customermaster values('C4','EMILO',3400) cust_id: C3 cust_name: DENNIS deposit: 4500 into customermaster values('&cust_id','&cust_name',&deposit) into customermaster values('C3','DENNIS',4500) cust_id: C2 cust_name: ANNA deposit: 3000 into customermaster values('&cust_id','&cust_name',&deposit) into customermaster values('C2','ANNA',3000)

SQL> select * from customermaster; CUST_ ----C1 C2 C3 C4 CUST_NAME DEPOSIT --------------- ---------AIDA 4000 ANNA 3000 DENNIS 4500 EMILO 3400

You might also like