Reputation: 1288
I'm trying to set up a DB for user registration and in want to have a column with the registration date in it, which should be the current time by default. I tried running this query:
CREATE TABLE `users` (
<some stuff>
`uRegDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
)
But it doesn't work with MariaDB. Anyone got a solution?
Upvotes: 8
Views: 24475
Reputation: 2439
I'm using MariaDB 10.3.39, on existing table, first I add attribute (created_at in my example) nullable default null, then run :
ALTER TABLE `my_table` CHANGE `created_at` `created_at` TIMESTAMP NULL
DEFAULT CURRENT_TIMESTAMP;
In some case, if you use phpmyadmin, it uses 'CURRENT_TIMESTAMP' between quotes as a string. In this case, remove quotes by editing the request.
Upvotes: 0
Reputation: 10092
I'm using MariaDB 10.1.12 and your statement works for me.
create table datetest (
date1 datetime default current_timestamp,
date2 timestamp default current_timestamp);
insert into datetest values ( );
The above statements execute without any error and the following result is displayed
+---------------------+---------------------+
| date1 | date2 |
+---------------------+---------------------+
| 2016-11-29 17:16:58 | 2016-11-29 17:16:58 |
+---------------------+---------------------+
Upvotes: 2