简体   繁体   中英

Trigger not updating my audit table. MySQL

Beginner problem,

I'm trying to get an 'after update' trigger to take the row's information and insert it into an audit table.

However, I cant seem to be able to do it. I don't get any error messages it just won't update so I'm a bit lost and can't find an answer to my problem.

DELIMITER //
CREATE TRIGGER Customers_After_Update AFTER UPDATE ON customers
FOR EACH ROW BEGIN  
    INSERT INTO audit_customers select * from customers where City = NEW.City;
END;//
DELIMITER ;

UPDATE customers
SET City='abc'
WHERE CustomerID = 'ALFKI'

Update: Here is how the tables are structured. ( This came from a school assignment)

CREATE TABLE `customers` (
   `CustomerID` varchar(5) NOT NULL,
   `CompanyName` varchar(40) NOT NULL,
   `ContactName` varchar(30) DEFAULT NULL,
   `ContactTitle` varchar(30) DEFAULT NULL,
   `Address` varchar(60) DEFAULT NULL,
   `City` varchar(15) DEFAULT NULL,
   `Region` varchar(15) DEFAULT NULL,
   `PostalCode` varchar(10) DEFAULT NULL,
   `Country` varchar(15) DEFAULT NULL,
   `Phone` varchar(24) DEFAULT NULL,
   `Fax` varchar(24) DEFAULT NULL,
   PRIMARY KEY (`CustomerID`),
   KEY `City` (`City`),
   KEY `CompanyName` (`CompanyName`),
   KEY `PostalCode` (`PostalCode`),
   KEY `Region` (`Region`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
 CREATE TABLE `audit_customers` (
   `Wijzigingsid` int(11) NOT NULL AUTO_INCREMENT,
   `CustomerID` varchar(5) NOT NULL,
   `CompanyName` varchar(40) NOT NULL,
   `Address` varchar(60) NOT NULL,
   `City` varchar(15) NOT NULL,
   `ModifiedDate` datetime NOT NULL,
   `Operation` varchar(12) NOT NULL,
   PRIMARY KEY (`Wijzigingsid`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1

Please share your create table structure to reproduce your problem. To me it looks like either the trigger failed to get created or there is some column data type mismtach which are some of the reasons why your trigger wouldn't populate data even after you had a successfull update. However this is even possible if you disable mysql's default auto commit and start your update using start transaction and if you miss to give commit from your end then technically the transaction is not commited to the database but the session you are connected to would see the changed data and that change is with reference to that session but trigger wouldn't apply as it is not commited to database. Give more details on your exact problem with table structure, confirmation on trigger's presense with show create trigger etc.

To mention the trigger you have as per the code shared is working and you can see it is working in my environment.

mysql> DELIMITER //
mysql> CREATE TRIGGER Customers_After_Update AFTER UPDATE ON sales
    -> FOR EACH ROW BEGIN
    ->     INSERT INTO audit_sales select * from sales where city = NEW.City;
    -> END;//
Query OK, 0 rows affected (0.01 sec)

mysql> DELIMITER ;
mysql>
mysql> create table audit_sales like sales;
Query OK, 0 rows affected (0.01 sec)

mysql> UPDATE sales
    -> SET City='LA'
    -> WHERE product_id = 1
    ->
    -> ;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql>
mysql>
mysql> select * from audit_sales;
+------------+--------------+----------+------+-------+
| product_id | retail_price | quantity | city | state |
+------------+--------------+----------+------+-------+
|          1 |            2 |        1 | LA   | CA    |
|          1 |            2 |        1 | LA   | CA    |
|          1 |            2 |        2 | LA   | CA    |
+------------+--------------+----------+------+-------+
3 rows in set (0.00 sec)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM