Triggers Prac

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

TRIGGERS,STORED PROCEDURE

1) TAKE ANY SAMPLE DATA WITH 2-3 TABLES & CREATE 3 DIFFERENT IN IT
 SIMPLE VIEW

2 TABLE JOIN

DERIVED FIELD VIEW


2) WHAT IS STORED PROCEDURE,WHY IS IT USED ? WHAT IS THE SYNTAX FOR STORED
PROCEDURE. ILLUSTRATE WITH AN EXAMPLE.

A stored procedure is a set of SQL statements that is stored as an object in the database.It
can be saved to run many times and even later time, making it easier for yourself and other
developers in the future.

 We can call stored procedures from a database trigger, a stored subprogram, or
interactively from SQL Command Line (SQL* ).

Functions can be used in any SQL command. Think of the COUNT function, or a function
that converts to upper case like UPPER. It can be used in SELECT, INSERT, UPDATE, and
DELETE statements, in many places.However, stored procedures cannot be used in this way.
They can only be used by using a specific command such as CALL or EXECUTE.

Eg. CREATE OR REPLACE PROCEDURE today_is AS


BEGIN
-- display the current system date in long format
DBMS_OUTPUT.PUT_LINE( 'Today is ' || TO_CHAR(SYSDATE, 'DL') );
END today_is;
/
-- to call the procedure today_is, you can use the following
block
BEGIN
today_is(); -- the parentheses are optional here
END;
/

3) Take an sample dataset & illustrate different triggers on it.

AFTER INSERT QUERY

create table BUYS(C_ID int, SHIRTS INT,PANTS INT,TOT NUMBER);


CREATE TRIGGER TRIG_SUM

AFTER INSERT ON BUYS

FOR EACH ROW

BEGIN

UPDATE BUYS SET TOT=SHIRTS+PANTS;

END;

INSERT INTO BUYS (C_ID,SHIRTS,PANTS);

SELECT * FROM BUYS;

BEFORE DELETE QUERY

create table BUYS(C_ID int, SHIRTS INT,PANTS INT);

INSERT INTO BUYS VALUES (3,3,3);

INSERT INTO BUYS VALUES (4,4,8);

CREATE TABLE TRIG_BACKUP(C_ID INT,SHIRTS INT,PANTS NUMBER);

CREATE TRIGGER TRIG_BACKUP

BEFORE DELETE ON BUYS

BEGIN

INSERT INTO TRIG_BACKUP

SELECT * FROM BUYS;

END;

DELETE FROM BUYS WHERE C_ID =3;

SELECT * FROM BUYS;

You might also like