What does this MySQL trigger do?

Discussion in 'MySQL' started by healthy, Feb 9, 2010.

  1. #1
    I have two versions of the same DB. The original has code for triggers. The second is a copy of the original without the triggers.

    With the original DB, the application fails to post data on submit, but data seems to post correctly to the copy.

    Here's the trigger code:
    
    DROP TRIGGER IF EXISTS `uat`.`updateConditionCCRDataObjectID`;
    DELIMITER //
    CREATE TRIGGER `uat`.`updateConditionCCRDataObjectID` BEFORE INSERT ON `uat`.`condition_details` 
     FOR EACH ROW BEGIN
    	IF NEW.CCRDataObjectID = NULL OR NEW.CCRDataObjectID = '' THEN
    		SELECT coalesce(MAX(id)+1,1) into @IDVal from condition_details;
    		SET NEW.CCRDataObjectID = CONCAT('Comp-Condition-',@IDVal);
    	END IF;
        END
    //
    DELIMITER ;
    Code (markup):
    Could someone explain what the trigger does and perhaps provide an opinion on whether it's necessary?

    Thanks
     
    healthy, Feb 9, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Basically, when you insert a new row into the `condition_details` table, the trigger looks at the value of the inserted CCRDataObjectID column. If that value is NULL or empty '', it selects the max, non null (coalesce), id value and increments by 1. It puts this value into the CCRDataObjectID column.

    My guess it that it is doing this to preserve some sort of manual incrementation and table association for each submitted row. Without seeing a lot more about the app and the database I couldn't tell you if this is required, but from my experience with apps, this is most likely essential, and shouldn't be removed.
     
    jestep, Feb 9, 2010 IP
  3. healthy

    healthy Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks much jesep. :D

    I'm surprised that the code did not copy to new DBs with phpMyAdmin's copy function. Do you have any quick hints on how to copy the trigger code?
     
    healthy, Feb 9, 2010 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    You need to run the trigger codes in a sql prompt. Just run exactly what you posted and it should go into the copied database.

    If the code was originally failing on inserts you will need to debug what is failing.
     
    jestep, Feb 9, 2010 IP
  5. healthy

    healthy Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The code contains both DB and table names such as `uat`.`updateConditionCCRDataObjectID`. Obviously for other DBs I'll have to change the DB name. However, is the DB name necessary? Could I just use `updateConditionCCRDataObjectID` without `uat`?

    Thanks again.
     
    healthy, Feb 9, 2010 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    Normally you would be fine without having the db name in the query. Just make sure you are using the correct database when you run the query.

    If you are logged into a specific database via phpmyadmin, you should be fine.
    Otherwise,

    run

    use myDbName;
     
    jestep, Feb 9, 2010 IP
  7. healthy

    healthy Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks Jestep. Now all I have to do is figure out why it wasn't working! The triggers may not be the problem.
     
    healthy, Feb 9, 2010 IP
  8. healthy

    healthy Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    A couple of other questions.

    1. Is there a way to see the trigger code in phpMyAdmin?

    2. Is there a way to remove the trigger code in phpMyAdmin?
     
    healthy, Feb 10, 2010 IP