mysql 觸發器的創建 修改 刪除


//做一個簡單的練習,創建一個簡單的觸發器 完成添加文章的時候,自動加上時間,默認作者 為 ‘日記本的回憶‘

 

show columns from test; //查看表結構

 

//查看已存在觸發器

show  triggers  \G

//將結束符換成$

\d $

//創建觸發器, 用before 在插入的同時作用觸發器(同時也作用於 update 更新) ,並應用到test表的每一行,時間unix時間戳

create trigger insert_arc before insert on test
for each row
begin
    if new.nickname is null then
    set new.nickname='日記本的回憶';
    end if;
    if new.addtime is null then
        set new.addtime = unix_timestamp();
    end if;
end
$

// 插入測試數據

insert into test () values ()$

//select * from test $

 

 

 

//做一個簡單的練習,創建一個簡單的觸發器 完成添加文章的時候,自動加上時間,默認作者 為 ‘日記本的回憶‘

 

show columns from test; //查看表結構

 

//查看已存在觸發器

show  triggers  \G

//將結束符換成$

\d $

//創建觸發器, 用before 在插入的同時作用觸發器(同時也作用於 update 更新) ,並應用到test表的每一行,時間unix時間戳

create trigger insert_arc before insert on test
for each row
begin
    if new.nickname is null then
    set new.nickname='日記本的回憶';
    end if;
    if new.addtime is null then
        set new.addtime = unix_timestamp();
    end if;
end
$

// 插入測試數據

insert into test () values ()$

//select * from test $

 

完成!!!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM