mysql視圖/存儲過程/函數/事件/觸發器

--語法參考:https://dev.mysql.com/doc/   (當前用的是5.6)html

https://dev.mysql.com/doc/refman/5.6/en/sql-syntax-data-manipulation.htmlmysql

--視圖sql

CREATE VIEW test.v AS SELECT * FROM t; session

 --存儲過程函數

CREATE
    [DEFINER = { user | CURRENT_USER }]
    PROCEDURE sp_name ([proc_parameter[,...]])
    [characteristic ...] routine_bodyspa

proc_parameter:
    [ IN | OUT | INOUT ] param_name type    日誌

type:
    Any valid MySQL data typehtm

characteristic:
    COMMENT 'string'
  | LANGUAGE SQL
  | [NOT] DETERMINISTIC
  | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
  | SQL SECURITY { DEFINER | INVOKER }事件

routine_body:
    Valid SQL routine statementip

--新建存儲過程

delimiter //
CREATE PROCEDURE simpleproc (OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END//
delimiter ;

--調用存儲過程
CALL simpleproc(@a);    

--函數
CREATE
    [DEFINER = { user | CURRENT_USER }]
    FUNCTION sp_name ([func_parameter[,...]])
    RETURNS type
    [characteristic ...] routine_body

func_parameter:
    param_name type

type:
    Any valid MySQL data type

characteristic:
    COMMENT 'string'
  | LANGUAGE SQL
  | [NOT] DETERMINISTIC
  | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
  | SQL SECURITY { DEFINER | INVOKER }

routine_body:
    Valid SQL routine statement

--新建函數
CREATE FUNCTION hello (s CHAR(20))
    RETURNS CHAR(50) DETERMINISTIC
    RETURN CONCAT('Hello, ',s,'!');
--使用函數
SELECT hello('world');

 

--事件
CREATE
    [DEFINER = { user | CURRENT_USER }]
    EVENT
    [IF NOT EXISTS]
    event_name
    ON SCHEDULE schedule
    [ON COMPLETION [NOT] PRESERVE]
    [ENABLE | DISABLE | DISABLE ON SLAVE]
    [COMMENT 'string']
    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}

用法一:
CREATE EVENT myevent
    ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
    DO
      UPDATE myschema.mytable SET mycol = mycol + 1;


用法二:
CREATE EVENT e_totals
    ON SCHEDULE AT '2006-02-10 23:59:00'
    DO INSERT INTO test.totals VALUES (NOW());      

用法三:
delimiter |

CREATE EVENT e_daily
    ON SCHEDULE
      EVERY 1 DAY
    COMMENT 'Saves total number of sessions then clears the table each day'
    DO
      BEGIN
        INSERT INTO site_activity.totals (time, total)
          SELECT CURRENT_TIMESTAMP, COUNT(*)
            FROM site_activity.sessions;
        DELETE FROM site_activity.sessions;
      END |

delimiter ;

用法四:
CREATE EVENT e_call_myproc
    ON SCHEDULE
      AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
    DO CALL myproc(5, 27);

 

--觸發器
    
CREATE
    [DEFINER = { user | CURRENT_USER }]
    TRIGGER trigger_name
    trigger_time trigger_event
    ON tbl_name FOR EACH ROW
    trigger_body

trigger_time: { BEFORE | AFTER }  #BEFORE:先觸發觸發器,再執行操做 | AFTER:執行完操做再觸發觸發器

trigger_event: { INSERT | UPDATE | DELETE }
    
    

CREATE TABLE classification_log
(
    id BIGINT(20),
    note varchar(128),
    utime datetime default NOW()
)
--插入時,觸發器記錄日誌記錄日誌
CREATE TRIGGER tri_classification
AFTER INSERT ON document_classification
FOR EACH ROW
BEGIN

INSERT classification_log(id , note)
VALUES(new.id,'新插入id');
END;
    
--修改時,觸發器記錄日誌記錄日誌
CREATE TRIGGER tri_classificationx3
AFTER UPDATE ON classification_log2
FOR EACH ROW
BEGIN

INSERT classification_log(id , note)
VALUES(old.id,'修改前');

INSERT classification_log(id , note)
VALUES(new.id,'修改後');
END;
    
--刪除時,觸發器記錄日誌記錄日誌
CREATE TRIGGER tri_classificationx4
AFTER DELETE ON classification_log2
FOR EACH ROW
BEGIN

INSERT classification_log(id , note)
VALUES(old.id,'刪除前');

END;

相關文章
相關標籤/搜索