Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • MariaDB Cookbook
  • Toc
  • feedback
MariaDB Cookbook

MariaDB Cookbook

By : Daniel Bartholomew
4.2 (9)
close
MariaDB Cookbook

MariaDB Cookbook

4.2 (9)
By: Daniel Bartholomew

Overview of this book

Table of Contents (20 chapters)
close
MariaDB Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Updating DATETIME and TIMESTAMP columns automatically


If our database has a DATETIME or TIMESTAMP column that we want to be updated whenever the record is updated, there is no need for us to put that logic in our application. MariaDB can take care of it for us.

How to do it...

  1. Launch the mysql command-line client application and connect to our MariaDB server.

  2. Create a test database if it doesn't already exist and switch to it using the following command:

    CREATE DATABASE IF NOT EXISTS test;
    USE test;
    
  3. Create a simple table named dtts using the following commands:

    CREATE TABLE dtts ( 
      id int(11) NOT NULL AUTO_INCREMENT, 
      name varchar(25), 
      dt datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), 
      ts timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) 
        ON UPDATE CURRENT_TIMESTAMP(3), 
      PRIMARY KEY (id) 
    );
    
  4. Insert some data into our new table using the INSERT command:

    INSERT INTO dtts (name) VALUES 
      ('Thomass'),('Gordon'),('Howard'),('Ezra'); 
    
  5. Fix the misspelling of Thomas:

    UPDATE dtts...

Unlock full access

Continue reading for free

A Packt free trial gives you instant online access to our library of over 7000 practical eBooks and videos, constantly updated with the latest in tech
bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete