Ask Your Question
4

How can a SQL trigger be used to prevent a user from modifying the salary of an accountant?

asked 2023-07-12 21:04:12 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-12 21:16:02 +0000

lalupa gravatar image

A SQL trigger can be used to prevent a user from modifying the salary of an accountant by creating a trigger that is set to execute before the update statement is processed on the salary column in the accountant table. The trigger would check the user's role or status and the updated salary value, and if the user's role is not authorized to modify the salary of an accountant or the updated salary value exceeds a certain amount, the trigger would raise an error and prevent the update statement from being executed.

For example, the following SQL code creates a trigger that prevents users with the role 'manager' from modifying the salary of an accountant to a value greater than 100,000:

CREATE TRIGGER preventsalaryupdate BEFORE UPDATE ON accountant FOR EACH ROW BEGIN IF USER() = 'manager' AND NEW.salary > 100000 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Managers are not authorized to update the salary of an accountant to a value greater than 100,000'; END IF; END;

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-12 21:04:12 +0000

Seen: 11 times

Last updated: Jul 12 '23