6. Stored Procedures, Functions and Triggers Flashcards

(6 cards)

1
Q

Purpose:

A

Extend SQL with programming capabilities (control structures, error handling) and store code within the database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Advantages:

A

Reduce network traffic, centralise business logic, enhance security.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Disadvantages:

A

Increased memory usage, difficult to debug, requires specialised maintenance skills.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Stored Procedures:

A

Do not return a value (like void in Java). Executed using CALL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Stored Functions:

A

Return a value (like in C-style languages). Can be used anywhere an expression is allowed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Triggers:

A

A special type of stored routine automatically invoked by the DBMS in response to specified database changes. Unlike procedures/functions, they are event-driven.
◦ Event-Action Model: Trigger runs in response to an Event (INSERT, UPDATE, DELETE, TRUNCATE on a table). The Action is the trigger body (SQL or stored code).
◦ Triggering Time: BEFORE, AFTER, or INSTEAD OF the triggering statement.
Trigger Type:
▪ Row-level: Activated once for each row affected by the statement. Can access OLD and NEW row versions. OLD and NEW are implicit record variables; use OLD.col_name or NEW.col_name. OLD is state before, NEW is state after; applicable rows/columns vary by event (UPDATE, DELETE, INSERT).
▪ Statement-level: Activated once per statement, regardless of the number of rows affected (even zero). Cannot access OLD or NEW.
◦ Uses: Enforce complex business rules, create audit logs, maintain derived attributes, maintain archive tables. BEFORE triggers can perform complex validation or modify values before changes are written. AFTER triggers modify related data in other tables after changes are committed. INSTEAD OF triggers on views allow complex updates on non-updatable views.
◦ Issues: Invisible automatic execution, hard to troubleshoot/model performance, may increase overhead. Avoid for simple constraints. Beware of recursive triggers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly