2017-02-04 104 views
0

我想在插入时使同一个表“clients”大写的2个字段(名字&姓氏)大写。mysql - 触发器,多个语句相同的事件和时间

mysql> show triggers LIKE 'clients'\G; 
*************************** 1. row *************************** 
      Trigger: ucase_insert 
       Event: INSERT 
       Table: tlc 
      Statement: SET NEW.firstname = upper(NEW.firstname) 
       Timing: BEFORE 
      Created: 2017-02-04 11:53:45.87 
      sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 
      Definer: [email protected]% 
character_set_client: utf8mb4 
collation_connection: utf8mb4_unicode_ci 
    Database Collation: utf8_general_ci 

我使用phpmyadmin作为接口。看来,我不能再添加第二个陈述,如...

  Statement: SET NEW.lastname = upper(NEW.lastname) 

......这应该触发与第一个相同的事件和时间。

这可能吗?

回答

0

我找到了解决方案。通过BEGIN/END,我可以包含多个语句。似乎微不足道,但花了我一段时间才发现...

mysql> show triggers LIKE 'clients'\G; 
*************************** 1. row *************************** 
      Trigger: ucase_insert 
       Event: INSERT 
       Table: tlc 
      Statement: BEGIN 
SET NEW.firstname = upper(NEW.firstname); 
SET NEW.lastname = upper(NEW.lastname); 
END 
      Timing: BEFORE 
      Created: 2017-02-04 11:53:45.87 
      sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 
      Definer: [email protected]% 
character_set_client: utf8mb4 
collation_connection: utf8mb4_unicode_ci 
    Database Collation: utf8_general_ci