Mysql
 sql >> Database >  >> RDS >> Mysql

Valore predefinito del timestamp di MySQL a due colonne NOW ERROR 1067

Puoi averne solo uno colonna timestamp che per impostazione predefinita è CURRENT_TIMESTAMP o NOW() per tavolo. Questo è un bug ben noto in MySQL.

Per ovviare a questo, imposta come valore predefinito per la colonna creata un valore timestamp valido, quindi inserisci il timestamp nel codice dell'applicazione CRUD.Usa NOW() o CURRENT_TIMESTAMP per l'impostazione predefinita della colonna aggiornata.

Materiale di riferimento:http://dev.mysql.com/doc/ refman/5.1/en/timestamp.html

Per illustrare ulteriormente le carenze di MySQL in quest'area, considera il codice seguente:

CREATE TABLE testing_timestamps (
  id INT NOT NULL AUTO_INCREMENT,
  pk_id INT NOT NULL,
  col1 TIMESTAMP DEFAULT 0,
  col2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY(id)
);

delimiter $$
CREATE TRIGGER testing_timestamps_trigger
  AFTER INSERT ON testing_timestamps
  FOR EACH ROW 
  BEGIN
    UPDATE testing_timestamps SET col1 = NOW() WHERE id = MAX(id);
  END;
$$
delimiter ;

INSERT INTO testing_timestamps (id) VALUES (0);

L'output da questo visualizzerà:

mysql> INSERT INTO testing_timestamps (id) VALUES (0);
ERROR 1442 (HY000): Can't update table 'testing_timestamps' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

Questo è un peccato perché l'utilizzo di un trigger in questo caso sarebbe una buona soluzione.