Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
When creating stored functions in MySQL, you are required to declare their behavioral characteristics: DETERMINISTIC or NOT DETERMINISTIC.
Declaring this correctly impacts both Query Optimization and Replication Safety.
A function is Deterministic if it always produces the exact same output given the exact same input parameters, with zero side effects.
The query optimizer knows that for a constant input, the result never changes. It can evaluate the function once and cache the result for the duration of the query!
A function is Non-Deterministic if its output can change between consecutive calls even when supplied with the identical input parameters.
A function is inherently non-deterministic if it references:
NOW(), CURRENT_DATE(), UNIX_TIMESTAMP())RAND())SELECT balance FROM accounts)CONNECTION_ID(), USER())Because CURRENT_DATE() changes as time elapses, this function is strictly NOT DETERMINISTIC.
When MySQL replication is enabled (statement-based logging):
DETERMINISTIC actually uses NOW() or reads unstable data, the master and replica databases will execute the function at slightly different times, producing divergent data between master and slave nodes!| Clause | Meaning |
|---|---|
DETERMINISTIC | Same inputs always return same output. |
NOT DETERMINISTIC | Output may vary for same inputs. |
CONTAINS SQL | Routine contains SQL statements but does not read or write data. |
NO SQL | Routine contains no SQL statements (pure math/logic). |
READS SQL DATA | Routine queries database tables using SELECT. |
MODIFIES SQL DATA | Routine writes to tables via INSERT, UPDATE, or DELETE. |
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Creating User-Defined Functions (CREATE FUNCTION) | Working with Cursors: Declare, Open, Fetch, Close |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.