Mysql數據庫相關操作

Mysql命令參考文檔

mysql事務
模式匹配

創建數據庫:

CREATE DATABASE `db_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

設置默認數據庫:

use db_name;

創建表格:

t1: create table tb_hour_reason_static(ip varchar(15) not null,reason varchar(30) not null,static_time datetime not null,shownum bigint(12),clicknum bigint(12),primary key (ip,reason,static_time));
t2: create view v_day_reason_static(ip,reason,static_date,shownum,clicknum) as  select ip,reason,date(static_time),sum(shownum),sum(clicknum) from tb_hour_reason_static  group by date(static_time),reason,ip;

t3: create table tb_hour_allip_reason_static(reason varchar(30) not null,static_time datetime not null,shownum bigint(12),clicknum bigint(12),primary key (reason,static_time));

插入數據:

insert into tb_hour_allip_reason_static(reason,static_time,shownum,clicknum) select reason,static_time,sum(shownum),sum(clicknum) from tb_hour_reason_static group by reason,static_time;

t4: create view v_day_allip_reason_static(reason,static_date,shownum,clicknum) as  select reason,date(static_time),sum(shownum),sum(clicknum) from tb_hour_allip_reason_static group by date(static_time),reason;

每次點擊
t5: create table tb_day_whole_static(static_date date not null primary key,shownum bigint(12),clicknum  bigint(12));
插入數據:
insert into tb_day_whole_static(static_date,shownum,clicknum) select static_date,sum(shownum) as shownum,sum(clicknum) as clicknum from v_day_allip_reason_static where reason regexp '^[0-9]*$' group by static_date;

通過部分字段查詢結果及部分字段給定值插入數據:

對 部分reason進行累加
insert into tb_hour_reason_static(ip,reason,static_time,shownum,clicknum) select ip,’RCMT’ as reason,static_time,sum(shownum) as shownum,sum(clicknum) as clicknum from tb_hour_reason_static where static_time=’2017-08-16 07:00:00’ and (reason=’RCM’ or reason=’RCMF’) group by ip;

insert into tb_hour_reason_static(ip,reason,static_time,shownum,clicknum) select ip,’CF’ as reason,static_time,sum(shownum) as shownum,sum(clicknum) as clicknum from tb_hour_reason_static where static_time=’2017-08-16 07:00:00’ and (reason=’IRCMR’ or reason=’IRCMH’ or reason=’IRCM1’ or reason=’IRCM2’ or reason=’IRCM3’) group by ip;

insert into tb_hour_reason_static(ip,reason,static_time,shownum,clicknum) select ip,’VIDEO’ as reason,static_time,sum(shownum) as shownum,sum(clicknum) as clicknum from tb_hour_reason_static where static_time=’2017-08-16 07:00:00’ and reason like ‘%VIDEO%’ group by ip;

創建視圖(通過查詢結果創建):

v1: create view v_hour_allreason_static(ip,static_time,shownum,clicknum) as select ip,static_time,sum(shownum),sum(clicknum) from tb_hour_reason_static where reason not regexp '^[0-9]*$' group by ip,static_time;
v2: create view v_day_allreason_static(ip,static_date,shownum,clicknum) as select ip,static_date,sum(shownum),sum(clicknum) from v_day_reason_static where reason not regexp '^[0-9]*$' group by ip,static_date;

v3: create view v_hour_allip_allreason_static(static_time,shownum,clicknum) as select static_time ,sum(shownum),sum(clicknum)  from tb_hour_allip_reason_static where reason not regexp '^[0-9]*$' group  by static_time;

查詢

select count(*) from tb_hour_reason_static where ip ='172.27.60.138';

select sum(shownum) from tb_hour_reason_static where ip ='172.27.60.138' and static_time like '2017-07-10%';
select * from v_hour_allreason_static where static_time>='2017-07-25 00:00:00' and ip='10.168.11.211';

關於時間字段的條件判斷

8天以前的數據刪除
 delete from tb_hour_reason_static where date(static_time) <= DATE_SUB(CURDATE(), INTERVAL 8 DAY);
32天以前的數據刪除
   delete from tb_hour_allip_reason_static where date(static_time) <= DATE_SUB(CURDATE(), INTERVAL 32 DAY);
367天以前的數據刪除
   delete from tb_day_whole_static where static_date <= DATE_SUB(CURDATE(), INTERVAL 367 DAY);
從當前時間前4小時 之後的數據插入
insert into tb_hour_allip_reason_static(reason,static_time,shownum,clicknum) (select reason,static_time,sum(shownum),sum(clicknum) from tb_hour_reason_static where static_time>DATE_SUB(NOW(), INTERVAL 4 HOUR) group by reason,static_time);
end;

查詢中的模式匹配與模糊查詢:

參考:mysql文檔

鏈接兩個查詢的結果擴充

/*********************************************************************************/
select a.static_date,a.shownum,a.clicknum,b.shownum,b.clicknum from 
(select static_date,sum(shownum) as shownum,sum(clicknum) as clicknum from v_day_allip_reason_static where reason regexp '^[0-9]*$') a
left join
(select static_date,sum(shownum) as shownum,sum(clicknum) as clicknum from v_day_allip_reason_static where reason not regexp '^[0-9]*$') b
on a.static_date=b.static_date
/**********************************************************************************/

事務和存儲過程

數據表類型爲innodb 才支持事務

 alter table tb_day_whole_static  engine=innodb;
 alter table tb_hour_reason_static engine=innodb;
 alter table tb_hour_allip_reason_static  engine=innodb;

設置數據庫事件開啓:

show variables like '%sche%';
set global event_scheduler='on';

存儲過程:

proc1:
對 table1 只保存當天,及其前7天的數據。即是:1天 0點過後1分鐘,刪除當前時間8天前的數據。

對 table3 只當天及其前保存31天數據,即是:1 天 0點過後 1分鐘,刪除當前時間 32天前的數據。

對 table5 只保存當天,及其前366天數據,即是:1天 0點過後 1分鐘,刪除 當前時間前367天數據。

對 table3 插入當前一小時產生的數據,整點15分鐘後,插入當前時間前3個小時的數據

對table5 插入當天的數據,零點15 分鐘後,插入當前時間前一天的數據。

DELIMITER //

mysql > CREATE PROCEDURE demo_in_parameter(IN p_in int)

     -> BEGIN

    -> SELECT p_in; /*查詢輸入參數*/

    -> SET p_in=2; /*修改*/

    -> SELECT p_in; /*查看修改後的值*/

    -> END;

    -> //
     DELIMITER ;

0點1分刪除任務

drop procedure if exists  keepsize;
DELIMITER //
create procedure keepsize()
begin
   delete from tb_hour_reason_static where date(static_time) >= DATE_SUB(CURDATE(), INTERVAL 8 DAY);
   delete from tb_hour_allip_reason_static where date(static_time) >= DATE_SUB(CURDATE(), INTERVAL 32 DAY);
   delete from tb_day_whole_static where static_date >= DATE_SUB(CURDATE(), INTERVAL 367 DAY);
end;
//
DELIMITER ;
drop procedure if exists  inserttballip;
DELIMITER //
create procedure inserttballip()
begin
   insert into tb_hour_allip_reason_static(reason,static_time,shownum,clicknum) (select reason,static_time,sum(shownum),sum(clicknum) from tb_hour_reason_static where static_time>DATE_SUB(NOW(), INTERVAL 4 HOUR) group by reason,static_time);
end;
//
DELIMITER ;

3點15插入任務

drop procedure if exists  inserttbwhole;
DELIMITER //
create procedure inserttbwhole()
begin
   insert into tb_day_whole_static(static_date,shownum,clicknum) select static_date,sum(shownum) as shownum,sum(clicknum) as clicknum from v_day_allip_reason_static where static_date>=DATE_SUB(CURDATE(), INTERVAL 1 DAY) and reason regexp '^[0-9]*$' group by static_date;
end;
//
DELIMITER ;

事務:

drop event if exists clear_event;
create event clear_event
ON SCHEDULE EVERY 1 DAY STARTS '2017-07-20 03:15:00'
on completion preserve disable
do call  keepsize();
drop event if exists allip_event;
create event allip_event
ON SCHEDULE EVERY 1 HOUR STARTS '2017-07-20 20:15:00'
on completion preserve disable
do call inserttballip();
drop event if exists whole_event;
create event whole_event
ON SCHEDULE EVERY 1 DAY STARTS '2017-07-20 03:15:00'
on completion preserve disable
do call  inserttbwhole();

1) 臨時關閉事件
  

ALTER EVENT e_test DISABLE;

  
2) 開啓事件

開啓事件
ALTER EVENT allip_event ENABLE;
ALTER EVENT whole_event ENABLE;

********************
alter event allip_event ON  COMPLETION PRESERVE ENABLE; 

alter event whole_event ON  COMPLETION PRESERVE ENABLE; 

alter event clear_event ON  COMPLETION PRESERVE ENABLE;
************************
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章