Knowee
Questions
Features
Study Tools

Exercise Questions(a) Create a trigger, which verify that updated salary of employee must begreater than his/her previous salary(b) Create a trigger, which verify that for the department 2, the number ofemployees should not exceed than 5.

Question

Exercise Questions(a) Create a trigger, which verify that updated salary of employee must begreater than his/her previous salary(b) Create a trigger, which verify that for the department 2, the number ofemployees should not exceed than 5.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here are the SQL commands to create the triggers you asked for:

(a) To create a trigger that verifies that the updated salary of an employee must be greater than his/her previous salary, you can use the following SQL command:

CREATE TRIGGER check_salary_increase
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
   IF NEW.salary <= OLD.salary THEN
      SIGNAL SQLSTATE '45000'
         SET MESSAGE_TEXT = 'Error: Updated salary must be greater than the previous salary.';
   END IF;
END;

This trigger is activated before an update on the employees table. It compares the new salary (NEW.salary) with the old salary (OLD.salary). If the new salary is less than or equal to the old salary, it raises an error.

(b) To create a trigger that verifies that for the department 2, the number of employees should not exceed 5, you can use the following SQL command:

CREATE TRIGGER check_dept2_employee_count
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
   IF NEW.department_id = 2 AND 
      (SELECT COUNT(*) FROM employees WHERE department_id = 2) >= 5 THEN
      SIGNAL SQLSTATE '45000'
         SET MESSAGE_TEXT = 'Error: Number of employees in department 2 should not exceed 5.';
   END IF;
END;

This trigger is activated before an insert on the employees table. It checks if the new employee is being assigned to department 2 (NEW.department_id = 2) and if the number of employees in department 2 is already 5 or more. If both conditions are true, it raises an error.

Please note that these triggers are written in MySQL syntax and the SIGNAL statement is used to raise an error. The exact syntax might vary depending on your SQL database system.

This problem has been solved

Similar Questions

For the Employee Class Program in this tutorial, what if we need the capability to update the salary for employees? What information is needed to perform this task, and what code segment implements obtaining it in a method?a.)Employees file information and the new salarydef upd_salary(): employees = read_employees() new_sal = input("Enter the new salary of the employee: ")b.)The new salarydef upd_salary(employees): new_sal = input("Enter the new salary of the employee: ")c.)Employee iddef upd_salary(employees): empid = input("Enter the employee ID: ")d.)Employee id and the new salarydef upd_salary(employees): empid = input("Enter the employee ID: ") new_sal = input("Enter the new salary of the employee: ")

Problem statementSend feedbackSuppose you have a list of employees in a company, and each employee is represented as an object with the following properties:"id" - a unique identifier for the employee"name" - the employee's name"department" - the department the employee works in"salary" - the employee's current salaryObjectives:You want to make a modification to the list of employees to reflect a recent company-wide salary increase. You have been provided with the following information:Salaries for employees in the "sales" department should be increased by 10%.Salaries for employees in the "engineering" department should be increased by 15%.Salaries for employees in all other departments should be increased by 5%.You need to write a function "increaseSalaries" that takes an array of employee objects and returns a new array of employee objects where each employee's salary has been updated based on their department. The original array of employees should be left unchanged.

(b) Skriv ett if-sats som ökar lönen med 5 % om den är större än 100, annars höj den med 10 %.(Write an if statement that increases salary by 5% if it is greater than 100, otherwise increaseit by 10%.)

Write SQL QueriesEmployee TableDepartment Table1. Get the list of employees whose salary is more than 20K and are in HR department.2. Get the salary being paid to each Department.3. Get the employees with the third highest salary.4. Name and designation of the employees who don't have any manager

payraiseProblem DescriptionA company has N employees, numbered 0 to N-1. The boss of the company is employee 0, while every other employee i has a direct superior Pi. Since employee 0 has no direct superior, P0 = -1. Each employee i starts with an initial salary of Si dollars.There will be a total of Q operations that you have to handle, each of them being one of two types:A raise operation, where we raise the salaries of employee x and all of their direct and indirect subordinates by d dollars.A query operation, where we want to know the current salary of employee x.InputThe first line of input will contain two integers, N and Q.The next line of input will contain N integers, indicating the array P.The next line of input will contain N integers, indicating the array S.The next Q lines of input will describe one operation each, in the following formats:0 x d, indicating a raise operation on employee x (and their subordinates) by d dollars.1 x, indicating a query operation on employee x.OutputThe output should contain one line with one integer for each query operation, indicating the current salary of the requested employee.Limits1 ≤ N, Q ≤ 106.0 ≤ Pi < i, for all 1 ≤ i < N.0 ≤ x < N0 ≤ d, Si ≤ 109.Sample Input5 5-1 0 0 1 11 3 2 3 20 1 31 40 0 21 11 3Sample Output588

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.