Gadgets 4 Students Career Guide Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
2.3k views
in RTU/BTU B.Tech (CSE-IV Sem) DBMS Lab by Goeduhub's Expert (7.6k points)
Perform the queries for triggers.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

TRIGGERS

TRIGGERS : A SQL trigger is a database object just like a stored procedure, or we can say it is a special kind of stored procedure which fires when an event occurs in a database. We can execute a SQL query that will "do something" in a database when an event is fired.

Types of Triggers

  1. DDL Trigger
  2. DML Trigger
TRIGGERS PROCEDURES
They are automatically executed on occurrence of specified event. They can be executed whenever required.
Triggers can't be called inside a procedure. But, you can call a procedure inside a trigger.
We can not pass parameters to triggers. We can pass parameters to procedures.
Trigger never return value on execution. Procedure may return value/s on execution.

Creating a trigger function : 

CREATE FUNCTION trigger_function()

       RETURNS trigger AS

SQL CREATE TRIGGER statement :

CREATE TRIGGER trigger_name

{BEFORE | AFTER | INSTEAD OF} {event [OR ...]}

   ON table_name

   [FOR [EACH] {ROW | STATEMENT}]

       EXECUTE PROCEDURE trigger_function

Example : 

CREATE TABLE employees(

   id SERIAL PRIMARY KEY,

   first_name VARCHAR(40) NOT NULL,

   last_name VARCHAR(40) NOT NULL

);

INSERT INTO employees (first_name, last_name)

VALUES ('John', 'Doe');

INSERT INTO employees (first_name, last_name)

VALUES ('Lily', 'Bush');

SELECT * FROM employees;

Output : 

id first_name last_name
1 john doe
2 lily blush


UPDATE QUERY : 

UPDATE employees

SET last_name = 'Brown'

WHERE ID = 2;

SELECT * FROM employees;

Output : 

id first_name last_name
1 john doe
2 lily brown

Advantages of Triggers :

  • Trigger generates some derived column values automatically
  • Enforces referential integrity
  • Event logging and storing information on table access
  • Auditing
  • Synchronous replication of tables
  • Imposing security authorizations
  • Preventing invalid transactions


For more Manipal University Jaipur CSE-IV Sem DBMS Lab Experiments CLICK HERE

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...