视图:虚拟表,保存有实表的查询结果,相当于别名
创建方法:
CREATE VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
查看视图定义:
SHOW CREATE VIEW view_name #只能看视图定义
SHOW CREATE TABLE view_name # 可以查看表和视图
删除视图:
DROP VIEW [IF EXISTS]
view_name [, view_name] ...
[RESTRICT | CASCADE]
注意:视图中的数据事实上存储于“基表”中,因此,其修改操作也会针对基表实现;其修改操作受基表限制
范例:
MariaDB [hellodb]> create view v_score as select st.name,co.Course,sc.score from students st inner join scores sc on st.stuid=sc.stuid inner join courses co on sc.courseid=co.courseid;
Query OK, 0 rows affected (0.002 sec)
MariaDB [hellodb]> select * from v;
ERROR 1146 (42S02): Table 'hellodb.v' doesn't exist
MariaDB [hellodb]> select * from v_score;
+-------------+----------------+-------+
| name | Course | score |
+-------------+----------------+-------+
| Shi Zhongyu | Kuihua Baodian | 77 |
| Shi Zhongyu | Weituo Zhang | 93 |
| Shi Potian | Kuihua Baodian | 47 |
| Shi Potian | Daiyu Zanghua | 97 |
| Xie Yanke | Kuihua Baodian | 88 |
| Xie Yanke | Weituo Zhang | 75 |
| Ding Dian | Daiyu Zanghua | 71 |
| Ding Dian | Kuihua Baodian | 89 |
| Yu Yutong | Hamo Gong | 39 |
| Yu Yutong | Dagou Bangfa | 63 |
| Shi Qing | Hamo Gong | 96 |
| Xi Ren | Hamo Gong | 86 |
| Xi Ren | Dagou Bangfa | 83 |
| Lin Daiyu | Taiji Quan | 57 |
| Lin Daiyu | Jinshe Jianfa | 93 |
+-------------+----------------+-------+
15 rows in set (0.001 sec)
MariaDB [hellodb]> SHOW TABLE STATUS LIKE 'v_score'\G
*************************** 1. row ***************************
Name: v_score
Engine: NULL
Version: NULL
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: NULL
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Create_options: NULL
Comment: VIEW
Max_index_length: NULL
Temporary: NULL
1 row in set (0.001 sec)
[root@centos8 ~]#ls /var/lib/mysql/hellodb/
classes.frm courses.frm emp.ibd students.ibd teachers.ibd
classes.ibd courses.ibd scores.frm teachers2.frm toc.frm
coc.frm db.opt scores.ibd teachers2.ibd toc.ibd
coc.ibd emp.frm students.frm teachers.frm v_score.frm
ON 函数
函数:分为系统内置函数和自定义函数
创建UDF语法
CREATE [AGGREGATE] FUNCTION function_name(parameter_name type,[parameter_name
type,...])
RETURNS {STRING|INTEGER|REAL}
runtime_body
说明:
查看函数列表:
SHOW FUNCTION STATUS;
查看函数定义
SHOW CREATE FUNCTION function_name
删除UDF
DROP FUNCTION function_name
调用自定义函数语法
SELECT function_name(parameter_value,...)
范例:
#无参UDF
CREATE FUNCTION simpleFun() RETURNS VARCHAR(20) RETURN "Hello World";
#有参数UDF
DELIMITER //
CREATE FUNCTION deleteById(id SMALLINT UNSIGNED) RETURNS VARCHAR(20)
BEGIN
DELETE FROM students WHERE stuid = id;
RETURN (SELECT COUNT(*) FROM students);
END//
DELIMITER ;
MySQL中的变量
两种变量:系统内置变量和用户自定义变量
自定义函数中定义局部变量语法
DECLARE 变量1[,变量2,... ]变量类型 [DEFAULT 默认值]
说明:局部变量的作用范围是在BEGIN…END程序中,而且定义局部变量语句必须在BEGIN…END的第一行定义
范例:
DELIMITER //
CREATE FUNCTION addTwoNumber(x SMALLINT UNSIGNED, y SMALLINT UNSIGNED)
RETURNS SMALLINT
BEGIN
DECLARE a, b SMALLINT UNSIGNED;
SET a = x, b = y;
RETURN a+b;
END//
DELIMITER ;
MariaDB [hellodb]> select addTwoNumber(10,20);
+---------------------+
| addTwoNumber(10,20) |
+---------------------+
| 30 |
+---------------------+
1 row in set (0.000 sec)
为变量赋值语法
SET parameter_name = value[,parameter_name = value...]
SELECT INTO parameter_name
范例:
.....
DECLARE x int;
SELECT COUNT(*) FROM tdb_name INTO x;
RETURN x;
END//
范例:自定义的普通变量
存储过程:多表SQL的语句的集合,可以独立执行,存储过程保存在mysql.proc表中
存储过程优势:
存储过程把经常使用的SQL语句或业务逻辑封装起来,预编译保存在数据库中,当需要时从数据库中直接调用,省去了编译的过程,提高了运行速度,同时降低网络数据传输量
存储过程与自定义函数的区别
存储过程实现的过程要复杂一些,而函数的针对性较强
存储过程可以有多个返回值,而自定义函数只有一个返回值
存储过程一般可独立执行,而函数往往是作为其他SQL语句的一部分来使用
创建存储过程
CREATE PROCEDURE sp_name ([ proc_parameter [,proc_parameter ...]])
routime_body
proc_parameter : [IN|OUT|INOUT] parameter_name type
说明:其中IN表示输入参数,OUT表示输出参数,INOUT表示既可以输入也可以输出;param_name 表示参数名称;type表示参数的类型
查看存储过程列表
SHOW PROCEDURE STATUS;
查看存储过程定义
SHOW CREATE PROCEDURE sp_name
调用存储过程
CALL sp_name ([ proc_parameter [,proc_parameter ...]])
说明:当无参时,可以省略"()",当有参数时,不可省略"()”
存储过程修改
ALTER语句修改存储过程只能修改存储过程的注释等无关紧要的东西,不能修改存储过程体,所以要修改存储过程,方法就是删除重建
删除存储过程
DROP PROCEDURE [IF EXISTS] sp_name
范例
# 创建无参存储过程
delimiter //
CREATE PROCEDURE showTime()
BEGIN
SELECT now();
END//
delimiter ;
CALL showTime;
范例
#创建含参存储过程:只有一个IN参数
delimiter //
CREATE PROCEDURE selectById(IN id SMALLINT UNSIGNED)
BEGIN
SELECT * FROM students WHERE stuid = id;
END//
delimiter ;
call selectById(2);
范例
delimiter //
CREATE PROCEDURE dorepeat(n INT)
BEGIN
SET @i = 0;
SET @sum = 0;
REPEAT SET @sum = @sum+@i;
SET @i = @i + 1;
UNTIL @i > n END REPEAT;
END//
delimiter ;
CALL dorepeat(100);
SELECT @sum;
范例
#创建含参存储过程:包含IN参数和OUT参数
delimiter //
CREATE PROCEDURE deleteById(IN id SMALLINT UNSIGNED, OUT num SMALLINT UNSIGNED)
BEGIN
DELETE FROM students WHERE stuid >= id;
SELECT row_count() into num;
END//
delimiter ;
call deleteById(2,@Line);
SELECT @Line;
#说明:创建存储过程deleteById,包含一个IN参数和一个OUT参数.调用时,传入删除的ID和保存被修改的行数值的用户变量@Line,select @Line;输出被影响行数
#row_count() 系统内置函数,用于存放前一条SQL修改过的表的记录数
流程控制
存储过程和函数中可以使用流程控制来控制语句的执行
触发器的执行不是由程序调用,也不是由手工启动,而是由事件来触发、激活从而实现执行
创建触发器
CREATE [DEFINER = { user | CURRENT_USER }]
TRIGGER trigger_name
trigger_time trigger_event
ON tbl_name FOR EACH ROW
trigger_body
说明:
trigger_name:触发器的名称
trigger_time:{ BEFORE | AFTER },表示在事件之前或之后触发
trigger_event::{ INSERT |UPDATE | DELETE },触发的具体事件
tbl_name:该触发器作用在表名
范例:
#创建触发器,在向学生表INSERT数据时,学生数增加,DELETE学生时,学生数减少
CREATE TABLE student_info (
stu_id INT(11) NOT NULL AUTO_INCREMENT ,
stu_name VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (stu_id)
);
CREATE TABLE student_count (
student_count INT(11) DEFAULT 0
);
INSERT INTO student_count VALUES(0);
CREATE TRIGGER trigger_student_count_insert
AFTER INSERT
ON student_info FOR EACH ROW
UPDATE student_count SET student_count=student_count+1;
CREATE TRIGGER trigger_student_count_delete
AFTER DELETE
ON student_info FOR EACH ROW
UPDATE student_count SET student_count=student_count-1;
[root@centos8 ~]#cat /var/lib/mysql/hellodb/trigger_student_count_delete.TRN
TYPE=TRIGGERNAME
trigger_table=student_info
[root@centos8 ~]#cat /var/lib/mysql/hellodb/trigger_student_count_insert.TRN
TYPE=TRIGGERNAME
trigger_table=student_info
[root@centos8 ~]#cat /var/lib/mysql/hellodb/student_info.TRG
TYPE=TRIGGERS
triggers='CREATE DEFINER=`root`@`localhost` TRIGGER trigger_student_count_insert\nAFTER INSERT\nON student_info FOR EACH ROW\nUPDATE student_count SET student_count=student_count+1' 'CREATE DEFINER=`root`@`localhost` TRIGGER trigger_student_count_delete\nAFTER DELETE\nON student_info FOR EACH ROW\nUPDATE student_count SET student_count=student_count-1'
sql_modes=1411383296 1411383296
definers='root@localhost' 'root@localhost'
client_cs_names='utf8' 'utf8'
connection_cl_names='utf8_general_ci' 'utf8_general_ci'
db_cl_names='utf8_general_ci' 'utf8_general_ci'
created=159142866954 159142867177
MariaDB [hellodb]> select * from student_info;
Empty set (0.001 sec)
MariaDB [hellodb]> select * from student_count;
+---------------+
| student_count |
+---------------+
| 0 |
+---------------+
1 row in set (0.000 sec)
MariaDB [hellodb]> insert student_info (stu_name)value('xioaming');
Query OK, 1 row affected (0.002 sec)
MariaDB [hellodb]> select * from student_info;
+--------+----------+
| stu_id | stu_name |
+--------+----------+
| 1 | xioaming |
+--------+----------+
1 row in set (0.001 sec)
MariaDB [hellodb]> select * from student_count;
+---------------+
| student_count |
+---------------+
| 1 |
+---------------+
1 row in set (0.000 sec)
MariaDB [hellodb]> insert student_info (stu_name)value('xiaohong');
Query OK, 1 row affected (0.001 sec)
MariaDB [hellodb]> insert student_info (stu_name)value('xiaoqiang'),('wangcai');
Query OK, 2 rows affected (0.002 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [hellodb]> select * from student_info;
+--------+-----------+
| stu_id | stu_name |
+--------+-----------+
| 1 | xioaming |
| 2 | xiaohong |
| 3 | xiaoqiang |
| 4 | wangcai |
+--------+-----------+
4 rows in set (0.000 sec)
MariaDB [hellodb]> select * from student_count;
+---------------+
| student_count |
+---------------+
| 4 |
+---------------+
1 row in set (0.000 sec)
MariaDB [hellodb]> delete from student_info where stu_id >= 3;
Query OK, 2 rows affected (0.001 sec)
MariaDB [hellodb]> select * from student_count;
+---------------+
| student_count |
+---------------+
| 2 |
+---------------+
1 row in set (0.000 sec)
MariaDB [hellodb]> select * from student_info;
+--------+----------+
| stu_id | stu_name |
+--------+----------+
| 1 | xioaming |
| 2 | xiaohong |
+--------+----------+
2 rows in set (0.000 sec)
查看触发器
#在当前数据库对应的目录下,可以查看到新生成的相关文件:trigger_name.TRN,table _name.TRG
SHOW TRIGGERS;
#查询系统表information_schema.triggers的方式指定查询条件,查看指定的触发器信息。
USE information_schema;
SELECT * FROM triggers WHERE trigger_name='trigger_student_count_insert';
删除触发器
DROP TRIGGER trigger_name;
事件(event)是MySQL在相应的时刻调用的过程式数据库对象。一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的“事件调度器”。
事件和触发器类似,都是在某些事情发生的时候启动。当数据库上启动一条语句的时候,触发器就启动了,而事件是根据调度事件来启动的。由于它们彼此相似,所以事件也称为临时性触发器。
事件取代了原先只能由操作系统的计划任务来执行的工作,而且MySQL的事件调度器可以精确到每秒钟执行一个任务,而操作系统的计划任务(如:Linux下的CRON或Windows下的任务计划)只能精确到每分钟执行一次。
事件的优缺点
优点:一些对数据定时性操作不再依赖外部程序,而直接使用数据库本身提供的功能,可以实现每秒钟执行一个任务,这在一些对实时性要求较高的环境下就非常实用
缺点:定时触发,不可以直接调用
5.2.1 相关变量和服务器选项
MySQL事件调度器event_scheduler负责调用事件,它默认是关闭的。这个调度器不断地监视一个事件是否要调用, 要创建事件,必须打开调度器
服务器系统变量和服务器选项:
event_scheduler:默认值为OFF,设置为ON才支持Event,并且系统自动打开专用的线程
范例:开启和关闭event_scheduler
#默认事件调度功能是关闭的
MariaDB [(none)]> select @@event_scheduler;
+-------------------+
| @@event_scheduler |
+-------------------+
| OFF |
+-------------------+
1 row in set (0.000 sec)
#临时开启事件调度功能
MariaDB [(none)]> set global event_scheduler=1;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> show processlist;
+----+-----------------+-----------+------+---------+------+--------------------------+------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+-----------------+-----------+------+---------+------+--------------------------+------------------+----------+
| 1 | system user | | NULL | Daemon | NULL | InnoDB purge coordinator | NULL | 0.000 |
| 2 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 4 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 3 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 5 | system user | | NULL | Daemon | NULL | InnoDB shutdown handler | NULL | 0.000 |
| 6 | event_scheduler | localhost | NULL | Daemon | 2166 | Waiting on empty queue | NULL | 0.000 |
| 10 | root | localhost | NULL | Query | 0 | Init | show processlist | 0.000 |
+----+-----------------+-----------+------+---------+------+--------------------------+------------------+----------+
7 rows in set (0.000 sec)
#临时关闭事件调度功能
MariaDB [(none)]> set global event_scheduler=0;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
| 1 | system user | | NULL | Daemon | NULL | InnoDB purge coordinator | NULL | 0.000 |
| 2 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 4 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 3 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 5 | system user | | NULL | Daemon | NULL | InnoDB shutdown handler | NULL | 0.000 |
| 10 | root | localhost | NULL | Query | 0 | Init | show processlist | 0.000 |
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
6 rows in set (0.000 sec)
#持久开启事件调度
[root@centos8 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
event_scheduler=ON
[root@centos8 ~]#systemctl restart mariadb
5.2.2 管理事件
create event 语句创建一个事件。每个事件由两个主要部分组成,第一部分是事件调度(event schedule),表示事件何时启动以及按什么频率启动,第二部分是事件动作(event action ),这是事件启动时执行的代码,事件的动作包含一条SQL语句,它可能是一个简单地insert或者update语句,也可以使一个存储过程或者 benin…end语句块,这两种情况允许我们执行多条SQL
一个事件可以是活动(打开)的或停止(关闭)的,活动意味着事件调度器检查事件动作是否必须调用,停止意味着事件的声明存储在目录中,但调度器不会检查它是否应该调用。在一个事件创建之后,它立即变为活动的,一个活动的事件可以执行一次或者多次
创建Event
CREATE
[DEFINER = { user | CURRENT_USER }]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'comment']
DO event_body;
schedule:
AT timestamp [+ INTERVAL interval] ...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] ...]
[ENDS timestamp [+ INTERVAL interval] ...]
interval:
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}
说明:
提示:event事件是存放在mysql.event表中
查看Event
SHOW EVENTS [{FROM | IN} schema_name]
[LIKE 'pattern' | WHERE expr]
注意:事件执行完即释放,如立即执行事件,执行完后,事件便自动删除,多次调用事件或等待执行事件,才可以用上述命令查看到。
修改Event
ALTER
[DEFINER = { user | CURRENT_USER }]
EVENT event_name
[ON SCHEDULE schedule]
[ON COMPLETION [NOT] PRESERVE]
[RENAME TO new_event_name]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'comment']
[DO event_body]
注意:alter event语句可以修改事件的定义和属性。可以让一个事件成为停止的或者再次让它活动,也可以修改一个事件的名字或者整个调度。然而当一个使用 ON COMPLETION NOT PRESERVE 属性定义的事件最后一次执行后,事件直接就不存在了,不能修改
删除Event
DROP EVENT [IF EXISTS] event_name
5.2.3 范例
范例:创建立即启动事件
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> use testdb
Database changed
#创建一个表记录每次事件调度的名字和事件戳
MariaDB [testdb]> create table events_list(event_name varchar(20) not null,event_started timestamp not null);
Query OK, 0 rows affected (0.006 sec)
#临时关闭事件调度功能
MariaDB [testdb]> set global event_scheduler=0;
Query OK, 0 rows affected (0.000 sec)
MariaDB [testdb]> show variables like 'event_scheduler';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
1 row in set (0.001 sec)
#创建一次性事件
MariaDB [testdb]> create event event_now on schedule at now() do insert into events_list values('event_now', now());
Query OK, 0 rows affected, 1 warning (0.000 sec)
#因为事件调度功能禁用,所有表中无记录
MariaDB [testdb]> select *from events_list;
Empty set (0.001 sec)
#查看事件
MariaDB [testdb]> show events\G
*************************** 1. row ***************************
Db: testdb
Name: event_now
Definer: root@localhost
Time zone: SYSTEM
Type: ONE TIME
Execute at: 2020-06-06 16:52:08
Interval value: NULL
Interval field: NULL
Starts: NULL
Ends: NULL
Status: ENABLED
Originator: 1
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (0.001 sec)
#任务计划存放在mysql.event表中
MariaDB [testdb]> select * from mysql.event\G
*************************** 1. row ***************************
db: testdb
name: event_now
body: insert into events_list values('event_now', now())
definer: root@localhost
execute_at: 2020-06-06 08:52:08
interval_value: NULL
interval_field: NULL
created: 2020-06-06 16:52:08
modified: 2020-06-06 16:52:08
last_executed: NULL
starts: NULL
ends: NULL
status: ENABLED
on_completion: DROP
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
comment:
originator: 1
time_zone: SYSTEM
character_set_client: utf8
collation_connection: utf8_general_ci
db_collation: latin1_swedish_ci
body_utf8: insert into events_list values('event_now', now())
1 row in set (0.000 sec)
#开启事件调度功能
MariaDB [testdb]> set global event_scheduler=1;
Query OK, 0 rows affected (0.000 sec)
#事件立即执行,每秒插入一条记录
MariaDB [testdb]> select *from events_list;
+------------+---------------------+
| event_name | event_started |
+------------+---------------------+
| event_now | 2020-06-06 16:52:39 |
+------------+---------------------+
1 row in set (0.000 sec)
#事件执行完成后自动删除
MariaDB [testdb]> show events;
Empty set (0.001 sec)
范例:创建每秒启动的事件
#创建一个表记录每次事件调度的名字和事件戳
MariaDB [testdb]> create table events_list(event_name varchar(20) not null,event_started timestamp not null);
Query OK, 0 rows affected (0.005 sec)
#开启事件调度功能
MariaDB [testdb]> set global event_scheduler=1;
Query OK, 0 rows affected (0.000 sec)
#创建事件
MariaDB [testdb]> CREATE EVENT event_every_second ON SCHEDULE EVERY 1 SECOND DO INSERT INTO events_list VALUES('event_now', now());
Query OK, 0 rows affected, 1 warning (0.000 sec)
MariaDB [testdb]> SHOW EVENTS\G
*************************** 1. row ***************************
Db: testdb
Name: event_every_second
Definer: root@localhost
Time zone: SYSTEM
Type: RECURRING
Execute at: NULL
Interval value: 1
Interval field: SECOND
Starts: 2020-06-06 17:27:44
Ends: NULL
Status: ENABLED
Originator: 1
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (0.001 sec)
#事件是存放在mysql.event表中
MariaDB [testdb]> select *from mysql.event\G
*************************** 1. row ***************************
db: testdb
name: event_every_second
body: INSERT INTO events_list VALUES('event_now', now())
definer: root@localhost
execute_at: NULL
interval_value: 1
interval_field: SECOND
created: 2020-06-06 17:27:44
modified: 2020-06-06 17:27:44
last_executed: NULL
starts: 2020-06-06 09:27:44
ends: NULL
status: ENABLED
on_completion: DROP
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
comment:
originator: 1
time_zone: SYSTEM
character_set_client: utf8
collation_connection: utf8_general_ci
db_collation: latin1_swedish_ci
body_utf8: INSERT INTO events_list VALUES('event_now', now())
1 row in set (0.000 sec)
MariaDB [testdb]> select *from events_list;
+------------+---------------------+
| event_name | event_started |
+------------+---------------------+
| event_now | 2020-06-06 17:28:09 |
| event_now | 2020-06-06 17:28:10 |
| event_now | 2020-06-06 17:28:11 |
| event_now | 2020-06-06 17:28:12 |
| event_now | 2020-06-06 17:28:13 |
| event_now | 2020-06-06 17:28:14 |
| event_now | 2020-06-06 17:28:15 |
| event_now | 2020-06-06 17:28:16 |
| event_now | 2020-06-06 17:28:17 |
| event_now | 2020-06-06 17:28:18 |
| event_now | 2020-06-06 17:28:19 |
| event_now | 2020-06-06 17:28:20 |
| event_now | 2020-06-06 17:28:21 |
| event_now | 2020-06-06 17:28:22 |
+------------+---------------------+
14 rows in set (0.000 sec)
MariaDB [testdb]> drop event event_every_second;
Query OK, 0 rows affected (0.000 sec)
MariaDB [testdb]> SHOW EVENTS\G
Empty set (0.001 sec)
MariaDB [testdb]> select *from mysql.event\G
Empty set (0.000 sec)
范例: 创建每分钟启动的事件
MariaDB [testdb]> show variables like 'event_scheduler';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | ON |
+-----------------+-------+
1 row in set (0.001 sec)
MariaDB [testdb]> create event testdb.event_every_minute on schedule every 1 minute do insert into events_list values('event_now', now());
Query OK, 0 rows affected (0.000 sec)
MariaDB [testdb]> select now();
+---------------------+
| now() |
+---------------------+
| 2020-06-06 17:34:30 |
+---------------------+
1 row in set (0.000 sec)
MariaDB [testdb]> select *from events_list;
+------------+---------------------+
| event_name | event_started |
+------------+---------------------+
| event_now | 2020-06-06 17:34:26 |
+------------+---------------------+
20 rows in set (0.000 sec)
MariaDB [testdb]> SHOW EVENTS\G
*************************** 1. row ***************************
Db: testdb
Name: event_every_minute
Definer: root@localhost
Time zone: SYSTEM
Type: RECURRING
Execute at: NULL
Interval value: 1
Interval field: MINUTE
Starts: 2020-06-06 17:34:26
Ends: NULL
Status: ENABLED
Originator: 1
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (0.001 sec)
MariaDB [testdb]> select *from events_list;
+------------+---------------------+
| event_name | event_started |
+------------+---------------------+
| event_now | 2020-06-06 17:34:26 |
| event_now | 2020-06-06 17:35:26 |
| event_now | 2020-06-06 17:36:26 |
| event_now | 2020-06-06 17:37:26 |
| event_now | 2020-06-06 17:38:26 |
| event_now | 2020-06-06 17:39:26 |
| event_now | 2020-06-06 17:40:26 |
| event_now | 2020-06-06 17:41:26 |
| event_now | 2020-06-06 17:42:26 |
+------------+---------------------+
9 rows in set (0.000 sec)
范例:创建定时调用存储过程的事件
MariaDB [testdb]> drop event event_every_minute;
Query OK, 0 rows affected (0.000 sec)
MariaDB [testdb]> truncate table events_list;
Query OK, 0 rows affected (0.007 sec)
MariaDB [testdb]> select *from events_list;
Empty set (0.000 sec)
MariaDB [testdb]> delimiter //
MariaDB [testdb]> create procedure sp_insert()
-> begin
-> insert into events_list values('event_now', now());
-> end//
Query OK, 0 rows affected (0.000 sec)
MariaDB [testdb]> delimiter ;
MariaDB [testdb]> CREATE DEFINER=`root`@`localhost` EVENT event_test ON SCHEDULE EVERY 10 SECOND STARTS '2019-12-02 22:55:00' ON COMPLETION PRESERVE ENABLE DO call sp_insert();
Query OK, 0 rows affected (0.001 sec)
MariaDB [testdb]> select now();
+---------------------+
| now() |
+---------------------+
| 2020-06-06 18:11:06 |
+---------------------+
1 row in set (0.000 sec)
MariaDB [testdb]> select *from events_list;
+------------+---------------------+
| event_name | event_started |
+------------+---------------------+
| event_now | 2020-06-06 18:11:10 |
| event_now | 2020-06-06 18:11:20 |
| event_now | 2020-06-06 18:11:30 |
| event_now | 2020-06-06 18:11:40 |
+------------+---------------------+
4 rows in set (0.000 sec)
范例:修改事件‘
MariaDB [testdb]> ALTER DEFINER='root'@'localhost' EVENT event_test ON SCHEDULE EVERY 30 SECOND ON COMPLETION PRESERVE ENABLE DO call sp_insert();
#禁用事件
MariaDB [testdb]> alter event testdb.event_test disable;
MariaDB [testdb]> show events\G
#启用事件
MariaDB [testdb]> alter event testdb.event_test enable;
Query OK, 0 rows affected (0.000 sec)
MariaDB [testdb]> show events\G
#修改事件名称
MariaDB [testdb]> alter event testdb.event_test rename to event_test2;
Query OK, 0 rows affected (0.001 sec)
MariaDB [testdb]> show events\G