use employees;delimiter //drop function if exists emp_dept_id //drop function if exists emp_dept_name //drop function if exists emp_name //drop function if exists current_manager //drop procedure if exists show_departments //---- returns the department id of a given employee--create function emp_dept_id( employee_id int )returns char(4)reads sql databegindeclare max_date date;set max_date = (selectmax(from_date)fromdept_empwhereemp_no = employee_id);set @max_date=max_date;return (selectdept_nofromdept_empwhereemp_no = employee_idandfrom_date = max_datelimit 1);end //---- returns the department name of a given employee--create function emp_dept_name( employee_id int )returns varchar(40)reads sql databeginreturn (selectdept_namefromdepartmentswheredept_no = emp_dept_id(employee_id));end//---- returns the employee name of a given employee id--create function emp_name (employee_id int)returns varchar(32)reads SQL databeginreturn (selectconcat(first_name, ' ', last_name) as namefromemployeeswhereemp_no = employee_id);end//---- returns the manager of a department-- choosing the most recent one-- from the manager list--create function current_manager( dept_id char(4) )returns varchar(32)reads sql databegindeclare max_date date;set max_date = (selectmax(from_date)fromdept_managerwheredept_no = dept_id);set @max_date=max_date;return (selectemp_name(emp_no)fromdept_managerwheredept_no = dept_idandfrom_date = max_datelimit 1);end //delimiter ;---- selects the employee records with the-- latest department--CREATE OR REPLACE VIEW v_full_employeesASSELECTemp_no,first_name , last_name ,birth_date , gender,hire_date,emp_dept_name(emp_no) as departmentfromemployees;---- selects the department list with manager names--CREATE OR REPLACE VIEW v_full_departmentsASSELECTdept_no, dept_name, current_manager(dept_no) as managerFROMdepartments;delimiter //---- shows the departments with the number of employees-- per department--create procedure show_departments()modifies sql databeginDROP TABLE IF EXISTS department_max_date;DROP TABLE IF EXISTS department_people;CREATE TEMPORARY TABLE department_max_date(emp_no int not null primary key,dept_from_date date not null,dept_to_date date not null, # bug#320513KEY (dept_from_date, dept_to_date));INSERT INTO department_max_dateSELECTemp_no, max(from_date), max(to_date)FROMdept_empGROUP BYemp_no;CREATE TEMPORARY TABLE department_people(emp_no int not null,dept_no char(4) not null,primary key (emp_no, dept_no));insert into department_peopleselect dmd.emp_no, dept_nofromdepartment_max_date dmdinner join dept_emp deon dmd.dept_from_date=de.from_dateand dmd.dept_to_date=de.to_dateand dmd.emp_no=de.emp_no;SELECTdept_no,dept_name,manager, count(*)from v_full_departmentsinner join department_people using (dept_no)group by dept_no;# with rollup;DROP TABLE department_max_date;DROP TABLE department_people;end //drop function if exists employees_usage //drop procedure if exists employees_help //CREATE FUNCTION employees_usage ()RETURNS TEXTDETERMINISTICBEGINRETURN'== USAGE ======================PROCEDURE show_departments()shows the departments with the manager andnumber of employees per departmentFUNCTION current_manager (dept_id)Shows who is the manager of a given departmenntFUNCTION emp_name (emp_id)Shows name and surname of a given employeeFUNCTION emp_dept_id (emp_id)Shows the current department of given employee';END //create procedure employees_help()deterministicbeginselect employees_usage() as info;end//delimiter ;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。