Câu hỏi phỏng vấn MySQL
Câu hỏi

What is Stored Function in MySQL. How are they different from Stored Procedure?

Câu trả lời

In MySQL, a stored function is a set of SQL statements that perform a specific task and return a single value. Stored functions are similar to procedures in that they are both stored on the server, can be invoked multiple times, and can encapsulate complex logic. However, there are several key differences between stored functions and stored procedures[1][3][7][8][9][11][13][15][18][19].

Key Differences Between Stored Functions and Stored Procedures

  1. Return Value:

    • Stored Functions: Must return exactly one value using the RETURN statement. This value can be of any data type that MySQL supports[1][3][7][8][11][15].
    • Stored Procedures: Do not need to return a value, but they can return multiple values through OUT parameters or result sets[1][3][8][9][13].
  2. Invocation:

    • Stored Functions: Can be invoked from within an SQL statement just like built-in functions. For example, they can be used in the SELECT list, WHERE clause, or any place where an expression is valid[1][3][7][11][15].
    • Stored Procedures: Must be invoked using the CALL statement and cannot be used directly within SQL statements like SELECT or WHERE clauses[1][3][8][9][13].
  3. Parameter Passing:

    • Stored Functions: Only IN parameters are allowed. Functions treat all parameters as input values[1][3][7][11][15].
    • Stored Procedures: Can have IN, OUT, and INOUT parameters, allowing them to pass values back to the caller or modify the passed arguments[1][3][8][9][13].
  4. Usage in SQL Statements:

    • Stored Functions: Due to their ability to return a value, functions can be used anywhere in SQL statements where expressions are allowed[1][3][7][11][15].
    • Stored Procedures: Cannot be embedded in SQL statements and are used for performing operations that typically involve more complex processing or need to return multiple result sets[1][3][8][9][13].
  5. Impact on Database State:

    • Stored Functions: Are not allowed to modify the database state. This means they cannot execute statements that perform updates, deletes, or inserts on database tables[1][3][7][11][15].
    • Stored Procedures: Can modify the database state and include SQL statements that change data within the database[1][3][8][9][13].
  6. Error Handling:
    ...

middle

middle

Gợi ý câu hỏi phỏng vấn

junior

What is an AGGREGATE function. Name few aggregate functions used in MySQL.

junior

How are VARCHAR and CHAR different. Talk about cases where you will use one over other.

junior

What is the difference between Data Definition Language (DDL) and Data Manipulation Language
(DML)?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào