0% found this document useful (0 votes)
20 views

SQL MISC 2

Uploaded by

Emerald Guppy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

SQL MISC 2

Uploaded by

Emerald Guppy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

/* Cursor is a Temporary Memory or Temporary Work Station.

It is
Allocated by Database Server at the Time of Performing DML(Data
Manipulation Language) operations on the Table by the User. Cursors
are used to store Database Tables.
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.
*/

--SYNTAX
DECLARE cursor_name CURSOR FOR SELECT * FROM table_name
OPEN cursor_connection
FETCH NEXT/FIRST/LAST/PRIOR/ABSOLUTE n/RELATIVE n FROM
cursor_name
CLOSE cursor_name
DEALLOCATE cursor_name

/*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. */

DECLARE s1 CURSOR FOR SELECT * FROM studDetails


OPEN s1
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
CLOSE s1
DEALLOCATE s1

USES of Cursor:
1. Cursors allow us to process data row-by-row, which can be
useful when we need to perform complex calculations or
transformations on the data.
2. Cursors allow us to iterate over a result set multiple times,
which can be useful when we need to perform multiple
operations on the same data.
3. Cursors can be useful when we need to join multiple tables
with complex relationships, such as when processing
hierarchical data structures or when performing recursive
queries.
4. Cursors allow us to perform operations such as updating,
deleting, or inserting records based on some condition or
criteria.
5. Cursors are especially useful when processing data from
multiple tables where the relationships are not
straightforward.
SQL Server Cursor Limitations
As a cursor has some limitations, it should only be used when there is
no other choice. These restrictions include:
1. When processing data, it imposes locks on a subset or the
entire table.
2. The cursor updates table records one row at a time, which
slows down its performance.
3. While loops are slower than cursors, they do have more
overhead.
4. Another factor that influences cursor speed is the quantity of
rows and columns brought into the cursor.
SQL TRIGGERS:
Trigger is a statement that a system executes automatically when
there is any modification to the database. In a trigger, we first specify
when the trigger is to be executed and then the action to be
performed when the trigger executes

TYPES OF TRIGGERS
1. AFTER INSERT activated after data is inserted into the
table.

2. AFTER UPDATE: activated after data in the table is


modified.

3. AFTER DELETE: activated after data is deleted/removed


from the table.

4. BEFORE INSERT: activated before data is inserted into the


table.

5. BEFORE UPDATE: activated before data in the table is


modified.

6. BEFORE DELETE: activated before data is deleted/removed


from the table.

delimiter $$
CREATE TRIGGER Check_age BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
IF NEW.age < 25 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ERROR:
AGE MUST BE ATLEAST 25 YEARS!';
END IF;
END; $$
delimiter;
we are adding a tuple to the ‘Donors’ table that is some person has
donated blood. So, we can design a trigger that will automatically add
the value of donated blood to the ‘Blood_record’ table.

Create a trigger which will work before deletion in employee


table and create a duplicate copy of the record in another
table employee_backup.
create table employee_backup (employee_no int,
employee_name varchar(40), job varchar(40),
hiredate date, salary int,
primary key(employee_no));

delimiter $$
CREATE TRIGGER Backup BEFORE DELETE ON employee
FOR EACH ROW
BEGIN
INSERT INTO employee_backup
VALUES (OLD.employee_no, OLD.name,
OLD.job, OLD.hiredate, OLD.salary);
END; $$
delimiter;
3. Write a trigger to count number of new tuples inserted
using each insert statement.
Declare count int
Set count=0;
delimiter $$
CREATE TRIGGER Count_tupples
AFTER INSERT ON employee
FOR EACH ROW
BEGIN
SET count = count + 1;
END; $$
delimiter

You might also like