Insert Date in MySQL Table Using Triggers



Triggers are a great feature in MySQL that allows the automation of processes either before or after data has changed in a table. An important application of the usage of triggers is for automatic date addition to the database.

This article guides you on how to add the current date in MySQL using triggers every time a new record is being inserted. The following examples uses a BEFORE INSERT trigger in setting a date within a column of a table automatically.

BEFORE INSERT trigger

This trigger is executed right before a value is inserted into a database table. Whenever any INSERT statement is queried in this database, this Trigger automatically follows and then only the value is inserted into the table.

Syntax

Below is the syntax to create a trigger before inserting.

CREATE TRIGGER trigger_name
BEFORE INSERT ON table_name FOR EACH ROW
BEGIN
   -- SQL statements
END;

Example

This example tells how to use a BEFORE INSERT trigger in MySQL to automatically set the DueDate column to the current date whenever a new record is inserted.

Creating Table

Let us first create a table called DemoTable1584 with a single column DueDate, with a DATETIME as the data type to store date and time values. Following is the query to create a table -

CREATE TABLE DemoTable1584 (
    DueDate DATETIME
);
Creating a Trigger

Following is the query to create a BEFORE INSERT trigger called insertDate.

CREATE TRIGGER insertDate
BEFORE INSERT ON DemoTable1584
FOR EACH ROW 
SET NEW.DueDate = CURDATE();

Let us understand the important terms in the above code.

    BEFORE INSERT ON: The Before Insert Trigger is executed right before a value is inserted into a database table.

    SET NEW.DueDate = CURDATE(): Automatically sets the DueDate column to the current date (only the date part) for each new row if no specific value is provided.

    CURDATE(): This is the MySQL function that produces the current date in the form of (year-month-day) i.e. (2024-11-05). Unlike NOW() function, which produces both the current date and time. But here CURDATE() is used with a data type called DATETIME which expects to give both date and time. so MySQL fills time as 00:00:00 by default.

Inserting records

To activate the trigger we will insert a few records into the table using the INSERT command.

 insert into DemoTable1584 values();
 insert into DemoTable1584 values();
 insert into DemoTable1584 values();
 

Verification

As we didn't assign values the trigger will automatically add the current date to the table. Let us check it by fetching the data, we will be using SELECT command.

SELECT * FROM DemoTable1584;

Following is the output of the above query ?

DueDate
2024-11-05 00:00:00
2024-11-05 00:00:00
2024-11-05 00:00:00

As we can see, the current date 2024-11-05 and the time 00:00:00 have been set for all the items in the column 'DueDate'.

Benefits of Using MySQL Triggers for Date Insertion

The paragraph below explains the advantages of using MySQL triggers for automated and consistent date insertion.

    Automation: The trigger will Automatically set a date which saves time and ensures consistency.

    Error Reduced: Without relying on user input, the date is always set accurately.

    Centralized Logic: Date insertion is managed by the database, so it's easier to understand and maintain the application code.

    Consistency Across Records: DueDate will be filled in for each record to make your data accurate and consistent.

Updated on: 2025-01-27T16:32:11+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements