【SQL篇章--DATABASE/EVENTS】
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] create_specification :
[DEFAULT] CHARACTER SET [=] charset_name
[DEFAULT] COLLATE [=] collation_name
CREATE DATABASE db2;
CREATE DATABASE `db2` /*!40100 DEFAULT CHARACTER SET latin1 */
SHOW DATABASES;
ALTER {DATABASE | SCHEMA} [db_name]
alter_specification ...
ALTER {DATABASE | SCHEMA} db_name
UPGRADE DATA DIRECTORY NAME alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name
ALTER DATABASE db1 CHARACTER SET = utf8;
mysql> SELECT * FROM information_schema.`SCHEMATA` WHERE schema_name='db1';
+--------------+-------------+----------------------------+------------------------+----------+
| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
+--------------+-------------+----------------------------+------------------------+----------+
| def | db1 | utf8 | utf8_general_ci | NULL |
+--------------+-------------+----------------------------+------------------------+----------+
1 row in set (0.04 sec)
ALTER DATABASE db1 CHARACTER SET = latin1;
mysql> SELECT * FROM information_schema.`SCHEMATA` WHERE schema_name='db1';
+--------------+-------------+----------------------------+------------------------+----------+
| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
+--------------+-------------+----------------------------+------------------------+----------+
| def | db1 | latin1 | latin1_swedish_ci | NULL |
+--------------+-------------+----------------------------+------------------------+----------+
1 row in set (0.00 sec)
CREATE TABLE t5(id int);
查看所建立表的字符集
mysql> SHOW CREATE TABLE db1.t5;
+-------+----------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------+
| t5 | CREATE TABLE `t5` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
测试发现:
1.db的character改变后,collation随之改变。
2.db中旧表character不会改变。新建表character默认为与当前db相同。
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
mysql> drop database wb;
ERROR 1010 (HY000): Error dropping database (can't rmdir './wb/', errno: 17)
mysql> drop database wb;
Query OK, 0 rows affected (0.00 sec)
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需要:event,super权限,建好之后,必须是enable状态。
- ON SCHEDULE : 决定什么时间或者多长时间,时间执行一次
- DO :包含被event执行的SQL语句
Select @@event_scheduler;
SET GLOBAL event_scheduler = ON;
[mysqld]
event_scheduler=1
mysql start --event_scheduler=1
CREATE
DEFINER = CURRENT_USER
EVENT IF NOT EXISTS myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 SECOND
DO
INSERT INTO db1.t1 VALUES(1);
CREATE
DEFINER = CURRENT_USER
EVENT IF NOT EXISTS myevent
ON SCHEDULE EVERY 1 SECOND
DO
INSERT INTO db1.t1 VALUES(1);
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2016-11-16 10:01:39 |
+---------------------+
1 row in set (0.00 sec) mysql> CREATE EVENT event_1
-> ON SCHEDULE AT '2006-02-10 23:59:00'
-> DO INSERT INTO test.totals VALUES (NOW());
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Note
Code: 1588
Message: Event execution time is in the past and ON COMPLETION NOT PRESERVE is set. The event was dropped immediately after creation.
1 row in set (0.00 sec)
AT CURRENT_TIMESTAMP + INTERVAL '2:3' MINUTE_SECOND
AT CURRENT_TIMESTAMP + INTERVAL 3 WEEK + INTERVAL 2 DAY
ON SCHEDULE EVERY 6 WEEK
EVERY 3 MONTH STARTS CURRENT_TIMESTAMP + INTERVAL 1 WEEK means
EVERY 2 WEEK STARTS CURRENT_TIMESTAMP + INTERVAL '6:15' HOUR_MINUTE.
EVERY 12 HOUR STARTS CURRENT_TIMESTAMP + INTERVAL 30 MINUTE ENDS CURRENT_TIMESTAMP + INTERVAL 4 WEEK
- 正常来说,一旦event过期,会立即drop掉。
- 可以通过设置:ON COMPLETION PRESERVE,来禁止drop操作。此时,event的状态从:ENABLE---->DISABLE。EVENT停止执行,保留存在。
- 可以通过设置:ON COMLETION NOT PRESERVE,不禁止drop操作,此时,event执行完毕,会立即drop掉。此时看不到event了。
- 在不指定时,默认:COMPLETION NOT PRESERVE ENABLE .也就是说,event过期后会自动drop。
CREATE DEFINER=CURRENT_USER EVENT IF NOT EXISTS myevent
ON SCHEDULE EVERY 1 SECOND STARTS CURRENT_TIMESTAMP + INTERVAL 1 MINUTE ENDS CURRENT_TIMESTAMP + INTERVAL 3 MINUTE
DO
INSERT INTO db1.`t1`(id) VALUES(5);
DELIMITER $$
ALTER DEFINER=`admin`@`%` EVENT `myevent`
ON SCHEDULE EVERY 1 SECOND STARTS '2016-11-30 16:42:02' ENDS '2016-11-30 16:44:02'
ON COMPLETION NOT PRESERVE ENABLE
DO INSERT INTO db1.`t1`(id) VALUES(5)$$
DELIMITER ;
CREATE DEFINER=CURRENT_USER EVENT IF NOT EXISTS myevent
ON SCHEDULE EVERY 1 SECOND STARTS CURRENT_TIMESTAMP + INTERVAL 1 MINUTE ENDS CURRENT_TIMESTAMP + INTERVAL 3 MINUTE
ON COMPLETION PRESERVE ENABLE
DO
INSERT INTO db1.t1(id) VALUES(1);
观察master 和 slave上event的结构:
DELIMITER $$
ALTER DEFINER=`admin`@`%` EVENT `myevent`
ON SCHEDULE EVERY 1 SECOND STARTS '2016-11-16 01:35:40' ENDS '2016-11-16 01:37:40'
ON COMPLETION PRESERVE ENABLE DO INSERT INTO db1.t1(id) VALUES(1)$$
DELIMITER ;
DELIMITER $$
ALTER DEFINER=`admin`@`%` EVENT `myevent`
ON SCHEDULE EVERY 1 SECOND STARTS '2016-11-16 01:35:40' ENDS '2016-11-16 01:37:40'
ON COMPLETION PRESERVE DISABLE ON SLAVE DO INSERT INTO db1.t1(id) VALUES(1)$$
DELIMITER ;
mysql> SELECT EVENT_SCHEMA,EVENT_NAME,STATUS FROM `information_schema`.`EVENTS` WHERE EVENT_SCHEMA='db1';
+--------------+------------+----------+
| EVENT_SCHEMA | EVENT_NAME | STATUS |
+--------------+------------+----------+
| db1 | myevent | DISABLED |
+--------------+------------+----------+
1 row in set (0.00 sec)
mysql> SELECT EVENT_NAME,STATUS FROM `information_schema`.`EVENTS`;
+------------+--------------------+
| EVENT_NAME | STATUS |
+------------+--------------------+
| myevent | SLAVESIDE_DISABLED |
+------------+--------------------+
[COMMENT 'comment']解析:
mysql> SELECT SQL_MODE FROM information_schema.`EVENTS`;
+--------------------------------------------+
| SQL_MODE |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
DELIMITER $$
CREATE EVENT e_count1
ON SCHEDULE
EVERY 4 SECOND
COMMENT 'Saves total number of tb1'
DO
BEGIN
INSERT INTO t2(TIME,total) SELECT CURRENT_TIMESTAMP,COUNT(*) FROM t1;
DELETE FROM t1;
END $$
DELIMITER ;
DELIMITER $$ CREATE EVENT e
ON SCHEDULE
EVERY 5 SECOND
DO
BEGIN
DECLARE v INTEGER;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
SET v = 0; WHILE v < 5 DO
INSERT INTO t3 VALUES(0);
UPDATE t3 SET s1 = s1 + 1;
SET v = v + 1;
END WHILE;
END $$
DELIMITER ;
CREATE EVENT e_call_myproc
ON SCHEDULE
AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
DO CALL myproc(5, 27);
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]
mysql> alter event event1 on schedule every '2:3' DAY_HOUR ;
ERROR 1539 (HY000): Unknown event 'event1'
ALTER EVENT myevent
ON SCHEDULE
EVERY 2 SECOND STARTS CURRENT_TIMESTAMP + INTERVAL 1 MINUTE ENABLE
DO
INSERT INTO t2(TIME,total) VALUES(CURRENT_TIMESTAMP,5);
ALTER EVENT myevent DISABLE;
ALTER EVENT olddb.myevent RENAME TO newdb.myevent;
SHOW EVENTS [{FROM | IN} schema_name] [LIKE 'pattern' | WHERE expr]
mysql> select current_user(),schema();
+-----------------+----------+
| current_user() | schema() |
+-----------------+----------+
| admin@localhost | db1 |
+-----------------+----------+
1 row in set (0.00 sec)
mysql> show events\G
*************************** 1. row ***************************
Db: db1
Name: myevent
Definer: admin@%
Time zone: SYSTEM
Type: RECURRING
Execute at: NULL
Interval value: 2
Interval field: SECOND
Starts: 2016-11-16 08:37:26
Ends: 2016-11-16 01:44:11
Status: DISABLED
Originator: 5
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
1 row in set (0.07 sec)
SHOW EVENTS FROM wb;
SHOW EVENTS FROM wb like '%wb';
SHOW CREATE EVENT event_name;
mysql> show create event myevent\G
*************************** 1. row ***************************
Event: myevent
sql_mode: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
time_zone: SYSTEM
Create Event: CREATE DEFINER=`admin`@`%` EVENT `myevent`
ON SCHEDULE EVERY 2 SECOND STARTS '2016-11-16 08:37:26' ENDS '2016-11-16 01:44:11'
ON COMPLETION PRESERVE DISABLE
DO insert into t2(Time,total) values(current_timestamp,5)
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec)
DROP EVENT [IF EXISTS] event_name
mysql> DROP EVENT E;
ERROR 1539 (HY000): Unknown event 'E'
mysql> DROP EVENT IF EXISTS E;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP EVENT e_1;
Query OK, 0 rows affected (0.00 sec)
【SQL篇章--DATABASE/EVENTS】的更多相关文章
- SQL UNION 和 UNION ALL 操作符\SQL SELECT INTO 语句\SQL CREATE DATABASE 语句
SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...
- Create a SQL Server Database on a network shared drive
(原文地址:http://blogs.msdn.com/b/varund/archive/2010/09/02/create-a-sql-server-database-on-a-network-sh ...
- 【SQL篇章--CREATE TABLE】
[SQL篇章][SQL语句梳理 :--基于MySQL5.6][已梳理:CREATE TABLE][会坚持完善] SQL : 1. Data Definition Statements: 1.3 CRE ...
- P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1
P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1 May ...
- 转载:Restore SQL Server database and overwrite existing database
转载自:https://www.mssqltips.com/sqlservertutorial/121/restore-sql-server-database-and-overwrite-existi ...
- How to Kill All Processes That Have Open Connection in a SQL Server Database[关闭数据库链接 最佳方法] -摘自网络
SQL Server database administrators may frequently need in especially development and test environmen ...
- Sqlite出现SQL error: database disk image is malformed的处理
SQLite有一个很严重的缺点就是不提供Repair命令.导致死亡提示database disk image is malformed它的产生有很多种可能,比如,磁盘空间不足,还有就是写入数据过程中突 ...
- How do I copy SQL Azure database to my local development server?(如何将Azure 中的数据库备份到本地)
Now you can use the SQL Server Managerment Studio to do this: Connect to the SQL Azure database. 通过 ...
- SQL CREATE DATABASE 语句
CREATE DATABASE 语句 CREATE DATABASE 用于创建数据库. SQL CREATE DATABASE 语法 CREATE DATABASE database_name SQL ...
随机推荐
- SSH/SSL 源码编译安装简易操作说明
环境:CentOS 6.7 安全加固需求,由于某盟扫描系统主机有SSL系列漏洞,客户要求必须修复: 解决方案:将SSH/SSL升级到最新版本,删除SSL旧版本(实测不删除旧版本某盟扫描无法通过). 当 ...
- 高版本->低版本迁移,低版本客户端连接高版本数据库EXP导出报错EXP-00008,ORA-01455,EXP-00000
生产环境: 源数据库:RHEL + Oracle 11.2.0.3 目标数据库:HP-UX + Oracle 10.2.0.4 需求:迁移部分表 11.2.0.3-->10.2.0.4,若 ...
- Tomcat服务器本地的搭建,以及在 IDEA软件下的配置,以及项目的测试运行(基于supermvc框架下的web)
一.声明 使用了基于springmvc的supermvc的web框架.实习公司的框架. 二.tomact的下载与安装 1选择适合自己电脑配置的jdk和jre版本(截图来自tomcat的官方网站http ...
- 浅谈C#中一种类插件系统编写的简单方法(插件间、插件宿主间本身不需要通信)
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 三年多前还在上研时,用C#+反射机制写过插件系统,后来又用M ...
- 获取当前方法名,行号,类名,所在java文件第几行
public class Demo { public static void main(String[] args) { Demo demo = new Demo(); demo.go(); } pu ...
- .net提交HTML元素到后台,遇到Request报错 解决方案
对于.NET MVC 项目来说,在Controller中对应的Action方法上打上标签: [ValidateInput(false)] 在MSDN上:HttpRequest 类使用输入验证标志来跟踪 ...
- SQL Server时间粒度系列----第9节时间粒度示例演示
本文目录列表: 1.准备测试数据 2.向测试数据表添加相关时间粒度字段列 3.基于日月季年统计汇总的演示 4.总结语 5.参考清单列表 准备测试数据 为了提供不同时间粒度示例的演示,就需要测试 ...
- gitlab工作流程简介
gitlab工作流程简介 新建项目流程 创建/导入项目 可以选择导入github.bitbucket项目,也可以新建空白项目,还可以从SVN导入项目 建议选择private等级 初始化项目 1.本地克 ...
- [WCF编程]4.契约概述
一.契约的基本概念 契约是消息参与者之间的约定.在SOA架构中,契约提供了服务通信所必需的元数据.契约用来定义数据类型,操作,消息交换模式和消息交换使用的传输协议.契约通常是在标准化平台中使用与编程语 ...
- Tomcat 8080端口被占用解决方法
使用lsof命令查看端口占用情况 sudo lsof -i:8080 端口占用情况 java 1564 tomcat8 50u IPv6 19336 0t0 TCP *:http-alt (LISTE ...