Navicat10 mysql 觸發器 應用案例

2021-06-09 11:31:49 字數 3151 閱讀 6334

首先建立tab1,tab2兩個表 當對tab1進行新增/修改/刪除時 自動新增/修改/刪除到tab2

請按照範例依次推廣應用

一、常用範例

建立 tab1

create table `tab1` (

`id` int(11) not null auto_increment,

`tab1_id` varchar(11) default null,

primary key  (`id`)

) engine=innodb default charset=utf8;

建立 tab2

create table `tab2` (

`id` int(11) not null auto_increment,

`tb1_id` int(11) default null,

`tab2_id` varchar(255) default null,

primary key  (`id`)

) engine=innodb default charset=utf8;

這裡tab2的tb1_id與tab1的id關聯 即 tab2.tb1_id=tab1.id

在tab1的設計介面 新增觸發器 

1、新增tab1 自動新增到tab2

方法:t_afterinsert_on_tab1  觸發:after 選擇:插入

語句:insert into tab2(tb1_id,tab2_id) values(new.id,new.tab1_id)

2、更新tab1 的記錄 自動更新 tab2記錄

方法:t_afterupdate_on_tab1 觸發:after 選擇:更新

語句:update  tab2 set tab2.tab2_id=new.tab1_id  where tab2.tb1_id=old.id

3、刪除tab1的某條記錄tab2的對應記錄也刪除

方法:t_afterdelete_on_tab1 觸發:after  選擇:刪除

語句:delete from tab2 where tab2.tb1_id=old.id

tab1 ddl資訊:

create table `tab1` (

`id` int(11) not null auto_increment,

`tab1_id` varchar(11) default null,

primary key  (`id`)

) engine=innodb default charset=utf8;

create trigger `t_afterinsert_on_tab1` after insert on `tab1` for each row   insert into tab2(tb1_id,tab2_id) values(new.id,new.tab1_id);

create trigger `t_afterupdate_on_tab1` after update on `tab1` for each row  update  tab2 set tab2.tab2_id=new.tab1_id  where tab2.tb1_id=old.id;

create trigger `t_afterdelete_on_tab1` after delete on `tab1` for each row  delete from tab2 where tab2.tb1_id=old.id;

二、觸發器條件語句更新

create table `tab1` (

`id` int(11) not null auto_increment,

`age` int(11) default null,

`dd` varchar(255) default null,

`tab1_id` varchar(11) default null,

primary key  (`id`)

) engine=innodb default charset=utf8;

create table `tab2` (

`id` int(11) not null auto_increment,

`tb1_id` int(11) default null,

`tab2_id` varchar(255) default null,

`dd` varchar(255) default null,

primary key  (`id`)

) engine=innodb default charset=utf8;

說明:對錶tab1的age年齡判斷 age>=10 表tab2 的dd=1 當age<10 表tab2的dd=0

方法:t_beforeupdate_on_tab1  觸發 before  選擇:更新

語句: 

if(old.age>=10)then 

update  tab2 set tab2.tab2_id=new.tab1_id,tab2.dd='1'  where tab2.tb1_id=old.id;

end if

說明:執行語句2末尾必須有";"

三、當前表自判斷條件自更新

tab111

create table `tab111` (

`id` int(11) not null auto_increment,

`age` int(11) default null,

`dd` varchar(255) default null,

`tab1_id` varchar(11) default null,

primary key  (`id`)

) engine=innodb default charset=utf8;

說明:當前tab111自判斷當更新了age >=10 後 設定dd為1或0

方法:t_beforeupdate_on_tab111  觸發 before  選擇:更新

語句:if(new.age>=10)then

set new.dd = 1;

else

set new.dd = 0;

end if

注意:1、主動更新還是被動更新 2、注意同步更新次序

測試已成功

請使用者依此方法自行編制對應程式 

請注意 語句中的字首 new.    old.  分別代表資料在快取中的字段

其他應用請自己推演,祝你成功!

Navicat 連線 Mysql 出現1251錯誤

出現這個原因是mysql8 之前的版本中加密規則是mysql native password 解決辦法 先輸入 alter user root localhost identified by password password expire never 修改加密規則 再輸入alter user ro...

關於Navicat連不上mysql原因

1.在阿里雲伺服器開放3306埠 2.在my.ini配置加bind address 127.0.0.1 改為 bind address 0.0.0.0 3.在mysql客戶端執行命令 grant all privileges on to root identified by root密碼 with ...

使用Navicat遠端連線MySQL

原因是mysql的使用者預設是沒有遠端訪問許可權的,因此當訪問程式與資料庫不是在同一伺服器時,就無法訪問了。解決辦法有兩種 1.授權法 賦予任何主機訪問資料的許可權 root kerwin usr local mysql bin mysql u root p mysql grant all priv...