Skip to content
  • Karthik Kamath's avatar
    4e88dd3f
    BUG#25209512: CURRENT_TIMESTAMP PRODUCES ZEROS IN TRIGGER · 4e88dd3f
    Karthik Kamath authored
    ANALYSIS:
    =========
    Zero date(0000-00-00 00:00:00) is generated by the
    'BEFORE INSERT' trigger defined on a table with TIMESTAMP
    or DATETIME field having the default as CURRENT_TIMESTAMP
    or NOW().
    
    Similar behavior is observed with LOAD INFILE queries and
    also with BEFORE UPDATE triggers.
    
    Whenever a 'BEFORE INSERT' trigger is defined on a table
    and an insert happens on it, the trigger body is executed
    first and then the new record is written into the table.
    If the trigger body tries to utilize the default values
    of the fields in the table, the values used will be
    incorrect.
    
    Brief working of 'AFTER INSERT' triggers:
    1. Set function defaults for the table.
    2. Write record into the table.
    3. Process the trigger defined on it.
      3.1 Execute the trigger body.
    
    Brief working of 'BEFORE INSERT' triggers:
    1. Process triggers defined on the table.
      1.1 Execute the trigger body.
    2. Set function defaults for the table.
    3. Write record into the table.
    
    Hence when a 'BEFORE INSERT' trigger is defined for a
    table, the evaluation of default functions is not done for
    the table before processing the trigger. Thus the default
    values of the table are not updated and if the trigger body
    tries to utilize these values, incorrect result will be
    obtained.
    
    FIX:
    ====
    We are now evaluating the function defaults for a table
    before processing the 'BEFORE INSERT' or 'BEFORE UPDATE'
    triggers defined on it.
    4e88dd3f
    BUG#25209512: CURRENT_TIMESTAMP PRODUCES ZEROS IN TRIGGER
    Karthik Kamath authored
    ANALYSIS:
    =========
    Zero date(0000-00-00 00:00:00) is generated by the
    'BEFORE INSERT' trigger defined on a table with TIMESTAMP
    or DATETIME field having the default as CURRENT_TIMESTAMP
    or NOW().
    
    Similar behavior is observed with LOAD INFILE queries and
    also with BEFORE UPDATE triggers.
    
    Whenever a 'BEFORE INSERT' trigger is defined on a table
    and an insert happens on it, the trigger body is executed
    first and then the new record is written into the table.
    If the trigger body tries to utilize the default values
    of the fields in the table, the values used will be
    incorrect.
    
    Brief working of 'AFTER INSERT' triggers:
    1. Set function defaults for the table.
    2. Write record into the table.
    3. Process the trigger defined on it.
      3.1 Execute the trigger body.
    
    Brief working of 'BEFORE INSERT' triggers:
    1. Process triggers defined on the table.
      1.1 Execute the trigger body.
    2. Set function defaults for the table.
    3. Write record into the table.
    
    Hence when a 'BEFORE INSERT' trigger is defined for a
    table, the evaluation of default functions is not done for
    the table before processing the trigger. Thus the default
    values of the table are not updated and if the trigger body
    tries to utilize these values, incorrect result will be
    obtained.
    
    FIX:
    ====
    We are now evaluating the function defaults for a table
    before processing the 'BEFORE INSERT' or 'BEFORE UPDATE'
    triggers defined on it.
Loading