DBMS Microproject
DBMS Microproject
A Project Report
On
PROF. B. K. BIRANGADDI
SANT GAJANAN MAHARAJ RURAL POLYTECHNIC MAHAGAON
ACADEMIC YEAR
2023 – 2024
1
Maharashtra State Board of Technical Education, Mumbai.
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
2
INDEX
1 INTRODUCTION 4
2 TYPES OF TRIGGERS 5
● DDL 5
● DML 6
● LOGON 7
3 CURSOR 9
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:
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
on database
for
create_table,alter_table,drop_table
as
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
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.
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:
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.
We will get various options here. When we choose the Triggers option, it displays all the
triggers available in this table.
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;
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.
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
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:
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.
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 :
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