若何測試mysql觸發器和存儲進程。本站提示廣大學習愛好者:(若何測試mysql觸發器和存儲進程)文章只能為提供參考,不一定能成為您想要的結果。以下是若何測試mysql觸發器和存儲進程正文
1. 為了測試觸發器和存儲進程,起首樹立一張簡略的表:
CREATE TABLE `airuser` (
`userId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(128) NOT NULL,
PRIMARY KEY (`userId`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8
2. 為該表的拔出操作,創立一張記載表:
CREATE TABLE `airuser_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(45) DEFAULT NULL,
`edittime` timestamp NULL DEFAULT NULL,
`edittype` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
3. 編寫一個拔出操作的觸發器:
DROP TRIGGER insert_trigger;
delimiter |
CREATE TRIGGER insert_trigger BEFORE INSERT ON airuser
FOR EACH ROW BEGIN
INSERT INTO airuser_record SET username = NEW.username, edittime=now(), edittype='insert';
END;
SHOW TRIGGERS;
4. 為批量拔出編寫存儲進程:
DROP procedure createUsers;
delimiter |
create procedure createUsers(IN count int)
begin
declare i int;
set i=0;
while i<count do
insert into airuser set username=concat('user_',i);
set i=i+1;
end while;
end;
show procedure status;
5. 挪用存儲進程,驗證存儲進程是任務的,並驗證在拔出記載前,觸發器能准確被觸發:
call createUsers(10);
6. 最初經由過程拔出記載表再次驗證:
SELECT * FROM mars_jpa.airuser_record;