created_at and updated_at that fill themselves

Let the database set the dates so every insert and update is stamped.

SQL · created_at and updated_at that fill themselvesBeginner
CREATE TABLE pages (
  id         INT AUTO_INCREMENT PRIMARY KEY,
  title      VARCHAR(120) NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT INTO pages (title) VALUES ('Home');
UPDATE pages SET title = 'Home page' WHERE id = 1;   -- updated_at changes by itself
timestampdefaulton update