0% found this document useful (0 votes)
5 views5 pages

Experiment 7

Uploaded by

lakshyasantani14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Experiment 7

Uploaded by

lakshyasantani14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

‭KJSCE/IT-AI&DS/SYBTECH/SEMIII/DMS/2024-25‬

‭Experiment No.7‬

‭Title: To Implement Triggers.‬

‭(Somaiya Vidyavihar University)‬


‭KJSCE/IT-AI&DS/SYBTECH/SEMIII/DMS/2024-25‬

‭Batch:‬‭C-2‬ ‭Roll No.:‬‭16014223049‬ ‭Experiment No.: 7‬

‭ itle:‬‭To implement Triggers for given database.‬


T
‭____________________________________________________________________‬
‭Resources needed:‬‭PostgreSQL 9.3‬
‭____________________________________________________________________‬
‭Theory‬
‭Pre Lab/ Prior Concepts:‬
‭Trigger:‬
‭A‬ ‭trigger‬ ‭is‬ ‭a‬ ‭statement‬ ‭that‬ ‭the‬ ‭system‬ ‭executes‬ ‭automatically‬ ‭as‬ ‭a‬ ‭side‬ ‭effect‬ ‭of‬ ‭a‬
‭modification to the database. Triggers are used to ensure some types of integrity.‬

‭ o design a trigger mechanism you should:‬


T
‭1.‬ ‭Specify‬ ‭when‬ ‭a‬ ‭trigger‬ ‭is‬ ‭to‬ ‭be‬ ‭executed.‬ ‭This‬ ‭is‬ ‭broken‬ ‭up‬ ‭into‬ ‭an‬ ‭event‬ ‭that‬ ‭causes‬ ‭a‬
‭trigger to be checked and a condition that must be satisfied for trigger execution to proceed‬
‭2. Specify the actions to be taken when the trigger executes.‬

‭ eneralized Model:‬
G
‭Triggers‬ ‭are‬‭based‬‭on‬‭the‬‭Event-‬‭condition-‬‭Action‬‭(ECA)‬‭Model.‬‭A‬‭rule‬‭in‬‭the‬‭ECA‬‭model‬
‭has three components.‬
‭1. The event(s) that triggers the rule.‬
‭2.‬ ‭The‬ ‭condition‬ ‭that‬ ‭determines‬ ‭whether‬ ‭the‬‭rule‬‭action‬‭should‬‭be‬‭executed.‬‭If‬‭no‬‭action‬‭is‬
‭specified, the action will be executed once the event occurs.‬
‭3.‬ ‭The‬‭action‬‭to‬‭be‬‭taken.‬‭It‬‭could‬‭be‬‭a‬‭sequence‬‭of‬‭SQL‬‭statements,‬‭a‬‭DB‬‭transaction‬‭or‬‭an‬
‭external program that will be executed automatically.‬

‭ hen to use Trigger:‬


W
‭In‬ ‭many‬ ‭cases,‬ ‭it‬ ‭is‬ ‭convenient‬ ‭to‬ ‭specify‬ ‭the‬‭type‬‭of‬‭action‬‭to‬‭be‬‭taken‬‭when‬‭certain‬‭event‬
‭occurs and when certain conditions are satisfied.‬
‭It‬ ‭may‬ ‭be‬ ‭useful‬ ‭to‬‭specify‬‭a‬‭condition‬‭that,‬‭if‬‭violated,‬‭causes‬‭some‬‭user‬‭to‬‭be‬‭informed‬‭of‬
‭the violation.‬
‭For example:-‬
‭A‬‭manager‬‭may‬‭want‬‭to‬‭be‬‭informed,‬‭if‬‭an‬‭employee’s‬‭travel‬‭expenses‬‭exceed‬‭a‬‭certain‬‭limit‬
‭by receiving a message whenever this occurs.‬
‭The condition is thus used to monitor the database.‬

‭ REATE‬ ‭TRIGGER‬ ‭statement‬ ‭is‬ ‭used‬ ‭to‬ ‭implement‬ ‭such‬ ‭action‬ ‭in‬ ‭SQL.‬ ‭Consider‬ ‭the‬
C
‭triggers for following cases:-‬
‭1. Trigger for insertion:-‬
‭This‬ ‭trigger‬ ‭executes‬ ‭whenever‬ ‭condition‬ ‭is‬ ‭satisfied,‬ ‭during‬ ‭the‬ ‭insertion‬ ‭statement.‬ ‭If‬ ‭no‬
‭condition is satisfied, then it executed for every insertion statement, for the relation specified.‬
‭Example:-‬
‭(Somaiya Vidyavihar University)‬
‭KJSCE/IT-AI&DS/SYBTECH/SEMIII/DMS/2024-25‬

I‭ n DB, we have created a trigger for insertion, on the relation Employee. If salaries <‬
‭1500 then print ‘Unsuccessful’ else print ‘Successful’.‬
‭2. Trigger for Updation:-‬
‭This‬ ‭trigger‬ ‭executes‬ ‭for‬ ‭updating‬ ‭of‬‭a‬‭column‬‭name(s)‬‭of‬‭a‬‭particular‬‭relation.‬‭A‬‭condition‬
‭may or may not be specified.‬
‭Example:-‬
‭In‬ ‭our‬ ‭DB,‬ ‭we‬ ‭have‬ ‭created‬ ‭a‬ ‭trigger‬ ‭for‬ ‭updating,‬ ‭on‬ ‭the‬ ‭relation‬ ‭Employee.‬ ‭If‬ ‭salary‬ ‭is‬
‭updated to unacceptable amount, then print unsuccessful and rollback else print successful.‬
‭3. Trigger for Deletion:-‬
‭This‬ ‭trigger‬ ‭executes‬ ‭during‬ ‭the‬ ‭deletion‬ ‭statement.‬‭If‬‭a‬‭particular‬‭condition‬‭is‬‭satisfied,‬‭the‬
‭system may allow or not allow the deletion of certain tuples,‬
‭Example:-‬
‭If SSN from Employee =101, then print ‘Unsuccessful’ and rollback, else print ‘Successful’.‬

I‭ n‬‭Postgresql‬‭we‬‭need‬‭to‬‭create‬‭a‬‭function‬‭to‬‭execute‬‭all‬‭action‬‭statements‬‭of‬‭trigger‬‭and‬‭call‬
‭this function in create trigger statement for certain event.‬
‭Example.‬
‭In‬‭employeee‬‭table‬‭dnum‬‭is‬‭a‬‭foreign‬‭key.‬‭So‬‭the‬‭trigger‬‭writen‬‭here‬‭will‬‭increment‬‭total_emp‬
‭of‬ ‭department‬ ‭table‬ ‭by‬ ‭one‬ ‭and‬ ‭total_sal‬ ‭of‬ ‭department‬ ‭by‬ ‭salary‬ ‭of‬ ‭newly‬ ‭inserted‬
‭employee's salary.‬

‭CREATE or replace FUNCTION inse_function() RETURNS trigger As $emp_update$‬

‭begin‬
‭UPDATE dept set total_emp=total_emp + 1,‬
‭total_sal=total_sal+ new.salary where dept.dno=new.dno;‬
‭RETURN new;‬

‭ ND‬
E
‭$emp_update$ LANGUAGE plpgsql;‬

‭CREATE TRIGGER emp_update AFTER INSERT‬


‭ON emp_table FOR each ROW‬
‭EXECUTE PROCEDURE inse_function();‬
‭________________________________________________________________________‬
‭Procedure / Approach /Algorithm / Activity Diagram‬

1‭ . Create new database for your application‬


‭2. Apply required integrity constraints on tables in your database‬
‭To design a trigger.‬
‭(Somaiya Vidyavihar University)‬
‭KJSCE/IT-AI&DS/SYBTECH/SEMIII/DMS/2024-25‬

1‭ . Identify events in the database.‬


‭2. Specify conditions under which the trigger is to be executed.‬
‭3. Specify actions to be taken when trigger is executed.‬

_‭ ____________________________________________________________________‬
‭Results: (Program printout with output / Document printout as per the format)‬

‭ REATE OR REPLACE FUNCTION prevent_duplicate_reviews()‬


C
‭RETURNS TRIGGER AS $$‬
‭BEGIN‬
‭-- Check if the user has already reviewed the restaurant‬
‭IF EXISTS (‬
‭SELECT 1‬
‭FROM Reviews‬
‭WHERE UserID = NEW.UserID‬
‭AND RestroID = NEW.RestroID‬
‭) THEN‬
‭RAISE EXCEPTION 'User has already reviewed this restaurant.';‬
‭END IF;‬

‭ ETURN NEW;‬
R
‭END;‬
‭$$ LANGUAGE plpgsql;‬

‭ REATE TRIGGER PreventDuplicateReviews‬


C
‭BEFORE INSERT OR UPDATE ON Reviews‬
‭FOR EACH ROW‬
‭EXECUTE FUNCTION prevent_duplicate_reviews();‬

‭(Somaiya Vidyavihar University)‬


‭KJSCE/IT-AI&DS/SYBTECH/SEMIII/DMS/2024-25‬

‭___________________________________________________________________________‬

‭Questions:‬

‭Explain any one real-time application of trigger‬

‭Ans.‬
‭One real-time application of triggers is in inventory management systems, where they‬
‭automatically reorder stock when levels fall below a set threshold. This ensures optimal‬
‭inventory levels without manual intervention. Additionally, triggers can enforce‬
‭business rules and log changes for audit trails. Overall, they enhance efficiency,‬
‭accuracy, and timeliness in managing inventory.‬

‭(Somaiya Vidyavihar University)‬

You might also like