trigger.result
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· RESULT 代码 · 共 1,965 行 · 第 1/4 页
RESULT
1,965 行
SET SESSION LOW_PRIORITY_UPDATES=DEFAULT;SET GLOBAL LOW_PRIORITY_UPDATES=DEFAULT;Bug#28502 Triggers that update another innodb table will blockon X lock unnecessarilyEnsure we do not open and lock tables for triggers we do not fire.drop table if exists t1, t2;drop trigger if exists trg_bug28502_au;create table t1 (id int, count int);create table t2 (id int);create trigger trg_bug28502_au before update on t2for each rowbeginif (new.id is not null) thenupdate t1 set count= count + 1 where id = old.id;end if;end|insert into t1 (id, count) values (1, 0);lock table t1 write;insert into t2 set id=1;unlock tables;update t2 set id=1 where id=1;select * from t1;id count1 1select * from t2;id1drop table t1, t2;Additionally, provide test coverage for triggers and all MySQL data changing commands.drop table if exists t1, t2, t1_op_log;drop view if exists v1;drop trigger if exists trg_bug28502_bi;drop trigger if exists trg_bug28502_ai;drop trigger if exists trg_bug28502_bu;drop trigger if exists trg_bug28502_au;drop trigger if exists trg_bug28502_bd;drop trigger if exists trg_bug28502_ad;create table t1 (id int primary key auto_increment, operation varchar(255));create table t2 (id int primary key);create table t1_op_log(operation varchar(255));create view v1 as select * from t1;create trigger trg_bug28502_bi before insert on t1for each rowinsert into t1_op_log (operation)values (concat("Before INSERT, new=", new.operation));create trigger trg_bug28502_ai after insert on t1for each rowinsert into t1_op_log (operation)values (concat("After INSERT, new=", new.operation));create trigger trg_bug28502_bu before update on t1for each rowinsert into t1_op_log (operation)values (concat("Before UPDATE, new=", new.operation,", old=", old.operation));create trigger trg_bug28502_au after update on t1for each rowinsert into t1_op_log (operation)values (concat("After UPDATE, new=", new.operation,", old=", old.operation));create trigger trg_bug28502_bd before delete on t1for each rowinsert into t1_op_log (operation)values (concat("Before DELETE, old=", old.operation));create trigger trg_bug28502_ad after delete on t1for each rowinsert into t1_op_log (operation)values (concat("After DELETE, old=", old.operation));insert into t1 (operation) values ("INSERT");set @id=last_insert_id();select * from t1;id operation1 INSERTselect * from t1_op_log;operationBefore INSERT, new=INSERTAfter INSERT, new=INSERTtruncate t1_op_log;update t1 set operation="UPDATE" where id=@id;select * from t1;id operation1 UPDATEselect * from t1_op_log;operationBefore UPDATE, new=UPDATE, old=INSERTAfter UPDATE, new=UPDATE, old=INSERTtruncate t1_op_log;delete from t1 where id=@id;select * from t1;id operationselect * from t1_op_log;operationBefore DELETE, old=UPDATEAfter DELETE, old=UPDATEtruncate t1;truncate t1_op_log;insert into t1 (id, operation) values(NULL, "INSERT ON DUPLICATE KEY UPDATE, inserting a new key")on duplicate key update id=NULL, operation="Should never happen";set @id=last_insert_id();select * from t1;id operation1 INSERT ON DUPLICATE KEY UPDATE, inserting a new keyselect * from t1_op_log;operationBefore INSERT, new=INSERT ON DUPLICATE KEY UPDATE, inserting a new keyAfter INSERT, new=INSERT ON DUPLICATE KEY UPDATE, inserting a new keytruncate t1_op_log;insert into t1 (id, operation) values(@id, "INSERT ON DUPLICATE KEY UPDATE, the key value is the same")on duplicate key update id=NULL,operation="INSERT ON DUPLICATE KEY UPDATE, updating the duplicate";select * from t1;id operation0 INSERT ON DUPLICATE KEY UPDATE, updating the duplicateselect * from t1_op_log;operationBefore INSERT, new=INSERT ON DUPLICATE KEY UPDATE, the key value is the sameBefore UPDATE, new=INSERT ON DUPLICATE KEY UPDATE, updating the duplicate, old=INSERT ON DUPLICATE KEY UPDATE, inserting a new keyAfter UPDATE, new=INSERT ON DUPLICATE KEY UPDATE, updating the duplicate, old=INSERT ON DUPLICATE KEY UPDATE, inserting a new keytruncate t1;truncate t1_op_log;replace into t1 values (NULL, "REPLACE, inserting a new key");set @id=last_insert_id();select * from t1;id operation1 REPLACE, inserting a new keyselect * from t1_op_log;operationBefore INSERT, new=REPLACE, inserting a new keyAfter INSERT, new=REPLACE, inserting a new keytruncate t1_op_log;replace into t1 values (@id, "REPLACE, deleting the duplicate");select * from t1;id operation1 REPLACE, deleting the duplicateselect * from t1_op_log;operationBefore INSERT, new=REPLACE, deleting the duplicateBefore DELETE, old=REPLACE, inserting a new keyAfter DELETE, old=REPLACE, inserting a new keyAfter INSERT, new=REPLACE, deleting the duplicatetruncate t1;truncate t1_op_log;create table if not exists t1select NULL, "CREATE TABLE ... SELECT, inserting a new key";Warnings:Note 1050 Table 't1' already existsset @id=last_insert_id();select * from t1;id operation1 CREATE TABLE ... SELECT, inserting a new keyselect * from t1_op_log;operationBefore INSERT, new=CREATE TABLE ... SELECT, inserting a new keyAfter INSERT, new=CREATE TABLE ... SELECT, inserting a new keytruncate t1_op_log;create table if not exists t1 replaceselect @id, "CREATE TABLE ... REPLACE SELECT, deleting a duplicate key";Warnings:Note 1050 Table 't1' already existsselect * from t1;id operation1 CREATE TABLE ... REPLACE SELECT, deleting a duplicate keyselect * from t1_op_log;operationBefore INSERT, new=CREATE TABLE ... REPLACE SELECT, deleting a duplicate keyBefore DELETE, old=CREATE TABLE ... SELECT, inserting a new keyAfter DELETE, old=CREATE TABLE ... SELECT, inserting a new keyAfter INSERT, new=CREATE TABLE ... REPLACE SELECT, deleting a duplicate keytruncate t1;truncate t1_op_log;insert into t1 (id, operation)select NULL, "INSERT ... SELECT, inserting a new key";set @id=last_insert_id();select * from t1;id operation1 INSERT ... SELECT, inserting a new keyselect * from t1_op_log;operationBefore INSERT, new=INSERT ... SELECT, inserting a new keyAfter INSERT, new=INSERT ... SELECT, inserting a new keytruncate t1_op_log;insert into t1 (id, operation)select @id,"INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate"on duplicate key update id=NULL,operation="INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate";select * from t1;id operation0 INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicateselect * from t1_op_log;operationBefore INSERT, new=INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicateBefore UPDATE, new=INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate, old=INSERT ... SELECT, inserting a new keyAfter UPDATE, new=INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate, old=INSERT ... SELECT, inserting a new keytruncate t1;truncate t1_op_log;replace into t1 (id, operation)select NULL, "REPLACE ... SELECT, inserting a new key";set @id=last_insert_id();select * from t1;id operation1 REPLACE ... SELECT, inserting a new keyselect * from t1_op_log;operationBefore INSERT, new=REPLACE ... SELECT, inserting a new keyAfter INSERT, new=REPLACE ... SELECT, inserting a new keytruncate t1_op_log;replace into t1 (id, operation)select @id, "REPLACE ... SELECT, deleting a duplicate";select * from t1;id operation1 REPLACE ... SELECT, deleting a duplicateselect * from t1_op_log;operationBefore INSERT, new=REPLACE ... SELECT, deleting a duplicateBefore DELETE, old=REPLACE ... SELECT, inserting a new keyAfter DELETE, old=REPLACE ... SELECT, inserting a new keyAfter INSERT, new=REPLACE ... SELECT, deleting a duplicatetruncate t1;truncate t1_op_log;insert into t1 (id, operation) values (1, "INSERT for multi-DELETE");insert into t2 (id) values (1);delete t1.*, t2.* from t1, t2 where t1.id=1;select * from t1;id operationselect * from t2;idselect * from t1_op_log;operationBefore INSERT, new=INSERT for multi-DELETEAfter INSERT, new=INSERT for multi-DELETEBefore DELETE, old=INSERT for multi-DELETEAfter DELETE, old=INSERT for multi-DELETEtruncate t1;truncate t2;truncate t1_op_log;insert into t1 (id, operation) values (1, "INSERT for multi-UPDATE");insert into t2 (id) values (1);update t1, t2 set t1.id=2, operation="multi-UPDATE" where t1.id=1;update t1, t2set t2.id=3, operation="multi-UPDATE, SET for t2, but the trigger is fired" where t1.id=2;select * from t1;id operation2 multi-UPDATE, SET for t2, but the trigger is firedselect * from t2;id3select * from t1_op_log;operationBefore INSERT, new=INSERT for multi-UPDATEAfter INSERT, new=INSERT for multi-UPDATEBefore UPDATE, new=multi-UPDATE, old=INSERT for multi-UPDATEAfter UPDATE, new=multi-UPDATE, old=INSERT for multi-UPDATEBefore UPDATE, new=multi-UPDATE, SET for t2, but the trigger is fired, old=multi-UPDATEAfter UPDATE, new=multi-UPDATE, SET for t2, but the trigger is fired, old=multi-UPDATEtruncate table t1;truncate table t2;truncate table t1_op_log;Now do the same but use a view instead of the base table.insert into v1 (operation) values ("INSERT");set @id=last_insert_id();select * from t1;id operation1 INSERTselect * from t1_op_log;operationBefore INSERT, new=INSERTAfter INSERT, new=INSERTtruncate t1_op_log;update v1 set operation="UPDATE" where id=@id;select * from t1;id operation1 UPDATEselect * from t1_op_log;operationBefore UPDATE, new=UPDATE, old=INSERTAfter UPDATE, new=UPDATE, old=INSERTtruncate t1_op_log;delete from v1 where id=@id;select * from t1;id operationselect * from t1_op_log;operationBefore DELETE, old=UPDATEAfter DELETE, old=UPDATEtruncate t1;truncate t1_op_log;insert into v1 (id, operation) values(NULL, "INSERT ON DUPLICATE KEY UPDATE, inserting a new key")on duplicate key update id=NULL, operation="Should never happen";set @id=last_insert_id();select * from t1;id operation1 INSERT ON DUPLICATE KEY UPDATE, inserting a new keyselect * from t1_op_log;operationBefore INSERT, new=INSERT ON DUPLICATE KEY UPDATE, inserting a new keyAfter INSERT, new=INSERT ON DUPLICATE KEY UPDATE, inserting a new keytruncate t1_op_log;insert into v1 (id, operation) values(@id, "INSERT ON DUPLICATE KEY UPDATE, the key value is the same")on duplicate key update id=NULL,operation="INSERT ON DUPLICATE KEY UPDATE, updating the duplicate";select * from t1;id operation0 INSERT ON DUPLICATE KEY UPDATE, updating the duplicateselect * from t1_op_log;operationBefore INSERT, new=INSERT ON DUPLICATE KEY UPDATE, the key value is the sameBefore UPDATE, new=INSERT ON DUPLICATE KEY UPDATE, updating the duplicate, old=INSERT ON DUPLICATE KEY UPDATE, inserting a new keyAfter UPDATE, new=INSERT ON DUPLICATE KEY UPDATE, updating the duplicate, old=INSERT ON DUPLICATE KEY UPDATE, inserting a new keytruncate t1;truncate t1_op_log;replace into v1 values (NULL, "REPLACE, inserting a new key");set @id=last_insert_id();select * from t1;id operation1 REPLACE, inserting a new keyselect * from t1_op_log;operationBefore INSERT, new=REPLACE, inserting a new keyAfter INSERT, new=REPLACE, inserting a new keytruncate t1_op_log;replace into v1 values (@id, "REPLACE, deleting the duplicate");select * from t1;id operation1 REPLACE, deleting the duplicateselect * from t1_op_log;operationBefore INSERT, new=REPLACE, deleting the duplicateBefore DELETE, old=REPLACE, inserting a new keyAfter DELETE, old=REPLACE, inserting a new keyAfter INSERT, new=REPLACE, deleting the duplicatetruncate t1;truncate t1_op_log;create table if not exists v1select NULL, "CREATE TABLE ... SELECT, inserting a new key";Warnings:Note 1050 Table 'v1' already existsset @id=last_insert_id();select * from t1;id operation1 CREATE TABLE ... SELECT, inserting a new keyselect * from t1_op_log;operationBefore INSERT, new=CREATE TABLE ... SELECT, inserting a new keyAfter INSERT, new=CREATE TABLE ... SELECT, inserting a new keytruncate t1_op_log;create table if not exists v1 replaceselect @id, "CREATE TABLE ... REPLACE SELECT, deleting a duplicate key";Warnings:Note 1050 Table 'v1' already existsselect * from t1;id operation1 CREATE TABLE ... REPLACE SELECT, deleting a duplicate keyselect * from t1_op_log;operationBefore INSERT, new=CREATE TABLE ... REPLACE SELECT, deleting a duplicate keyBefore DELETE, old=CREATE TABLE ... SELECT, inserting a new keyAfter DELETE, old=CREATE TABLE ... SELECT, inserting a new keyAfter INSERT, new=CREATE TABLE ... REPLACE SELECT, deleting a duplicate keytruncate t1;truncate t1_op_log;insert into v1 (id, operation)select NULL, "INSERT ... SELECT, inserting a new key";set @id=last_insert_id();select * from t1;id operation1 INSERT ... SELECT, inserting a new keyselect * from t1_op_log;operationBefore INSERT, new=INSERT ... SELECT, inserting a new keyAfter INSERT, new=INSERT ... SELECT, inserting a new keytruncate t1_op_log;insert into v1 (id, operation)select @id,"INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate"on duplicate key update id=NULL,operation="INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate";select * from t1;id operation0 INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicateselect * from t1_op_log;operationBefore INSERT, new=INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicateBefore UPDATE, new=INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate, old=INSERT ... SELECT, inserting a new keyAfter UPDATE, new=INSERT ... SELECT ... ON DUPLICATE KEY UPDATE, updating a duplicate, old=INSERT ... SELECT, inserting a new keytruncate t1;truncate t1_op_log;replace into v1 (id, operation)select NULL, "REPLACE ... SELECT, inserting a new key";set @id=last_insert_id();select * from t1;id operation1 REPLACE ... SELECT, inserting a new keyselect * from t1_op_log;operationBefore INSERT, new=REPLACE ... SELECT, inserting a new keyAfter INSERT, new=REPLACE ... SELECT, inserting a new keytruncate t1_op_log;replace into v1 (id, operation)select @id, "REPLACE ... SELECT, deleting a duplicate";select * from t1;id operation1 REPLACE ... SELECT, deleting a duplicateselect * from t1_op_log;operationBefore INSERT, new=REPLACE ... SELECT, deleting a duplicateBefore DELETE, old=REPLACE ... SELECT, inserting a new keyAfter DELETE, old=REPLACE ... SELECT, inserting a new keyAfter INSERT, new=REPLACE ... SELECT, deleting a duplicatetruncate t1;truncate t1_op_log;insert into v1 (id, operation) values (1, "INSERT for multi-DELETE");insert into t2 (id) values (1);delete v1.*, t2.* from v1, t2 where v1.id=1;select * from t1;id operationselect * from t2;idselect * from t1_op_log;operationBefore INSERT, new=INSERT for multi-DELETEAfter INSERT, new=INSERT for multi-DELETEBefore DELETE, old=INSERT for multi-DELETEAfter DELETE, old=INSERT for multi-DELETEtruncate t1;truncate t2;truncate t1_op_log;insert into v1 (id, operation) values (1, "INSERT for multi-UPDATE");insert into t2 (id) values (1);update v1, t2 set v1.id=2, operation="multi-UPDATE" where v1.id=1;update v1, t2set t2.id=3, operation="multi-UPDATE, SET for t2, but the trigger is fired" where v1.id=2;select * from t1;id operation2 multi-UPDATE, SET for t2, but the trigger is firedselect * from t2;id3select * from t1_op_log;operationBefore INSERT, new=INSERT for multi-UPDATEAfter INSERT, new=INSERT for multi-UPDATEBefore UPDATE, new=multi-UPDATE, old=INSERT for multi-UPDATEAfter UPDATE, new=multi-UPDATE, old=INSERT for multi-UPDATEBefore UPDATE, new=multi-UPDATE, SET for t2, but the trigger is fired, old=multi-UPDATEAfter UPDATE, new=multi-UPDATE, SET for t2, but the trigger is fired, old=multi-UPDATEdrop view v1;drop table t1, t2, t1_op_log;Bug#27248 Triggers: error if insert affects temporary tableThe bug was fixed by the fix for Bug#26141drop table if exists t1;drop temporary table if exists t2;create table t1 (s1 int);create temporary table t2 (s1 int);create trigger t1_bi before insert on t1 for each row insert into t2 values (0);create trigger t1_bd before delete on t1 for each row delete from t2;insert into t1 values (0);insert into t1 values (0);select * from t1;s100select * from t2;s100delete from t1;select * from t1;s1select * from t2;s1drop table t1;drop temporary table t2;End of 5.0 tests
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?