Stored Procedures & Triggers Flashcards

Functions vs Procedures (Oracle PL/SQL, Postgres PL/pgSQL) Trigger types: BEFORE, AFTER, INSTEAD OF Writing procedures with control flow Exception handling in procedures Invoking from client code or SQL Security and performance considerations (30 cards)

1
Q

What is the difference between a function and a procedure in PL/SQL or PL/pgSQL?

A

Functions return a value and can be used in SQL statements, while procedures perform actions without returning a value and are invoked independently.

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

What are BEFORE triggers?

A

BEFORE triggers execute prior to an insert, update, or delete operation, often used for validation or transformation.

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

What are AFTER triggers?

A

AFTER triggers execute after the triggering statement, useful for logging, auditing, or chained operations.

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

What is an INSTEAD OF trigger?

A

INSTEAD OF triggers override the action on a view, enabling updates or inserts on views that normally can’t be modified.

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

How do you write control flow in procedures?

A

Control flow in procedures uses constructs like IF, CASE, LOOP, WHILE, and FOR to manage logic.

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

How is exception handling done in stored procedures?

A

Use BEGIN…EXCEPTION…END blocks in PL/pgSQL or EXCEPTION WHEN in PL/SQL to catch and handle errors.

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

How can procedures be invoked from SQL or client code?

A

Procedures can be called using CALL in SQL or invoked using database drivers from client applications.

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

What are security considerations for stored procedures?

A

They can encapsulate permissions, reducing direct access to tables, but care is needed with SQL injection risks.

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

What are performance advantages of stored procedures?

A

They reduce network traffic, enable logic reuse, and may benefit from pre-compilation by the database engine.

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

What are disadvantages of stored procedures?

A

They can lead to business logic duplication, be harder to version control, and create vendor lock-in.

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

What is a best practice for using triggers?

A

Use triggers sparingly for side-effects like auditing, and avoid complex logic to prevent performance bottlenecks.

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

When should functions be preferred over procedures?

A

When a computation is needed in a SELECT statement or to return a single value, use functions.

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

What’s a good use case for an INSTEAD OF trigger?

A

To allow inserts or updates on a view that combines multiple tables or filters out rows.

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

What’s the impact of stored procedures on system design?

A

They centralize business logic in the DB layer, reducing reliance on app logic but making debugging more complex.

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

How can stored procedures affect architectural decisions?

A

They may require teams to maintain more logic at the DB level, possibly affecting deployment strategies.

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

How do stored procedures improve fault tolerance?

A

They can enforce consistent logic and exception handling, reducing application-level inconsistencies.

17
Q

What’s a way to debug stored procedures?

A

Use RAISE NOTICE or DBMS_OUTPUT.PUT_LINE for debugging, or enable detailed error logging.

18
Q

What’s a performance tradeoff with triggers?

A

They add hidden execution cost and can cause cascading logic that’s hard to trace or optimize.

19
Q

What is a real-world tradeoff of using stored procedures?

A

They simplify client code but make the database layer heavier and harder to maintain with source control.

20
Q

What’s a common interview question about triggers?

A

Explain the difference between BEFORE and AFTER triggers, and provide a use case for each.

21
Q

What is a gotcha with AFTER triggers in PostgreSQL?

A

They do not fire if a BEFORE trigger cancels the operation, and their execution order isn’t guaranteed.

22
Q

What is a potential issue with using triggers extensively?

A

They can lead to non-transparent logic flow and unpredictable side effects if overused.

23
Q

What’s a best practice for exception handling in procedures?

A

Log meaningful error messages, avoid silent failures, and re-throw if needed for higher-level handling.

24
Q

What’s a limitation of using stored functions in queries?

A

They can’t modify data and may have limitations on transaction control depending on the DBMS.

25
What’s the role of grants in stored procedure security?
You can allow users to execute a procedure without granting direct table access, enforcing access control.
26
What’s an advantage of encapsulating logic in procedures?
Improves reuse, reduces duplication, and ensures consistent business rules across applications.
27
What’s the difference between PL/SQL and PL/pgSQL?
PL/SQL is Oracle’s procedural language extension; PL/pgSQL is PostgreSQL’s version with similar syntax and capabilities.
28
What’s a use case for BEFORE INSERT triggers?
To automatically populate fields like timestamps, audit info, or default values before data is inserted.
29
What does raising an exception in a trigger do?
It aborts the operation, which can be used for enforcing business rules and data integrity.
30
Why avoid complex logic in triggers?
They’re harder to debug, can slow down write operations, and may cause unintended side effects.