EN
MySQL - set current DATETIME as default
3 points
In this article, we would like to show you how to set current DATETIME
as default value in DATETIME
column in MySQL.
Quick solution:
xxxxxxxxxx
1
CREATE TABLE `table_name` (
2
`column_name` DATETIME DEFAULT CURRENT_TIMESTAMP
3
);
In this example, we will create users
table with registration_time
column which default value will be the current timestamp.
Query:
xxxxxxxxxx
1
CREATE TABLE `users` (
2
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
3
`username` VARCHAR(50) NOT NULL,
4
`registration_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
5
PRIMARY KEY (`id`)
6
);
Now when we insert some values to the table and don't specify the registration_time
value, the column will be filled with current timestamp value.
Query:
xxxxxxxxxx
1
INSERT INTO `users`
2
(`username`)
3
VALUES
4
('Tom'),
5
('Chris'),
6
('Jack'),
7
('Kim'),
8
('Marco'),
9
('Kate'),
10
('Nam');
Result:
