programming beginner 10 sample questions

Stored Procedures & Triggers MCQ Practice Test

Predefined SQL code (procedures) and automatic actions (triggers) executed on events.

Q1. A stored procedure and a trigger are both database objects used to perform specific tasks. What is the primary difference in how they are executed?

Explanation: Stored procedures are executed explicitly by calling them, whereas triggers are automatically activated by database events (e.g., INSERT, UPDATE, DELETE operations).

Q2. A stored procedure is created to update a customer's address. The procedure checks if the customer's ID exists in the database. If it does, it updates the address. If not, it inserts a new record with the customer's ID and the updated address. Which of the following statements best describes the purpose of a trigger in this scenario?

Explanation: A trigger is a database object that automatically executes a set of actions in response to certain events, such as insert, update, or delete operations. In this scenario, a trigger could be used to update dependent tables, such as the customer's order history, when the customer's address is updated.

Q3. Which trigger timing lets you validate or block an operation before it happens?

Explanation: BEFORE triggers run prior to the change, so they can validate input or modify the new values; the original options mixed timing and granularity confusingly.

Q4. Which of the following is a characteristic of a stored procedure in a database?

Explanation: A stored procedure is a set of SQL statements that can be executed multiple times without recompilation, making it more efficient and improving database performance.

Q5. A stored procedure is created to update the salary of employees in a table. Which of the following statements is true about the stored procedure?

Explanation: A stored procedure is a set of SQL statements that can be executed multiple times from an application, and it can be created by any user with the necessary permissions. It can be executed from any application that has a connection to the database, making it a reusable piece of code.

Q6. A database contains a table with a column named "salary" and another column named "bonus". A trigger is created to update the "salary" column whenever the "bonus" column is updated. The trigger is written in SQL and uses the "UPDATE" statement. Which of the following statements about the trigger is correct?

Explanation: A trigger in SQL will fire when the specified action occurs, regardless of the method used to cause the action. In this case, the trigger is set up to fire when the "bonus" column is updated, which includes direct updates, updates through views, or updates through stored procedures.

Q7. A stored procedure is created to update a table with a new salary for employees. The procedure uses a trigger to   insert a new record into a separate audit table. What is the purpose of the trigger in this scenario?

Explanation: The trigger is used to insert a new record into the audit table, which allows for tracking of all changes made to the salary table. This is an example of an auditing mechanism, which is a common use case for triggers.

Q8. A stored procedure is called from a trigger to perform an action on a table. What is the primary reason for using a transaction in this scenario?

Explanation: When a stored procedure is called from a trigger, it's essential to use a transaction to ensure data consistency. This is because the trigger may modify multiple rows, and a transaction allows you to roll back changes in case of an error or inconsistency.

Q9. A stored procedure is called from a trigger to perform an action. Which of the following statements is true about the execution of the stored procedure?

Explanation: A stored procedure called from a trigger will be executed once for each row affected by the trigger. This is because triggers are typically row-level triggers, and they fire once for each row that meets the trigger condition.

Q10. In a database, what is the primary purpose of a trigger in relation to a stored procedure that updates a table?

Explanation: A trigger in a database is primarily used to enforce data integrity by validating changes made by a stored procedure or other database operations. It can also be used to perform additional actions before or after the operation, such as logging changes to an audit table. However, its primary purpose is to ensure that the data being modified conforms to the defined rules and constraints.

That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.

Sign Up Free
Stored Procedures & Triggers MCQ Practice Test | Mind Hustle