JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Using CURRENT_TIMESTAMP as a default value in MySQL

Author:JIYIK Last Updated:2025/04/21 Views:

This article teaches you how to use as 5.6.5in MySQL versions lower than . Thus, you can prevent MySQL error 1293.CURRENT_TIMESTAMPDEFAULT

Our approach involves reordering table columns and using DEFAULT 0and time values.


Reproduce the error in MySQL CURRENT_TIMESTAMPusingDEFAULT

Before I show you how to CURRENT_TIMESTAMPuse as DEFAULT, let's reproduce the error. This is the error you'll get when you try to CURRENT_TIMESTAMPuse as .DEFAULT

To continue:

  • Download 5.6.5a version of MySQL below . You'll get it as part of XAMPP 1.8.0 , or some other way.
  • Open MySQL in XAMPP and mysql -u root -plog in using . You will notice that the MySQL version is 5.5.2a, so it is smaller than 5.6.5.
  • Create a just_a_test_dbdatabase named .

Now, create a table in the database using the following SQL site_users:

CREATE TABLE site_users (
    user_id INT NOT NULL AUTO_INCREMENT,
    username VARCHAR(255) NOT NULL UNIQUE,
    user_firstname VARCHAR(255) NOT NULL,
    user_surname VARCHAR(255) NOT NULL,
    user_email_address VARCHAR(255) NOT NULL UNIQUE,
    user_password CHAR(40) NOT NULL,
    is_active BOOL NOT NULL DEFAULT FALSE,
    is_validated BOOL NOT NULL DEFAULT FALSE,
    date_validated TIMESTAMP,
    date_registered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (user_id)
) Engine=InnoDB;

You will receive an error like the one below:


Reorder table columns to use CURRENT_TIMESTAMPas in MySQLDEFAULT

If you reorder table columns in a SQL query, you can use CURRENT_TIMESTAMPas DEFAULT. The column with CURRENT_TIMESTAMPas DEFAULTshould come first; TIMESTAMPother columns with as values ​​should follow.

In the following SQL, we reorder the SQL and MySQL creates the table:

CREATE TABLE site_users (
    user_id INT NOT NULL AUTO_INCREMENT,
    username VARCHAR(255) NOT NULL UNIQUE,
    user_firstname VARCHAR(255) NOT NULL,
    user_surname VARCHAR(255) NOT NULL,
    user_email_address VARCHAR(255) NOT NULL UNIQUE,
    user_password CHAR(40) NOT NULL,
    is_active BOOL NOT NULL DEFAULT FALSE,
    is_validated BOOL NOT NULL DEFAULT FALSE,
    date_registered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    date_validated TIMESTAMP,
    PRIMARY KEY (user_id)
) Engine=InnoDB;

Output in MySQL console:


Use DEFAULT 0in MySQL CURRENT_TIMESTAMPasDEFAULT

Using DEFAULT 0is another option that allows you to use CURRENT_TIMESTAMPas DEFAULT. You apply it to TIMESTAMPa column that does not have a default value.

In our SQL, this is date_validatedthe column. The following SQL shows you how to do this:

CREATE TABLE site_users_2 (
    user_id INT NOT NULL AUTO_INCREMENT,
    username VARCHAR(255) NOT NULL UNIQUE,
    user_firstname VARCHAR(255) NOT NULL,
    user_surname VARCHAR(255) NOT NULL,
    user_email_address VARCHAR(255) NOT NULL UNIQUE,
    user_password CHAR(40) NOT NULL,
    is_active BOOL NOT NULL DEFAULT FALSE,
    is_validated BOOL NOT NULL DEFAULT FALSE,
    date_validated TIMESTAMP NOT NULL DEFAULT 0,
    date_registered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (user_id)
) Engine=InnoDB;

Output in MySQL console:


Use time values ​​in MySQL using CURRENT_TIMESTAMPasDEFAULT

If the default value of is time, you can use as TIMESTAMPin other columns . This time value is , we used it in the column.CURRENT_TIMESTAMPDEFAULT0000-00-00 00:00:00date_validated

The following SQL shows this:

CREATE TABLE site_users_3 (
    user_id INT NOT NULL AUTO_INCREMENT,
    username VARCHAR(255) NOT NULL UNIQUE,
    user_firstname VARCHAR(255) NOT NULL,
    user_surname VARCHAR(255) NOT NULL,
    user_email_address VARCHAR(255) NOT NULL UNIQUE,
    user_password CHAR(40) NOT NULL,
    is_active BOOL NOT NULL DEFAULT FALSE,
    is_validated BOOL NOT NULL DEFAULT FALSE,
    date_validated TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
    date_registered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (user_id)
) Engine=InnoDB;

Output in MySQL console:

Previous:If ELSE in MySQL

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

If ELSE in MySQL

Publish Date:2025/04/11 Views:86 Category:MySQL

In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering

DATETIME vs. TIMESTAMP in MySQL

Publish Date:2025/04/11 Views:117 Category:MySQL

DATETIME and TIMESTAMP are two different data types that can be used to store values ​​that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea

Execute multiple joins in one query in MYSQL

Publish Date:2025/04/11 Views:94 Category:MySQL

Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.

Joining 3 tables in MySQL

Publish Date:2025/04/11 Views:187 Category:MySQL

In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS

Use of UPDATE JOIN in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla

How to use the Row_Number() function in MySQL

Publish Date:2025/04/11 Views:143 Category:MySQL

In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti

Multiple primary keys in MySQL

Publish Date:2025/04/11 Views:66 Category:MySQL

In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b

Displaying foreign keys in MySQL

Publish Date:2025/04/11 Views:55 Category:MySQL

In this tutorial, we aim to explore how to display foreign keys for tables and columns in MySQL. The type of key that references a primary key, also known as the primary key of another table, is called a foreign key. Understanding the forei

Select first N rows in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

Sometimes, you have to select first N rows of MySQL database according to your project requirements. n The value of varies according to the requirement; it can be TOP 1 row or TOP 30 rows. We will learn how to select top N rows using the cl

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial