NOW() Function SQL

The NOW() function returns the current date and time. This can be a handy function for creating a time-stamp for INSERTs and UPDATEs. If we wished to add a new fictional character to our fict_chars table and use NOW for a date-time-stamp (provided we had a column for date time called dts). We could use the following:

INSERT INTO fict_chars (name, dts) VALUE ('Natasha Rostov', NOW())

To make this even easier, when we set up the table originally with the CREATE TABLE statement we could have included a parameter that would automatically insert NOW() in our dts column:

CREATE TABLE fict_chars
(
id INT NOT NULL,
name VARCHAR (255),
description TEXT,
book VARCHAR (255) CHECK (book != 'Flashman'),
author VARCHAR (255),
dts DATETIME NOT NULL DEFAULT NOW(), PRIMARY KEY(id)
)

This lets SQL do all the work and ensures that a date-time-stamp is attached for every INSERT. It will not automatically change the date-time-stamp on say an UPDATE statement, unless NOW() is specifically used in the statement. For example, if we wished to UPDATE the dts on row one:

UPDATE fict_chars SET dts=(NOW()) WHERE id = '1'

< MAX MIN functions | MySQL BIT_XOR Function >


Send notes in disappearing ink!

Interesting Pages