2017-02-04 56 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