diff --git a/2208/存储过程练习题.md b/2208/存储过程练习题.md index aa651a7..6e714f0 100644 --- a/2208/存储过程练习题.md +++ b/2208/存储过程练习题.md @@ -186,22 +186,97 @@ CALL update_employee_salary(1, 60000); 创建一个存储过程 `delete_employee`,接受员工ID作为参数,删除该员工的记录。 +```sql +DELIMITER $$ +DROP PROCEDURE IF EXISTS delete_employee; +CREATE PROCEDURE delete_employee(IN id INT) +BEGIN + DELETE FROM employee + WHERE emp_id = id; +END $$ +DELIMITER ; + +CALL delete_employee(1); +``` + + + #### 练习题 4: 查询部门员工 创建一个存储过程 `get_department_employees`,接受部门ID作为参数,返回该部门所有员工的详细信息。 +```sql +DELIMITER $$ +DROP PROCEDURE IF EXISTS get_department_employees; +CREATE PROCEDURE get_department_employees(IN id INT) +BEGIN + SELECT e.* + FROM employee e + WHERE e.dept_id = id; +END $$ +DELIMITER ; +CALL get_department_employees(1); +``` + + + #### 练习题 5: 查询职位员工 创建一个存储过程 `get_job_employees`,接受职位ID作为参数,返回该职位所有员工的详细信息。 +```sql +DELIMITER $$ +DROP PROCEDURE IF EXISTS get_job_employees; +CREATE PROCEDURE get_job_employees(IN id INT) +BEGIN + SELECT e.* + FROM employee e + WHERE e.job_id = id; +END $$ +DELIMITER ; +CALL get_job_employees(1); +``` + + + #### 练习题 6: 计算部门平均工资 创建一个存储过程 `calculate_avg_salary_by_dept`,接受部门ID作为参数,返回该部门的平均工资。 +```sql +DELIMITER $$ +DROP PROCEDURE IF EXISTS calculate_avg_salary_by_dept; +CREATE PROCEDURE calculate_avg_salary_by_dept(IN id INT) +BEGIN + SELECT AVG(e.salary) + FROM employee e + WHERE e.dept_id = id; +END $$ +DELIMITER ; +CALL calculate_avg_salary_by_dept(1); +``` + + + #### 练习题 7: 计算职位平均工资 创建一个存储过程 `calculate_avg_salary_by_job`,接受职位ID作为参数,返回该职位的平均工资。 +```sql +DELIMITER $$ +DROP PROCEDURE IF EXISTS calculate_avg_salary_by_job; +CREATE PROCEDURE calculate_avg_salary_by_job(IN id INT) +BEGIN + SELECT AVG(e.salary) + FROM employee e + WHERE e.job_id = id; +END $$ +DELIMITER ; +CALL calculate_avg_salary_by_job(1); +``` + + + #### 练习题 8: 查询高薪员工 创建一个存储过程 `get_high_salary_employees`,接受一个工资阈值作为参数,返回所有工资高于该阈值的员工。