0% found this document useful (0 votes)
16 views5 pages

DBMS Cursors and Triggers Notes

The document provides detailed notes on Cursors and Triggers in PL/SQL, defining cursors as pointers for processing SQL query results and explaining their types (implicit and explicit). It also covers triggers as stored procedures that execute automatically in response to events, detailing their uses, types, and syntax. Examples and attributes related to both concepts are included to illustrate their functionality.

Uploaded by

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

DBMS Cursors and Triggers Notes

The document provides detailed notes on Cursors and Triggers in PL/SQL, defining cursors as pointers for processing SQL query results and explaining their types (implicit and explicit). It also covers triggers as stored procedures that execute automatically in response to events, detailing their uses, types, and syntax. Examples and attributes related to both concepts are included to illustrate their functionality.

Uploaded by

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

DBMS: Cursors and Triggers (Detailed Notes)

Cursors in PL/SQL

CURSORS IN DBMS (PL/SQL):

1. Definition:

- A cursor is a pointer to the context area that stores the result of a SQL query.

- Used to fetch and process rows returned by a query one at a time.

2. Types of Cursors:

a. Implicit Cursor:

- Automatically created by Oracle for single row queries (like SELECT INTO).

- Automatically managed.

- Example:

DECLARE

emp_name employees.name%TYPE;

BEGIN

SELECT name INTO emp_name FROM employees WHERE id = 101;

END;

b. Explicit Cursor:

- Declared and controlled explicitly by the programmer.

- Used for queries returning more than one row.

3. Explicit Cursor Syntax:


DBMS: Cursors and Triggers (Detailed Notes)

DECLARE

CURSOR cursor_name IS SELECT_statement;

BEGIN

OPEN cursor_name;

FETCH cursor_name INTO variable_list;

CLOSE cursor_name;

END;

4. Cursor FOR Loop:

- Automatically opens, fetches, and closes the cursor.

- Syntax:

FOR record IN cursor_name LOOP

-- statements

END LOOP;

5. Cursor Attributes:

- %FOUND: TRUE if fetch returned a row

- %NOTFOUND: TRUE if fetch did not return a row

- %ROWCOUNT: Number of rows fetched so far

- %ISOPEN: TRUE if the cursor is open

6. Example:

DECLARE

CURSOR emp_cursor IS SELECT name, salary FROM employees;

BEGIN
DBMS: Cursors and Triggers (Detailed Notes)

FOR emp IN emp_cursor LOOP

DBMS_OUTPUT.PUT_LINE(emp.name || ' earns ' || emp.salary);

END LOOP;

END;

Triggers in PL/SQL

TRIGGERS IN DBMS (PL/SQL):

1. Definition:

- A trigger is a stored procedure that executes automatically in response to certain events on a table/view.

2. Uses of Triggers:

- Auditing

- Enforcing business rules

- Preventing invalid transactions

- Automatically updating values

3. Types of Triggers:

a. Based on Timing:

- BEFORE Trigger: Executes before the operation.

- AFTER Trigger: Executes after the operation.

b. Based on Event:
DBMS: Cursors and Triggers (Detailed Notes)

- INSERT

- UPDATE

- DELETE

4. Syntax:

CREATE [OR REPLACE] TRIGGER trigger_name

{BEFORE | AFTER} {INSERT | UPDATE | DELETE}

ON table_name

[FOR EACH ROW]

BEGIN

-- PL/SQL code

END;

5. Row-Level vs Statement-Level:

- Row-Level: Executes for each row affected (uses FOR EACH ROW).

- Statement-Level: Executes once per triggering statement.

6. Example:

CREATE OR REPLACE TRIGGER trg_salary_check

BEFORE UPDATE OF salary ON employees

FOR EACH ROW

BEGIN

IF :NEW.salary < 0 THEN

RAISE_APPLICATION_ERROR(-20001, 'Salary cannot be negative');

END IF;
DBMS: Cursors and Triggers (Detailed Notes)

END;

7. Trigger Restrictions:

- Can't use COMMIT, ROLLBACK, or SAVEPOINT inside a trigger.

You might also like