Home > Database > Mysql Tutorial > body text

How to set the default time in mysql

PHPz
Release: 2023-04-20 11:07:24
Original
21712 people have browsed it

MySQL is an open source relational database management system. In MySQL, we often need to set default time values. The default time means that when inserting data, if the field has no value, it will be assigned the preset time by default. In this article, we will introduce how to set the default time in MySQL.

In MySQL, we can set the default time value in two ways.

The first method is to set it when creating the table. We can specify the default value of a field as the current time (CURRENT_TIMESTAMP) when creating the table.

Sample code:

CREATE TABLE my_table (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Copy after login

In the above code, we create a my_table table, which has a creation time field created_at, and the default value is set to the current time.

When we insert data, if the creation time field does not specify a value, the current time will be used by default.

Sample code:

INSERT INTO my_table (name) VALUES ("test1");
Copy after login

In the above code, we only inserted a name field, and the created_at field will use the current time by default.

The second method is to set it when modifying the table structure. We can use the ALTER TABLE statement to modify the table structure and specify the default value of a field as the current time.

Sample code:

ALTER TABLE my_table 
MODIFY created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
Copy after login

In the above code, we modified the created_at field of the my_table table and specified that the default value of this field is the current time.

Afterwards, when we insert data into the table, if the creation time field does not specify a value, the current time will be used by default.

When we need to modify the default time value, we can also use the ALTER TABLE statement. Here are some commonly used sample codes:

1. Modify the default time value to a Unix timestamp:

ALTER TABLE my_table MODIFY created_at TIMESTAMP DEFAULT UNIX_TIMESTAMP();
Copy after login

2. Set the default time value to the current date:

ALTER TABLE my_table MODIFY created_at TIMESTAMP DEFAULT CURRENT_DATE();
Copy after login

3. Set the default time value to the day after the current time:

ALTER TABLE my_table MODIFY created_at TIMESTAMP DEFAULT DATE_ADD(NOW(), INTERVAL 1 DAY);
Copy after login

Summary:

To set the default time value in MySQL, we can specify it when creating a table or modifying the table structure . Using the default time value makes it easier for us to record the time when data is created or modified. In actual development, we can flexibly set the format and value rules of the default time value according to specific business needs.

The above is the detailed content of How to set the default time in mysql. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!