1 Replies - 323 Views - Last Post: 10 April 2018 - 05:02 PM

#1 mookol   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 169
  • Joined: 31-May 15

MySql Triggers

Posted 09 April 2018 - 11:09 AM

Hello! I'm having trouble with triggers. I've created a trigger below, and I am expecting it to execute after the update on the city table, but its not and I am not getting any error messages suggesting why this might happen. Is there anyway someone can give me a hint? I am using the world database located here: https://dev.mysql.co...orld-setup/en/. Also I would like to insert this result into another table should I in the trigger add an update for another table inserting the new pop and old pop or should I do an update outside of the trigger?

DELIMITER $$

USE `world`$$

DROP TRIGGER /*!50032 IF EXISTS */ `test`$$

CREATE
    /*!50017 DEFINER = 'root'@'localhost' */
    TRIGGER `test` AFTER UPDATE ON `city` 
    FOR EACH ROW     BEGIN
      UPDATE country
	SET A.Population = A.Population + (city.NEW.Population - city.OLD.Population)
      WHERE  CODE = OLD.CountryCode;
    END;
$$

DELIMITER ;


After creating the trigger I ran this:

	UPDATE city
	SET Population = 1
	WHERE CountryCode = 'AFG';

    SELECT Population, CountryCode FROM city;

    Population	CountryCode
    1	AFG
    1	AFG
    1	AFG
    1	AFG


And the country population did not change:

    SELECT Population FROM country WHERE CODE = 'AFG';

    Population
    20387904

This post has been edited by mookol: 09 April 2018 - 11:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: MySql Triggers

#2 mookol   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 169
  • Joined: 31-May 15

Re: MySql Triggers

Posted 10 April 2018 - 05:02 PM

The trigger did work its the version of my problem was, I was not updating the right city column thus not making the trigger activate.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1