DELIMITER // CREATETRIGGER create_uuid_for_loginmain BEFORE INSERTON loginmain FOREACHROW BEGIN IF NEW.idx ='none'THEN SET NEW.idx = UUID(); END IF; END; //
當變更 (update) 時自動更新 upddate。
1 2 3 4 5 6 7 8
DELIMITER // CREATETRIGGER update_upddate_for_loginmain BEFORE UPDATEON loginmain FOREACHROW BEGIN SET NEW.upddate = now(); END; //
這裡要注意的是為什麼不 AFTER UPDATE 而是 BEFORE UPDATE 呢? 原因在於 AFTER UPDATE 的 NEW 與 OLD 新舊值是不能變動的,但是 BEFORE UPDATE 是可以改變 NEW 值(因為還沒真正更新) 這樣就可以在更新時更新 upddate 欄位了。
CREATE TABLE `logindate` ( `idx` char(36) NOT NULLDEFAULT'none', `lm_idx` char(36) NOT NULL, `wkdnum` int(11) NOT NULL, `dfrom` timeNOT NULLDEFAULT'00:00:00', `dto` timeNOT NULLDEFAULT'23:59:59', `crtdate` datetime NOT NULLDEFAULTCURRENT_TIMESTAMP, `upddate` datetime DEFAULTNULL, PRIMARY KEY (`idx`) );
DELIMITER // CREATETRIGGER create_uuid_for_logindate BEFORE INSERTON logindate FOREACHROW BEGIN IF NEW.idx ='none'THEN SET NEW.idx = UUID(); END IF; END;
CREATETRIGGER update_upddate_for_logindate BEFORE UPDATEON logindate FOREACHROW BEGIN SET NEW.upddate = now(); END; //
DELIMITER // CREATEPROCEDURE check_login() SQL SECURITY DEFINER BEGIN
SET@idx=''; SET@idx2='';
select idx into@idxfrom master.loginmain where username = SUBSTRING_INDEX(SUBSTRING_INDEX(user(), '@', 1), '@', -1) and srcip like CONCAT('%', SUBSTRING_INDEX(SUBSTRING_INDEX(user(), '@', 2), '@', -1), '%') ; if @idx=''then SIGNAL SQLSTATE'45000'SET MYSQL_ERRNO=30001, MESSAGE_TEXT='無法在資料庫中找到您的登入帳號、來源IP、或是使用之程式不正確。'; else select idx into@idx2from master.logindate where lm_idx =@idxand wkdnum = dayofweek(now())-1and (cast(now() astime) between dfrom and dto); if @idx2=''then SIGNAL SQLSTATE'45000'SET MYSQL_ERRNO=30002, MESSAGE_TEXT='您的帳號無法在此時段登入。'; end if; end if; END; //
select idx into @idx from master.loginmain where username = SUBSTRING_INDEX(SUBSTRING_INDEX(user(), '@', 1), '@', -1) and srcip like CONCAT('%', SUBSTRING_INDEX(SUBSTRING_INDEX(user(), '@', 2), '@', -1), '%') ; 這段是把 username@host 拆開來分別比對。
C:\Users\Administrator>mysql -u sujunmin -p Enter password: ************ Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 10.1.14-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select true from dual; +------+ | TRUE | +------+ | 1 | +------+ 1 row in set (0.00 sec)
MariaDB [(none)]> quit Bye
C:\Users\Administrator>mysql -u test -p Enter password: ************ Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 10.1.14-MariaDB
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select true from dual; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 6 Current database: *** NONE ***
C:\Users\Administrator>mysql -u test -p Enter password: ************ Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 7 Server version: 10.1.14-MariaDB
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select true from dual; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 8 Current database: *** NONE ***
C:\Users\Administrator>mysql -u test -p Enter password: ************ Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.1.14-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select true from dual; +------+ | TRUE | +------+ | 1 | +------+ 1 row in set (0.00 sec)
If a statement has been specified for the init-connect mysqld option, that command will not be executed when a user with SUPER privileges connects to the server.
難怪發現怎麼我的帳號鎖不住@@
在 MariaDB 裡沒有 alter procedure 內容這回事,要的話就要先 DROP 再 CREATE,記得權限要重給。
However, you cannot change the parameters or body of a stored procedure using this statement; to make such changes, you must drop and re-create the procedure using DROP PROCEDURE and CREATE PROCEDURE.