0% found this document useful (0 votes)
41 views15 pages

DBMS Microproject

Uploaded by

namdev misal
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)
41 views15 pages

DBMS Microproject

Uploaded by

namdev misal
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/ 15

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION, MUMBAI.

A Project Report
On

“ TRIGGER AND CURSOR ”


In

Second Year Computer Engineering


Submitted by

MAST.SHUBHAM DATTA GAINGADE

MAST.DARSHAN VITTHAL SUTAR

MAST. OMKAR JAYWANT PATIL

MAST. NAMDEV EKNATH MISAL

UNDER THE GUIDANCE OF,

PROF. B. K. BIRANGADDI
SANT GAJANAN MAHARAJ RURAL POLYTECHNIC MAHAGAON

ACADEMIC YEAR

2023 – 2024

1
Maharashtra State Board of Technical Education, Mumbai.

“SANT GAJANAN MAHARAJ RURAL


POLYTECHNIC”
AP/-MAHAGAON, SITE- CHINCHEWADI, TAL-GADHINGLAJ, DIST – KOLHAPUR

CERTIFICATE
THIS IS TO CERTIFY THAT THE FOLLOWING STUDENTS OF SECOND YEAR.
THIRD SEMESTER OR DIPLOMA IN COMPUTER ENGINEERING OF
INSTITUTE SANT GAJANAN MAHARAJ RURAL POLYTECHNIC, MAHAGAON
– (CODE)416502 HAS COMPLETED MICRO PROJECT- DIGITAL ARITHMETIC
CIRCUIT– IN SUBJECT –DATABASE MANAGEMENT SYSTEM CODE- 22319
FOR ACADEMIC YEAR 2023-2024

AS PRESCRIBED IN THE CURRICULUM

ROLL STUDENT NAME ENROLLMENT NO. SEAT NO.


NO.
20 SHUBHAM DATTA
GAINGADE
21 DARSHAN VITTHAL
SUTAR
27 OMKAR JAYWANT
PATIL
69 NAMDEV EKNATH
MISAL

DATE: PLACE: MAHAGAON

PROF. G.K. BIRANGADDI PROF.G.K. BIRANGADDI PROF.R.S.PATIL

( SUBJECT ) ( HOD ) ( PRINCIPAL )

2
INDEX

SR. CONTENT PAGE

1 INTRODUCTION 4

2 TYPES OF TRIGGERS 5
● DDL 5
● DML 6
● LOGON 7

3 CURSOR 9

4 ADVANTAGES AND DISADVANTAGES 13


OF TRIGGERS

5 ADVANTAGES AND DISADVANTAGES


OF CURSOR 14

6 CONCLUSION AND REFERENCE 14

3
INTRODUCTION :

A trigger is a stored procedure in a database that automatically invokes whenever a special event in
the database occurs. For example, a trigger can be invoked when a row is inserted into a specified
table or when specific table columns are updated in simple words a trigger is a collection of SQL
statements with particular names that are stored in system memory. It belongs to a specific class of
stored procedures that are automatically invoked in response to database server events. Every
trigger has a table attached to it.

Because a trigger cannot be called directly, unlike a stored procedure, it is referred to as a special
procedure. A trigger is automatically called whenever a data modification event against a table takes
place, which is the main distinction between a trigger and a procedure. On the other hand, a stored
procedure must be called directly.

The following are the key differences between triggers and stored procedures:

1. Triggers cannot be manually invoked or executed.


2. There is no chance that triggers will receive parameters.
3. A transaction cannot be committed or rolled back inside a trigger.

A cursor in SQL Server is a database object that allows us to retrieve each row at a time and
manipulate its data. A cursor is nothing more than a pointer to a row. It's always used in
conjunction with a SELECT statement. It is usually a collection of SQL logic that loops
through a predetermined number of rows one by one. A simple illustration of the cursor is
when we have an extensive database of worker's records and want to calculate each
worker's salary after deducting taxes and leaves.
The SQL Server cursor's purpose is to update the data row by row, change it, or perform
calculations that are not possible when we retrieve all records at once. It's also useful for
performing administrative tasks like SQL Server database backups in sequential order.
Cursors are mainly used in the development, DBA, and ETL processes.
This article explains everything about SQL Server cursor, such as cursor life cycle, why and
when the cursor is used, how to implement cursors, its limitations, and how we can
replace a cursor.

4
● TYPES :

TRIGGER
1. DDL Trigger
2. DML Trigger
3. Logon Triggers

 DDL Triggers :-
The Data Definition Language (DDL) command events such as Create_table, Create_view,
drop_table, Drop_view, and Alter_table cause the DDL triggers to be activated.

SQL Server

create tigger safety

on database

for

create_table,alter_table,drop_table

as

print 'you can not create,drop and alter tab

Output:

5
 DML Triggers
The Data uses manipulation Language (DML) command events that begin with Insert,
Update, and Delete set off the DML triggers. corresponding to insert_table, update_view,
and delete_table.

SQL Server

create trigger deep

on emp

for

insert,update ,delete

as

print 'you can not insert,update and delete this table i'

rollback;

Output:

6
 Logon Triggers
Logon triggers are fires in response to a LOGON event. When a user session is created with a
SQL Server instance after the authentication process of logging is finished but before
establishing a user session, the LOGON event takes place. As a result, the PRINT statement
messages and any errors generated by the trigger will all be visible in the SQL Server error
log. Authentication errors prevent logon triggers from being used. These triggers can be
used to track login activity or set a limit on the number of sessions that a given login can
have in order to audit and manage server sessions.

How does SQL Server Show Trigger?

The show or list trigger is useful when we have many databases with many tables. This
query is very useful when the table names are the same across multiple databases. We can
view a list of every trigger available in the SQL Server by using the command below:

Syntax:

FROM sys.triggers, SELECT name, is_instead_of_trigger


IF type = ‘TR’;

The SQL Server Management Studio makes it very simple to display or list all triggers that
are available for any given table. The following steps will help us accomplish this:

Go to the Databases menu, select the desired database, and then expand it.

Select the Tables menu and expand it.

Select any specific table and expand it.

We will get various options here. When we choose the Triggers option, it displays all the
triggers available in this table.

BEFORE and AFTER Trigger

BEFORE triggers run the trigger action before the triggering statement is run. AFTER triggers
run the trigger action after the triggering statement is run.

Example
Given Student Report Database, in which student marks assessment is recorded. In such a
schema, create a trigger so that the total and percentage of specified marks are
automatically inserted whenever a record is inserted.

Here, a trigger will invoke before the record is inserted so BEFORE Tag can be used.

7
Suppose the Database Schema

Query

mysql>>desc Student;

SQL Trigger to the problem statement

8
○ CURSOR :-
There are 2 types of Cursors: Implicit Cursors, and Explicit Cursors. These
are explained as following below.
1. Implicit Cursors: Implicit Cursors are also known as Default Cursors of
SQL SERVER. These Cursors are allocated by SQL SERVER when the
user performs DML operations.
2. Explicit Cursors: Explicit Cursors are Created by Users whenever the
user requires them. Explicit Cursors are used for Fetching data from
Table in Row-By-Row Manner.

1. Declare Cursor Object

Syntax: DECLARE cursor_name CURSOR FOR SELECT * FROM table_name

Query: DECLARE s1 CURSOR FOR SELECT * FROM studDetails

2. Open Cursor Connection

Syntax: OPEN cursor_connection


Query: OPEN s1

Fetch Data from the Cursor There is a total of 6 methods to access data
from the cursor. They are as follows:
1. FIRST is used to fetch only the first row from the cursor table.
2. LAST is used to fetch only the last row from the cursor table.
3. NEXT is used to fetch data in a forward direction from the cursor table.
4. PRIOR is used to fetch data in a backward direction from the cursor
table.
5. ABSOLUTE n is used to fetch the exact n th row from the cursor table.
6. RELATIVE n is used to fetch the data in an incremental way as well as a
decremental way.
Syntax:
FETCH NEXT/FIRST/LAST/PRIOR/ABSOLUTE n/RELATIVE n FROM
cursor_name
Query:
FETCH FIRST FROM s1
FETCH LAST FROM s1
FETCH NEXT FROM s1
FETCH PRIOR FROM s1
FETCH ABSOLUTE 7 FROM s1
FETCH RELATIVE -2 FROM s1

9
 Close cursor connection

Syntax: CLOSE cursor_name


Query: CLOSE s1

● Deallocate cursor memory

Syntax: DEALLOCATE cursor_name


Query: DEALLOCATE s1

An implicit cursor is a cursor that is automatically created by PL/SQL when you


execute a SQL statement. You don’t need to declare or open an implicit cursor
explicitly. Instead, PL/SQL manages the cursor for you behind the scenes.
To create an implicit cursor in PL/SQL, you simply need to execute a SQL
statement. For example, to retrieve all rows from the EMP table, you can use the
following code:

Query:
BEGIN
FOR emp_rec IN SELECT * FROM emp LOOP
DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp_rec.ename);
END LOOP;
END;
In PL/SQL, when we perform INSERT, UPDATE or DELETE operations, an implicit
cursor is automatically created. This cursor holds the data to be inserted or
identifies the rows to be updated or deleted. You can refer to this cursor as the
SQL cursor in your code. Thi SQL cursor has several useful attributes.
1. %FOUND is true if the most recent SQL operation affected at least one row.
2. %NOTFOUND is true if it didn’t affect any rows.
3. %ROWCOUNT is returns the number of rows affected.
4. %ISOPEN checks if the cursor is open.
In addition to these attributes, %BULK_ROWCOUNT and %BULK_EXCEPTIONS
are specific to the FORALL statement, which is used to perform multiple DML
operations at once. %BULK_ROWCOUNT returns the number of rows affected by
each DML operation, while %BULK_EXCEPTION returns any exception that
occurred during the operations.

10
Query:
CREATE TABLE Emp(
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Salary int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Emp (EmpID, Name,Country, Age, Salary)
VALUES (1, 'Shubham', 'India','23','30000'),
(2, 'Aman ', 'Australia','21','45000'),
(3, 'Naveen', 'Sri lanka','24','40000'),
(4, 'Aditya', 'Austria','21','35000'),
(5, 'Nishant', 'Spain','22','25000');
Select * from Emp;

Output:

This program updates a table by increasing the salary of each employee by


1500. After the update, the SQL%ROWCOUNT attribute is used to find out
how many rows were affected by the operation.

11
Query:
DECLARE
total_rows number;
BEGIN
UPDATE Emp
SET Salary = Salary + 1500;
total_rows := SQL%ROWCOUNT;
dbms_output.put_line(total_rows || ' rows updated.');
END;

Output:

5 Emp selected
PL/SQL procedure successfully completed.

SQL Cursor Exceptions


Whenever we execute an SQL query then there is the possibility of an error
that is unexpected. Cursor goes through each set of rows to return in an SQL
query.
There are some very popular exceptions:
1. Duplicate Value: This type of error occur when the cursor tries to insert a
record or tuple which already exists in the database. these types of errors
can be avoided by handling proper error conf
2. Invalid Cursor State: Whenever the cursor is in an invalid state this type
of error will show as an error.
3. Lock Timeout: This occurs when the cursor tries to obtain a lock on a
row or table but the lock is already held by another transaction.

12
ADVANTAGES AND DISADVANTAGES :

○ Advantages of Trigger :

1. Database object rules are established by triggers, which cause changes to be undone
if they are not met.
2. The trigger will examine the data and, if necessary, make changes.
3. We can enforce data integrity thanks to triggers.
4. Data is validated using triggers before being inserted or updated.
5. Triggers assist us in maintaining a records log.
6. Due to the fact that they do not need to be compiled each time they are run, triggers
improve the performance of SQL queries.
7. The client-side code is reduced by triggers, saving time and labor.
8. Trigger maintenance is simple.

○ Disadvantages of Trigger :

1. Only triggers permit the use of extended validations.


2. Automatic triggers are used, and the user is unaware of when they are being
executed. Consequently, it is difficult to troubleshoot issues that arise in the
database layer.
3. The database server’s overhead may increase as a result of triggers.
4. In a single CREATE TRIGGER statement, we can specify the same trigger action for
multiple user actions, such as INSERT and UPDATE.
5. Only the current database is available for creating triggers, but they can still make
references to objects outside the database.

13
○ Advantages of CURSOR :

1. Using Cursor we can perform row by row processing so we can perform row wise
validation or operations on each row.
2. Cursors can provide the first few rows before the whole result set is assembled.
Without using cursors, the entire result set must be delivered before any rows are
displayed by the application. So using cursor, better response time is achieved.
3. If we make updates to our without using cursors in your application then we must
send separate SQL statements to the database server to apply the changes. This can
cause the possibility of concurrency problems if the result set has changed since it
was queried by the client. In turn, this raises the possibility of lost updates. So using
cursor, better concurrency Control can be achieved.
4. Cursors can be faster than a while loop but at the cost of more overhead.

○ Disadvantages of CURSOR :

1. Cursor in SQL is temporary work area created in the system memory, thus it occupies
memory from your system that may be available for other processes. So occupies
more resources and temporary storage.
2. Each time when a row is fetched from the cursor it may result in a network round
trip. This uses much more network bandwidth than the execution of a single SQL
statement like SELECT or DELETE etc that makes only one round trip.
3. Repeated network round trips can degrade the speed of the operation
using the cursor.

14
○ CONCLUSION :
Trigger in dbms are automatic actions that are executed in response to specific
database events. They can enforce business rules, validate data, or perform auditing tasks.
With the help of a cursor in SQL, we can perform queries on complex relational databases.
Since cursors help to process tables row-by-row and perform multiple calculations,
modifications to the data can be done easily.

○ REFERENCE :

 https://www.geeksforgeeks.org/sql-trigger-student-database/amp/
 https://www.geeksforgeeks.org/what-is-cursor-in-sql/

15

You might also like