SQL MISC 2
SQL MISC 2
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
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.
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.
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