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:
CREATE TABLE `table_name` (
`column_name` DATETIME DEFAULT CURRENT_TIMESTAMP
);
Practical example
In this example, we will create users
table with registration_time
column which default value will be the current timestamp.
Query:
CREATE TABLE `users` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`registration_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
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:
INSERT INTO `users`
(`username`)
VALUES
('Tom'),
('Chris'),
('Jack'),
('Kim'),
('Marco'),
('Kate'),
('Nam');
Result: