[置頂] MySQL -- 建立函數(Function

目標

如何在MySQL數據庫中建立函數(Function)mysql

 

語法

[sql]  view plain  copy
 print?
  1. CREATE FUNCTION func_name ( [func_parameter] ) //括號是必須的,參數是可選的  
  2. RETURNS type  
  3. [ characteristic ...] routine_body  
  • CREATE FUNCTION 用來建立函數的關鍵字;
  • func_name 表示函數的名稱;
  • func_parameters爲函數的參數列表,參數列表的形式爲:[IN|OUT|INOUT] param_name type
  1. IN:表示輸入參數;
  2. OUT:表示輸出參數;
  3. INOUT:表示既能夠輸入也能夠輸出;
  4. param_name:表示參數的名稱;
  5. type:表示參數的類型,該類型能夠是MySQL數據庫中的任意類型;

 

示例

建立示例數據庫、示例表與插入樣例數據腳本:sql

 

[sql]  view plain  copy
 print?
  1. create database hr;  
  2. use hr;  
  3.   
  4. create table employees  
  5. (  
  6.     employee_id int(11) primary key not null auto_increment,  
  7.     employee_name varchar(50) not null,  
  8.     employee_sex varchar(10) default '男',  
  9.     hire_date datetime not null default current_timestamp,  
  10.     employee_mgr int(11),  
  11.     employee_salary float default 3000,  
  12.     department_id int(11)  
  13. );  
  14.   
  15.   
  16. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('David Tian','男',10,7500,1);  
  17. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Black Xie','男',10,6600,1);  
  18. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Moses Wang','男',10,4300,1);  
  19. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Rena Ruan','女',10,5300,1);  
  20. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Sunshine Ma','女',10,6500,2);  
  21. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Scott Gao','男',10,9500,2);  
  22. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Warren Si','男',10,7800,2);  
  23. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Kaishen Yang','男',10,9500,3);  
  24. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Simon Song','男',10,5500,3);  
  25. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Brown Guan','男',10,5000,3);  
  26. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Eleven Chen','女',10,3500,2);  
  27. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Cherry Zhou','女',10,5500,4);  
  28. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Klause He','男',10,4500,5);  
  29. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Maven Ma','男',10,4500,6);  
  30. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Stephani Wang','女',10,5500,7);  
  31. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Jerry Guo','男',10,8500,1);  
  32. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Gerardo Garza','男',10,25000,8);  
  33. insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Derek Wu','男',10,5500,5);  
  34.   
  35.   
  36. select * from employees;  


 

 

建立函數-根據ID獲取員工姓名與員工工資數據庫

 

[sql]  view plain  copy
 print?
  1. DELIMITER //  
  2. CREATE FUNCTION GetEmployeeInformationByID(id INT)  
  3. RETURNS VARCHAR(300)  
  4. BEGIN  
  5.     RETURN(SELECT CONCAT('employee name:',employee_name,'---','salary: ',employee_salary) FROM employees WHERE employee_id=id);  
  6. END//  
  7. DELIMITER ;  

 

 

調用函數

mysql——函數的使用方法與MySQL內部函數的使用方法同樣。app

 

相關文章
相關標籤/搜索