0% found this document useful (0 votes)
40 views2 pages

DQ 2

Triggers are stored database procedures that automatically execute when data is inserted, updated or deleted from a table. They allow actions to be taken before or after events, like automatically adding 100 points to a student's marks when a new record is inserted into the student table. Stored procedures are reusable code blocks that simplify writing SQL queries. They can capture business transactions and make querying faster by minimizing network traffic between servers and clients. An example creates a stored procedure that selects all records from a Customers table.

Uploaded by

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

DQ 2

Triggers are stored database procedures that automatically execute when data is inserted, updated or deleted from a table. They allow actions to be taken before or after events, like automatically adding 100 points to a student's marks when a new record is inserted into the student table. Stored procedures are reusable code blocks that simplify writing SQL queries. They can capture business transactions and make querying faster by minimizing network traffic between servers and clients. An example creates a stored procedure that selects all records from a Customers table.

Uploaded by

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

What is a trigger, and what is its purpose? Give an example.

What is a stored procedure,


and why is it particularly useful? Give an example
Ans:-
Trigger: A trigger is a collection of commands used to carry out certain actions whenever a
table's data is added, deleted, or updated. Any time a data manipulation event takes place, the
system itself activates the trigger. Triggers are called either prior to or following the addition,
modification, or deletion of a data row and are always connected to a table. Any table may
contain one or more triggers. Additionally, it is kept in the database as a stored process. It is a
component of a transaction and does not involve any COMMIT transactions.
Triggers are used for several purposes. Some purposes are:
1. Triggers' primary function is to automatically run code when an event takes place. To put it
another way, using triggers is the ideal choice if we require a specific piece of code to always
be run in response to an event.
2. When performing insert, update, or delete actions on the impacted table, triggers generate
additional checks.
3. Because default constraints are unable to accommodate complex default values, triggers
enable us to encode them.
4. Triggers give us the ability to decide what actually occurs when someone executes an insert,
update, or delete on a view that has access to many tables.
5. In a table, we can use triggers to compute aggregated columns.

Example: Assume that the attributes Student id, Name, Address, and Marks are present in a
table called Student. As a result, we need to develop a trigger that, each time a new student is
added to the table, will add 100 marks to each new row of the Marks column. The SQL Trigger
is going to be:

CREATE TRIGGER Add_marks


BEFORE
INSERT ON Student
FOR EACH ROW
SET new.Marks = new.Marks + 100;

Stored Procedure: A stored procedure is a collection of Structured Query Language (SQL)


commands with a given name that are kept together in a relational database management system
(RDBMS) so they can be used and shared by various programs. Therefore, if we need to
frequently write SQL queries, we can save them as stored procedures and simply call them to run
them.
Stored procedures are especially beneficial. because business transactions may be captured and
represented using stored procedures. Executing SQL queries can be made easier and faster by
using stored procedures. Additionally, stored procedures might help to minimize the amount of
network traffic between servers and clients.

Example: The following SQL statement creates a stored procedure named "SelectAllCustomers"
that selects all records from the "Customers" table:

CREATE PROCEDURE SelectAllCustomers
AS
SELECT * FROM Customers
GO;
EXEC SelectAllCustomers; [to execute]

You might also like