mysql> delimiter $$ mysql> create function avg_email(user varchar(30) ) -> returns float reads sql data -> begin -> if user is null then -> return(select avg(size) from email); -> else -> return(select avg(size) from email where srcuser = user); -> end if; -> end ; -> $$ Query OK, 0 rows affected (0.02 sec)
mysql> delimiter ;
mysql> select srcuser,avg_email(srcuser) from email ;| srcuser | avg_email(srcuser) | +---------+--------------------+ | aa | 11111 | | bb | 22222 | | cc | 33333 | | dd | 44444 | | ee | 55555 | | ff | 66666 | +---------+--------------------+ 6 rows in set (0.00 sec) |
-> begin-> selectcount(*),ifnull(sum(size),0) ,ifnull(avg(size),0) intomessages,total_size,avg_size-> from mail where srcuser = user;-> end;-> $$
mysql> create table doc_table( -> author varchar(100) not null, -> title varchar(100) not null, -> document mediumblob not null, -> doc_hash char(32) not null, -> primary key(doc_hash)); -> $$ Query OK, 0 rows affected (0.16 sec) mysql> delimiter ; mysql> create trigger bi_doc_table before insert on doc_table -> for each row set new.doc_hash = md5(new.document); Query OK, 0 rows affected (0.23 sec) mysql> insert into doc_table(author,title,document) values('zhangsan','sanmaoliuliangji','aaaaaaaaa'); ERROR 1364 (HY000): Field 'doc_hash' doesn't have a default value mysql> show create table doc_table; +-----------+--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------+ | Table | Create Table | +-----------+--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------+ | doc_table | CREATE TABLE `doc_table` ( `author` varchar(100) NOT NULL, `title` varchar(100) NOT NULL, `document` mediumblob NOT NULL, `doc_hash` char(32) NOT NULL DEFAULT '', PRIMARY KEY (`doc_hash`) )ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-----------+--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> alter table doc_table engine=myisam; Query OK, 0 rows affected (0.28 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> insert into doc_table(author,title,document) values('zhangsan','sanmaoliuliangji','aaaaaaaaa'); Query OK, 1 row affected (0.03 sec) mysql> select * from doc_table; +----------+------------------+-----------+----------------------------------+ | author | title | document | doc_hash | +----------+------------------+-----------+----------------------------------+ | zhangsan | sanmaoliuliangji | aaaaaaaaa | 552e6a97297c53e592208cf97fbb3b60 | +----------+------------------+-----------+----------------------------------+ 1 row in set (0.00 sec) //你会发现在使用了INNODB的存储引擎时,非空插入并且即使触发器会填充数据,它也汇报非空约束;但是使用了MYISAM的存储引擎时,非空插入并且触发器填充数据,它就能够成功的插入数据 |
mysql> create trigger up_doc_table beforeinsertondoc_table -> for each row set new.doc_hash = md5(new.document); ERROR 1235 (42000):This version of MySQL doesn't yet support 'multiple triggers withthe same actiontime and event for one table'//不能对同一张表创建相同触发事件 mysql> create trigger up_doc_table before update on doc_table -> for each row setnew.doc_hash = md5(new.document);//指定将要被插入到指定列的新值 Query OK, 0 rows affected (0.22 sec) mysql> update doc_table set document = 'bbbbbbbb' -> where document = 'aaaaaaaaa'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from doc_table; +----------+------------------+----------+----------------------------------+ | author | title | document | doc_hash | +----------+------------------+----------+----------------------------------+ | zhangsan | sanmaoliuliangji | bbbbbbbb | 810247419084c82d03809fc886fedaad | +----------+------------------+----------+----------------------------------+ 1 row in set (0.00 sec) |
mysql> select * from ts_emulate; +------+------------+----------+---------------------+ | data | d | t | dt | +------+------------+----------+---------------------+ |ccc | 2014-12-12 | 18:57:55 | 2014-12-12 18:57:55 | | dd | 2014-12-12 | 18:57:59 | 2014-12-12 18:57:59 | +------+------------+----------+---------------------+ 2 rows in set (0.00 sec) mysql> delimiter $$ mysql> create trigger up_ts_emulate before update on ts_emulate -> for each row -> begin -> if new.data <> old.data then -> set new.d = curdate(),new.t = curtime(),new.dt = now(); -> end if; -> end; -> $$ Query OK, 0 rows affected (0.23 sec) mysql> update ts_emulate te set te.data = 'eeee' where te.data = 'cc -> $$ Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from ts_emulate; -> $$ +------+------------+----------+---------------------+ | data | d | t | dt | +------+------------+----------+---------------------+ |eeee | 2014-12-12 | 19:08:51 | 2014-12-12 19:08:51 | | dd | 2014-12-12 | 18:57:59 | 2014-12-12 18:57:59 | +------+------------+----------+---------------------+ 2 rows in set (0.00 sec) mysql> |
mysql> create trigger up_ts_emulate after update on ts_emulate -> for each row -> set new.d = adddata(curdate(),interval 1 day); ERROR 1362 (HY000): Updating of NEW row is not allowed in after trigger mysql> create trigger up_ts_emulate after update on ts_emulate -> for each row -> set old.d = adddata(curdate(),interval 1 day); ERROR 1362 (HY000): Updating of OLD row is not allowed in trigger mysql> |
mysql> create table log( -> ts timestamp,message varchar(255)); Query OK, 0 rows affected (0.22 sec) mysql> create event log -> on schedule every 1 minute//用来设置执行的时间间隔;该表示每分钟执行一次 -> do insert into log(message) values('--mark--'); Query OK, 0 rows affected (0.13 sec) mysql> select * from log; Empty set (0.00 sec) mysql> show variables like 'event_scheduler'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | event_scheduler | OFF | +-----------------+-------+ 1 row in set (0.11 sec) mysql> set global event_scheduler = 1;//默认是没开启,这种开启方式服务器运行期间,要想一直开启配置:event_scheduler = 1 Query OK, 0 rows affected (0.06 sec) mysql> show variables like 'event_scheduler'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | event_scheduler | ON | +-----------------+-------+ 1 row in set (0.00 sec) mysql> select * from log; +---------------------+----------+ | ts | message | +---------------------+----------+ | 2014-12-12 20:22:02 | --mark-- | +---------------------+----------+ 1 row in set (0.05 sec) mysql> set @@event_scheduler = 1;//不可以通过这种方式设置值 ERROR 1229 (HY000): Variable 'event_scheduler' is a GLOB mysql> select @@event_scheduler;//可以通过这种方式引用 +-------------------+ | @@event_scheduler | +-------------------+ | ON | +-------------------+ 1 row in set (0.00 sec) mysql> select @@event_scheduler; +-------------------+ | @@event_scheduler | +-------------------+ | ON | +-------------------+ 1 row in set (0.00 sec) mysql> select * from log; +---------------------+----------+ | ts | message | +---------------------+----------+ | 2014-12-12 20:22:02 | --mark-- | | 2014-12-12 20:23:02 | --mark-- | +---------------------+----------+ 2 rows in set (0.00 sec) mysql> alter event log disable;//悬挂事件的执行 Query OK, 0 rows affected (0.00 sec) mysql> select * from log; +---------------------+----------+ | ts | message | +---------------------+----------+ | 2014-12-12 20:22:02 | --mark-- | | 2014-12-12 20:23:02 | --mark-- | | 2014-12-12 20:24:02 | --mark-- | +---------------------+----------+ 3 rows in set (0.00 sec) mysql> select * from log; +---------------------+----------+ | ts | message | +---------------------+----------+ | 2014-12-12 20:22:02 | --mark-- | | 2014-12-12 20:23:02 | --mark-- | | 2014-12-12 20:24:02 | --mark-- | +---------------------+----------+ 3 rows in set (0.00 sec) mysql> alter event log enable;//重新激活调度器 Query OK, 0 rows affected (0.00 sec) mysql> select * from log; +---------------------+----------+ | ts | message | +---------------------+----------+ | 2014-12-12 20:22:02 | --mark-- | | 2014-12-12 20:23:02 | --mark-- | | 2014-12-12 20:24:02 | --mark-- | | 2014-12-12 20:27:02 | --mark-- | +---------------------+----------+ 4 rows in set (0.00 sec) mysql> create event delete_log//让其仍然执行,但是创建一个互补的事件来清除日志;相互写作 -> on schedule every 1 minute -> do delete from log where ts < now() - interval 2 Query OK, 0 rows affected (0.03 sec) mysql> select * from log; +---------------------+----------+ | ts | message | +---------------------+----------+ | 2014-12-12 20:29:02 | --mark-- | | 2014-12-12 20:30:02 | --mark-- | +---------------------+----------+ 2 rows in set (0.02 sec) mysql> select * from log; +---------------------+----------+ | ts | message | +---------------------+----------+ | 2014-12-12 20:29:02 | --mark-- | | 2014-12-12 20:30:02 | --mark-- | +---------------------+----------+ 2 rows in set (0.00 sec) mysql> select * from log; |