Auto commit
This commit is contained in:
parent
da1fcd8e75
commit
4cf78065f1
296
2207/存储过程练习题.md
296
2207/存储过程练习题.md
@ -151,14 +151,14 @@ INSERT INTO employee (emp_name, salary, hire_date, dept_id, job_id) VALUES
|
|||||||
```sql
|
```sql
|
||||||
DELIMITER $$
|
DELIMITER $$
|
||||||
DROP PROCEDURE IF EXISTS add_employee;
|
DROP PROCEDURE IF EXISTS add_employee;
|
||||||
CREATE PROCEDURE add_employee(IN `name` VARCHAR(100), IN salary DECIMAL(10, 2), IN hire_date DATE, IN dept_id INT, IN job_id INT)
|
CREATE PROCEDURE add_employee(IN emp_name VARCHAR(100), IN salary DECIMAL(10,2), IN hire_date DATE, IN dept_id INT, IN job_id INT)
|
||||||
BEGIN
|
BEGIN
|
||||||
INSERT INTO employee()
|
INSERT INTO employee()
|
||||||
VALUES(DEFAULT, `name`, salary, hire_date, dept_id, job_id);
|
VALUES(DEFAULT, emp_name, salary, hire_date, dept_id, job_id);
|
||||||
END $$
|
END $$
|
||||||
DELIMTIER ;
|
DELIMITER ;
|
||||||
|
|
||||||
CALL add_employee('拼好饭', 3000, NOW(), 8, 3);
|
CALL add_employee('李泽龙', 6666, '2025-3-20', 1, 1);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
@ -167,75 +167,363 @@ CALL add_employee('拼好饭', 3000, NOW(), 8, 3);
|
|||||||
|
|
||||||
创建一个存储过程 `update_employee_salary`,接受员工ID和新的工资作为参数,更新该员工的工资。
|
创建一个存储过程 `update_employee_salary`,接受员工ID和新的工资作为参数,更新该员工的工资。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS update_employee_salary;
|
||||||
|
CREATE PROCEDURE update_employee_salary(IN emp_id INT, IN salary DECIMAL(10, 2))
|
||||||
|
BEGIN
|
||||||
|
UPDATE employee e
|
||||||
|
SET e.salary = salary
|
||||||
|
WHERE e.emp_id = emp_id;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL update_employee_salary(1, 60000);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 3: 删除员工
|
#### 练习题 3: 删除员工
|
||||||
|
|
||||||
创建一个存储过程 `delete_employee`,接受员工ID作为参数,删除该员工的记录。
|
创建一个存储过程 `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: 查询部门员工
|
#### 练习题 4: 查询部门员工
|
||||||
|
|
||||||
创建一个存储过程 `get_department_employees`,接受部门ID作为参数,返回该部门所有员工的详细信息。
|
创建一个存储过程 `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: 查询职位员工
|
#### 练习题 5: 查询职位员工
|
||||||
|
|
||||||
创建一个存储过程 `get_job_employees`,接受职位ID作为参数,返回该职位所有员工的详细信息。
|
创建一个存储过程 `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: 计算部门平均工资
|
#### 练习题 6: 计算部门平均工资
|
||||||
|
|
||||||
创建一个存储过程 `calculate_avg_salary_by_dept`,接受部门ID作为参数,返回该部门的平均工资。
|
创建一个存储过程 `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: 计算职位平均工资
|
#### 练习题 7: 计算职位平均工资
|
||||||
|
|
||||||
创建一个存储过程 `calculate_avg_salary_by_job`,接受职位ID作为参数,返回该职位的平均工资。
|
创建一个存储过程 `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: 查询高薪员工
|
#### 练习题 8: 查询高薪员工
|
||||||
|
|
||||||
创建一个存储过程 `get_high_salary_employees`,接受一个工资阈值作为参数,返回所有工资高于该阈值的员工。
|
创建一个存储过程 `get_high_salary_employees`,接受一个工资阈值作为参数,返回所有工资高于该阈值的员工。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_high_salary_employees;
|
||||||
|
CREATE PROCEDURE get_high_salary_employees(IN sal DECIMAL(10, 2))
|
||||||
|
BEGIN
|
||||||
|
SELECT e.*
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.salary > sal;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_high_salary_employees(50000);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 9: 查询员工工龄
|
#### 练习题 9: 查询员工工龄
|
||||||
|
|
||||||
创建一个存储过程 `get_employee_years_of_service`,接受员工ID作为参数,返回该员工的工龄(以年为单位)。
|
创建一个存储过程 `get_employee_years_of_service`,接受员工ID作为参数,返回该员工的工龄(以年为单位)。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_employee_years_of_service;
|
||||||
|
CREATE PROCEDURE get_employee_years_of_service(IN id INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT -TIMESTAMPDIFF(YEAR,NOW(),e.hire_date)
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.emp_id = id;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_employee_years_of_service(1);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 10: 查询部门员工总数
|
#### 练习题 10: 查询部门员工总数
|
||||||
|
|
||||||
创建一个存储过程 `get_department_employee_count`,接受部门ID作为参数,返回该部门的员工总数。
|
创建一个存储过程 `get_department_employee_count`,接受部门ID作为参数,返回该部门的员工总数。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_department_employee_count;
|
||||||
|
CREATE PROCEDURE get_department_employee_count(IN id INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.dept_id = id;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_department_employee_count(1);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 11: 查询职位员工总数
|
#### 练习题 11: 查询职位员工总数
|
||||||
|
|
||||||
创建一个存储过程 `get_job_employee_count`,接受职位ID作为参数,返回该职位的员工总数。
|
创建一个存储过程 `get_job_employee_count`,接受职位ID作为参数,返回该职位的员工总数。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_job_employee_count;
|
||||||
|
CREATE PROCEDURE get_job_employee_count(IN id INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.job_id = id;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_job_employee_count(1);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 12: 查询员工详细信息
|
#### 练习题 12: 查询员工详细信息
|
||||||
|
|
||||||
创建一个存储过程 `get_employee_details`,接受员工ID作为参数,返回该员工的姓名、工资、部门名称和职位名称。
|
创建一个存储过程 `get_employee_details`,接受员工ID作为参数,返回该员工的姓名、工资、部门名称和职位名称。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_employee_details;
|
||||||
|
CREATE PROCEDURE get_employee_details(IN emp_id INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT e.emp_name, e.salary, d.dept_name, j.job_title
|
||||||
|
FROM employee e, department d, job j
|
||||||
|
WHERE e.emp_id = emp_id AND e.dept_id = d.dept_id AND e.job_id = j.job_id;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_employee_details(1);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 13: 查询部门最高工资
|
#### 练习题 13: 查询部门最高工资
|
||||||
|
|
||||||
创建一个存储过程 `get_department_max_salary`,接受部门ID作为参数,返回该部门的最高工资。
|
创建一个存储过程 `get_department_max_salary`,接受部门ID作为参数,返回该部门的最高工资。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_department_max_salary;
|
||||||
|
CREATE PROCEDURE get_department_max_salary(IN id INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT MAX(e.salary)
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.dept_id = id;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_department_max_salary(1);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 14: 查询职位最低工资
|
#### 练习题 14: 查询职位最低工资
|
||||||
|
|
||||||
创建一个存储过程 `get_job_min_salary`,接受职位ID作为参数,返回该职位的最低工资。
|
创建一个存储过程 `get_job_min_salary`,接受职位ID作为参数,返回该职位的最低工资。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_job_min_salary;
|
||||||
|
CREATE PROCEDURE get_job_min_salary(IN id INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT MIN(e.salary)
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.dept_id = id;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_job_min_salary(1);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 15: 查询员工工资排名
|
#### 练习题 15: 查询员工工资排名
|
||||||
|
|
||||||
创建一个存储过程 `get_employee_salary_rank`,接受员工ID作为参数,返回该员工在公司中的工资排名。
|
创建一个存储过程 `get_employee_salary_rank`,接受员工ID作为参数,返回该员工在公司中的工资排名。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_employee_salary_rank;
|
||||||
|
CREATE PROCEDURE get_employee_salary_rank(IN emp_id INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT COUNT(*) + 1
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.salary > (
|
||||||
|
SELECT e.salary
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.emp_id = emp_id
|
||||||
|
);
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
|
||||||
|
CALL get_employee_salary_rank(2);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 16: 查询部门工资总和
|
#### 练习题 16: 查询部门工资总和
|
||||||
|
|
||||||
创建一个存储过程 `get_department_total_salary`,接受部门ID作为参数,返回该部门所有员工的工资总和。
|
创建一个存储过程 `get_department_total_salary`,接受部门ID作为参数,返回该部门所有员工的工资总和。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_department_total_salary;
|
||||||
|
CREATE PROCEDURE get_department_total_salary(IN id INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT SUM(e.salary)
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.dept_id = id;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_department_total_salary(1);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 17: 查询职位工资总和
|
#### 练习题 17: 查询职位工资总和
|
||||||
|
|
||||||
创建一个存储过程 `get_job_total_salary`,接受职位ID作为参数,返回该职位所有员工的工资总和。
|
创建一个存储过程 `get_job_total_salary`,接受职位ID作为参数,返回该职位所有员工的工资总和。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_job_total_salary;
|
||||||
|
CREATE PROCEDURE get_job_total_salary(IN id INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT SUM(e.salary)
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.job_id = id;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_job_total_salary(1);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 18: 查询员工入职年份分布
|
#### 练习题 18: 查询员工入职年份分布
|
||||||
|
|
||||||
创建一个存储过程 `get_employees_by_hire_year`,接受年份作为参数,返回该年份入职的所有员工。
|
创建一个存储过程 `get_employees_by_hire_year`,接受年份作为参数,返回该年份入职的所有员工。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_employees_by_hire_year;
|
||||||
|
CREATE PROCEDURE get_employees_by_hire_year(IN y INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT *
|
||||||
|
FROM employee e
|
||||||
|
WHERE YEAR(e.hire_date) = y;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_employees_by_hire_year(2020);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 19: 查询部门工资分布
|
#### 练习题 19: 查询部门工资分布
|
||||||
|
|
||||||
创建一个存储过程 `get_department_salary_distribution`,接受部门ID作为参数,返回该部门工资的分布情况(如最高、最低、平均工资)。
|
创建一个存储过程 `get_department_salary_distribution`,接受部门ID作为参数,返回该部门工资的分布情况(如最高、最低、平均工资)。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_department_salary_distribution;
|
||||||
|
CREATE PROCEDURE get_department_salary_distribution(IN id INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT MAX(e.salary), MIN(e.salary), AVG(e.salary)
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.dept_id = id;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_department_salary_distribution(1);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### 练习题 20: 查询职位工资分布
|
#### 练习题 20: 查询职位工资分布
|
||||||
|
|
||||||
创建一个存储过程 `get_job_salary_distribution`,接受职位ID作为参数,返回该职位工资的分布情况(如最高、最低、平均工资)。
|
创建一个存储过程 `get_job_salary_distribution`,接受职位ID作为参数,返回该职位工资的分布情况(如最高、最低、平均工资)。
|
||||||
|
|
||||||
|
```sql
|
||||||
|
DELIMITER $$
|
||||||
|
DROP PROCEDURE IF EXISTS get_job_salary_distribution;
|
||||||
|
CREATE PROCEDURE get_job_salary_distribution(IN id INT)
|
||||||
|
BEGIN
|
||||||
|
SELECT MAX(e.salary), MIN(e.salary), AVG(e.salary)
|
||||||
|
FROM employee e
|
||||||
|
WHERE e.job_id = id;
|
||||||
|
END $$
|
||||||
|
DELIMITER ;
|
||||||
|
CALL get_job_salary_distribution(1);
|
||||||
|
```
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user