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

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

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
TRIGGERSPROCEDURES
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 : 

idfirst_namelast_name
1johndoe
2lilyblush


UPDATE QUERY : 

UPDATE employees

SET last_name = 'Brown'

WHERE ID = 2;

SELECT * FROM employees;

Output : 

idfirst_namelast_name
1johndoe
2lilybrown

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

3.3k questions

7.1k answers

394 comments

4.6k users

Related questions

 Goeduhub:

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