mysql 自定義函式的使用
檢視函式功能是否開啟:show variables like '%func%'
//mysql8 預設為關閉
開啟函式功能:SET GLOBAL log_bin_trust_function_creators=1;
關閉函式功能:SET GLOBAL log_bin_trust_function_creators=1;
函式的建立:
語法:
create function 函式名([引數列表]) returns 資料型別
begin
sql語句;
return 值;
end;
示例1:
create table class ( id int not null, cname varchar(10) not null, primary key(id) );
-- 最簡單的僅有一條sql的函式 create function myselect2() returns int return 666; select myselect2(); -- 呼叫函式 -- DELIMITER // create function myselect3() returns int begin declare c int; select id from class where cname="python" into c; return c; end; END// DELIMITER ; select myselect3(); -- 帶傳參的函式 create function myselect5(name varchar(15)) returns int begin declare c int; select id from class where cname=name into c; return c; end; select myselect5("python");
函式的呼叫:
-- 無參呼叫
select myselect3();
-- 傳參呼叫
select myselect5("python");
select * from class where id=myselect5("python");
函式的檢視:
檢視函式建立語句show create function 函式名;
檢視所有函式:show function status [like 'pattern'];
函式的刪除:drop function 函式名;
實際案例:https://www.cnblogs.com/mafeng/p/7121473.html
建立示例資料庫和資料表並插入資料
create database hr;
use hr;
create table employees
(
employee_id int(11) primary key not null auto_increment,
employee_name varchar(50) not null,
employee_sex varchar(10) default '男',
hire_date datetime not null default current_timestamp,
employee_mgr int(11),
employee_salary float default 3000,
department_id int(11)
);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('David Tian','男',10,7500,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Black Xie','男',10,6600,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Moses Wang','男',10,4300,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Rena Ruan','女',10,5300,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Sunshine Ma','女',10,6500,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Scott Gao','男',10,9500,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Warren Si','男',10,7800,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Kaishen Yang','男',10,9500,3);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Simon Song','男',10,5500,3);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Brown Guan','男',10,5000,3);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Eleven Chen','女',10,3500,2);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Cherry Zhou','女',10,5500,4);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Klause He','男',10,4500,5);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Maven Ma','男',10,4500,6);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Stephani Wang','女',10,5500,7);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Jerry Guo','男',10,8500,1);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Gerardo Garza','男',10,25000,8);
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Derek Wu','男',10,5500,5);
select * from employees;
建立函式-根據ID獲取員工姓名與員工工資
DELIMITER //
CREATE FUNCTION GetEmployeeInformationByID(id INT)
RETURNS VARCHAR(300)
BEGIN
RETURN(SELECT CONCAT('employee name:',employee_name,'---','salary: ',employee_salary) FROM employees WHERE employee_id=id);
END//
DELIMITER ;
使用方法:
mysql> select GetEmployeeInformationByID(2);
+----------------------------------------+
| GetEmployeeInformationByID(2) |
+----------------------------------------+
| employee name:Black Xie---salary: 6600 |
+----------------------------------------+
1 row in set (0.00 sec)
shell> vi function.sql;
DROP FUNCTION IF EXISTS get_sal_level;
DELIMITER $$
CREATE FUNCTION get_sal_level(emp int) RETURNS
VARCHAR(10)
DETERMINISTIC
BEGIN
DECLARE sal_level varchar(10);
DECLARE avg_sal FLOAT;
SELECT AVG(salary) INTO avg_sal FROM salaries WHERE
emp_no=emp;
IF avg_sal < 50000 THEN
SET sal_level = 'BRONZE';
ELSEIF (avg_sal >= 50000 AND avg_sal < 70000) THEN
SET sal_level = 'SILVER';
ELSEIF (avg_sal >= 70000 AND avg_sal < 90000) THEN
SET sal_level = 'GOLD';
ELSEIF (avg_sal >= 90000) THEN
SET sal_level = 'PLATINUM';
ELSE
SET sal_level = 'NOT FOUND';
END IF;
RETURN (sal_level);
END
$$
DELIMITER ;
To create the function:
mysql> SOURCE function.sql;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
You have to pass the employee number and the function
returns the income level.
mysql> SELECT get_sal_level(10002);
+----------------------+
| get_sal_level(10002) |
+----------------------+
| SILVER |
+----------------------+
1 row in set (0.00 sec)
mysql> SELECT get_sal_level(10001);
+----------------------+
| get_sal_level(10001) |
+----------------------+
| GOLD |
+----------------------+
1 row in set (0.00 sec)
mysql> SELECT get_sal_level(1);
+------------------+
| get_sal_level(1) |
+------------------+
| NOT FOUND |
+------------------+
1 row in set (0.00 sec)
Inbuilt functions 內建函式
MySQL provides numerous inbuilt functions. You have already used the CURDATE() function to get the current date.
mysql> SELECT * FROM employees WHERE hire_date =CURDATE();
mysql> SELECT DATE_ADD(CURDATE(), INTERVAL -7DAY) AS '7 Days Ago';
SELECT CONCAT(first_name, ' ', last_name) FROM employees LIMIT 1;
函式和內建函式:https://dev.mysql.com/doc/refman/8.0/en/func-op-summary-ref.html
Triggers 觸發器:使用很少(略)
參考:https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html
Views 檢視
View is a virtual table based on the result-set of an SQL statement. It will also have rows and columns just like a real table, but few restrictions, which will be discussed later. Views hide the SQL complexity and, more importantly, provide additional security.
CREATE ALGORITHM=UNDEFINED
DEFINER=`root`@`localhost`
SQL SECURITY DEFINER VIEW salary_view
AS
SELECT emp_no, salary FROM salaries WHERE from_date >
'2002-01-01';
create view view_name
as
select * from tablename;
[ WITH CHECK OPTION] //保證更新檢視是在該檢視的許可權範圍之內。
Now the salary_view view is created and you can query it just like any other table:
mysql> SELECT emp_no, AVG(salary) as avg FROM
salary_view GROUP BY emp_no ORDER BY avg DESC LIMIT
5;
To list all views:SHOW FULL TABLES WHERE TABLE_TYPE LIKE 'VIEW';
參考:https://www.cnblogs.com/geaozhang/p/6792369.html //深入理解mysql檢視
EVENTS:事件
檢視事件排程是否開啟:show variables like '%event_scheduler%';
啟動事件排程:SET GLOBAL event_scheduler = ON;
具體參考:https://www.cnblogs.com/qlqwjy/p/7954175.html
PROCESSLIST:
One of the most used views is the process list. It lists all the queries running on the server:
select * from information_schema.processlist;
show processlist;
具體參考:https://www.cnblogs.com/shengdimaya/p/6920677.html //十分鐘了結MySQL information_schema